示例#1
0
 public async void WorkWithImageFileAndWrongKeyTestAsync()
 {
     var RAW_FILE = Path.Combine("Testfiles", "MyAwesomeChipmunkKiller.jpg");
     var OUTPUT_DIRECTORY = Path.Combine("Testfiles", "decrypted");
     const string PRIVATE_KEY = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
     const string PUBLIC_KEY = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
     var keyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     var testKeyPair = PublicKeyBox.GenerateKeyPair();
     Console.Write("Encrypting testfile . . .\n");
     //encrypt the file with an ephmeral key
     var encryptedFile =
         await
             Cryptor.EncryptFileWithStreamAsync(keyPair, testKeyPair.PublicKey, RAW_FILE, null, OUTPUT_DIRECTORY,
                 ".test", true);
     Console.Write("Decrypting testfile . . .\n");
     //try to decrypt with an wrong key
     var decryptedFile =
         await
             Cryptor.DecryptFileWithStreamAsync(keyPair, Path.Combine(OUTPUT_DIRECTORY, encryptedFile),
                 OUTPUT_DIRECTORY);
     Console.Write("Get checksum of testfiles . . .\n");
     Assert.AreEqual(Utils.GetChecksum(RAW_FILE),
         Utils.GetChecksum(Path.Combine(OUTPUT_DIRECTORY, decryptedFile)));
     //clear garbage 
     File.Delete(Path.Combine(OUTPUT_DIRECTORY, encryptedFile));
     File.Delete(Path.Combine(OUTPUT_DIRECTORY, decryptedFile));
 }
 public void EncryptionInputFileNotFoundTest()
 {
     const string PRIVATE_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     Cryptor.EncryptFileWithStream(testKeyPair, "badfile");
 }
 public async void DecryptionInputFileNotFoundTestAsync()
 {
     const string PRIVATE_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     await Cryptor.DecryptFileWithStreamAsync(testKeyPair, "badfile", Path.Combine("Testfiles", "decrypted"));
 }
示例#4
0
		public async Task WorkWithImageFileAndWrongKeyTestAsync()
		{
			var rawFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			var outputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted");
			const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
			const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
			var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			var testKeyPair = PublicKeyBox.GenerateKeyPair();
			Console.Write("Encrypting testfile . . .\n");
			//encrypt the file with an ephmeral key
			var encryptedFile =
				await
					Cryptor.EncryptFileWithStreamAsync(keyPair, testKeyPair.PublicKey, rawFile, null, outputDirectory,
						".test", true);
			Console.Write("Decrypting testfile . . .\n");
			//try to decrypt with an wrong key
			var decryptedFile =
				Assert.ThrowsAsync<CryptographicException>(
					async () => await
						Cryptor.DecryptFileWithStreamAsync(keyPair,
							Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile),
							outputDirectory));


			File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile));
		}
 public KeyPair(Sodium.KeyPair a)
 {
     this.PublicKey = PublicKeyAuth
                      .ConvertEd25519PublicKeyToCurve25519PublicKey(a.PublicKey);
     this.PrivateKey = PublicKeyAuth
                       .ConvertEd25519SecretKeyToCurve25519SecretKey(a.PrivateKey);
 }
		public void EncryptionNoPublicKeyInPairTest()
		{
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(null, Utilities.HexToBinary(privateKey));
			Assert.Throws<NullReferenceException>(
				() => { Cryptor.EncryptFileWithStream(testKeyPair, "badfile"); });
		}
示例#7
0
		public async Task WorkWithImageFileByteArrayTestAsync()
		{
			var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			progressEncrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
			progressDecrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n"); };
			var rawFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			var outputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted");
			const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
			const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
			var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Console.Write("Encrypting testfile . . .\n");
			var encryptedFile =
				await
					Cryptor.EncryptFileWithStreamAsync(keyPair.PrivateKey, keyPair.PublicKey,
						Utilities.HexToBinary(publicKey), rawFile, progressEncrypt, outputDirectory, ".test", true);
			Console.Write("Decrypting testfile . . .\n");
			var decryptedFileObject =
				await
					Cryptor.DecryptFileWithStreamAsync(keyPair.PrivateKey,
						Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile),
						progressDecrypt);
			Console.Write("Get checksum of testfiles . . .\n");
			Assert.AreEqual(Utils.GetChecksum(rawFile), Utils.GetChecksum(decryptedFileObject.FileData));
			//clear garbage 
			File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile));
		}
		public void DecryptionCancellationTestAsync()
		{
			var cancellationTokenSource = new CancellationTokenSource();
			var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			progressEncrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
			progressDecrypt.ProgressChanged +=
				(s, e) =>
				{
					if (e.ProgressPercentage > 10)
					{
						cancellationTokenSource.Cancel();
					}
					Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n");
				};
			var rawFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			var outputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted");
			const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
			const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
			var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Console.Write("Encrypting testfile . . .\n");
			var encryptedFile = Cryptor.EncryptFileWithStream(keyPair.PrivateKey, keyPair.PublicKey,
				Utilities.HexToBinary(publicKey), rawFile, outputDirectory, ".test", true);
			Console.Write("Decrypting testfile . . .\n");
			Assert.ThrowsAsync<TaskCanceledException>(async () => await
				Cryptor.DecryptFileWithStreamAsync(keyPair.PrivateKey, Path.Combine(outputDirectory, encryptedFile),
					outputDirectory, progressDecrypt, cancellationToken: cancellationTokenSource.Token));
		}
示例#9
0
        /// <summary>
        ///   Constructs the server given the Network key and its keypair
        /// </summary>
        /// <param name="network_key">
        ///   The key that identifies the network
        /// </param>
        /// <param name="server_keypair">
        ///   The server's long term keypair
        /// </param>
        public Server(byte[] network_key, Sodium.KeyPair server_keypair)
        {
            this._network_key             = network_key;
            this._longterm_server_keypair = server_keypair;

            _ephemeral_server_keypair = new KeyPair(PublicKeyAuth.GenerateKeyPair());
        }
示例#10
0
 public async void WorkWithImageFileTestAsync()
 {
     var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
     var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
     progressEncrypt.ProgressChanged +=
         (s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
     progressDecrypt.ProgressChanged +=
         (s, e) => { Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n"); };
     var RAW_FILE = Path.Combine("Testfiles", "MyAwesomeChipmunkKiller.jpg");
     var OUTPUT_DIRECTORY = Path.Combine("Testfiles", "decrypted");
     const string PRIVATE_KEY = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
     const string PUBLIC_KEY = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
     var keyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     Console.Write("Encrypting testfile . . .\n");
     var encryptedFile =
         await
             Cryptor.EncryptFileWithStreamAsync(keyPair.PrivateKey, keyPair.PublicKey,
                 Utilities.HexToBinary(PUBLIC_KEY), RAW_FILE, progressEncrypt, OUTPUT_DIRECTORY, ".test", true);
     Console.Write("Decrypting testfile . . .\n");
     var decryptedFile =
         await
             Cryptor.DecryptFileWithStreamAsync(keyPair.PrivateKey, Path.Combine(OUTPUT_DIRECTORY, encryptedFile),
                 OUTPUT_DIRECTORY, progressDecrypt);
     Console.Write("Get checksum of testfiles . . .\n");
     Assert.AreEqual(Utils.GetChecksum(RAW_FILE),
         Utils.GetChecksum(Path.Combine(OUTPUT_DIRECTORY, decryptedFile)));
     //clear garbage 
     File.Delete(Path.Combine(OUTPUT_DIRECTORY, encryptedFile));
     File.Delete(Path.Combine(OUTPUT_DIRECTORY, decryptedFile));
 }
 public void EncryptionInvalidPrivateKeyInPairTest()
 {
     const string PRIVATE_KEY =
         "863df54207c285feac2c22235c336869fee8dba6605b8e1bc45cc8aa5e1be3fd7e53781865717d686cb3fee427823ffd8c71ea6a4d8f79c0b410457c9f881fa3";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     Cryptor.EncryptFileWithStream(testKeyPair, "badfile");
 }
 public async void DecryptioInvalidPublicKeyInPairTestAsync()
 {
     const string PRIVATE_KEY =
         "863df54207c285feac2c22235c336869fee8dba6605b8e1bc45cc8aa5e1be3fd7e53781865717d686cb3fee427823ffd8c71ea6a4d8f79c0b410457c9f881fa3";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     await Cryptor.DecryptFileWithStreamAsync(testKeyPair, "badfile", Path.Combine("Testfiles", "decrypted"));
 }
 public async void DecryptionOutputFolderNotFoundTestAsync()
 {
     var TESTFILE_RAW = Path.Combine("Testfiles", "MyAwesomeChipmunkKiller.jpg");
     const string PRIVATE_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     await Cryptor.DecryptFileWithStreamAsync(testKeyPair, TESTFILE_RAW, "badfolder");
 }
		public void DecryptionInputFileNotFoundTestAsync()
		{
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Assert.ThrowsAsync<FileNotFoundException>(
				async () => await Cryptor.DecryptFileWithStreamAsync(testKeyPair, "badfile", Path.Combine("Testfiles", "decrypted")));
		}
示例#15
0
        /// <summary>
        ///  Sign the specified message with the private key. 
        /// </summary>
        /// <param name='privateKey'>
        ///  The private key. 
        /// </param>
        /// <param name='message'>
        ///  The message. 
        /// </param>
        /// <returns>
        ///  The message that has been signed (encrypted) by the private key. 
        /// </returns>
        public byte[] Sign(byte[] privateKey, byte[] message)
        {
            if(Keys == null)
            {
                Keys = PublicKeyAuth.GenerateKeyPair(privateKey);
            }

            return PublicKeyAuth.Sign(message, Keys.PrivateKey);
        }
		public void EncryptionBadFileExtensionTest()
		{
			var testfileRaw = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Assert.Throws<ArgumentOutOfRangeException>(
				() => { Cryptor.EncryptFileWithStream(testKeyPair, testfileRaw, Path.Combine("Testfiles", "decrypted"), "hulk"); });
		}
		public void DecryptioInvalidPublicKeyInPairTestAsync()
		{
			const string privateKey =
				"863df54207c285feac2c22235c336869fee8dba6605b8e1bc45cc8aa5e1be3fd7e53781865717d686cb3fee427823ffd8c71ea6a4d8f79c0b410457c9f881fa3";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Assert.ThrowsAsync<ArgumentOutOfRangeException>(
				async () => await Cryptor.DecryptFileWithStreamAsync(testKeyPair, "badfile", Path.Combine("Testfiles", "decrypted")));
		}
		public void EncryptionInputFileNotFoundTest()
		{
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));

			Assert.Throws<FileNotFoundException>(
				() => { Cryptor.EncryptFileWithStream(testKeyPair, "badfile"); });
		}
        public void GenerateKeySeedTest()
        {
            var expected = new KeyPair(Utilities.HexToBinary("76a1592044a6e4f511265bca73a604d90b0529d1df602be30a19a9257660d1f5"),
            Utilities.HexToBinary("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff76a1592044a6e4f511265bca73a604d90b0529d1df602be30a19a9257660d1f5"));
              var actual = PublicKeyAuth.GenerateKeyPair(Utilities.HexToBinary("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));

              CollectionAssert.AreEqual(expected.PublicKey, actual.PublicKey);
              CollectionAssert.AreEqual(expected.PrivateKey, actual.PrivateKey);
        }
		public void DecryptionOutputFolderNotFoundTest()
		{
			var testfileRaw = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Assert.Throws<DirectoryNotFoundException>(
				() => { Cryptor.DecryptFileWithStream(testKeyPair, testfileRaw, "badfolder"); });
		}
        /// <summary>
        ///   Constructs the client given
        /// </summary>
        /// <param name="network_key">
        ///   The key that identifies the network
        /// </param>
        /// <param name="server_pk">
        ///   The long term server public key
        /// </param>
        /// <param name="client_keys">
        ///   The long term client key pair
        /// </param>
        public Client(byte[] network_key, byte[] server_pk, Sodium.KeyPair client_keys)
        {
            this._network_key = network_key;

            var ed_keypair = PublicKeyAuth.GenerateKeyPair();

            _ephemeral_client_keypair = new KeyPair(ed_keypair);

            _longterm_server_pk      = server_pk;
            _longterm_client_keypair = client_keys;
        }
        /// <summary>
        ///     Constructor to prepare the communication.
        /// </summary>
        /// <param name="clientPrivateKey">The clients 32 byte private key (hex format)</param>
        /// <exception cref="NotSupportedException"></exception>
        public KnownPasswords(string clientPrivateKey)
        {
            SignaurKeyPair =
                new KeyPair(
                    PublicKeyAuth.ExtractEd25519PublicKeyFromEd25519SecretKey(Utilities.HexToBinary(clientPrivateKey)),
                    Utilities.HexToBinary(clientPrivateKey));

            EncryptionKeyPair = PublicKeyBox.GenerateKeyPair();

            // the API only accepts TLS 1.2 connections
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            _restClient = new RestClient(ServerApiUrl);
        }
示例#23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CoCKeyPair"/> class with the
        /// specified public key and private key.
        /// </summary>
        /// <param name="publicKey">Public key.</param>
        /// <param name="privateKey">Private key.</param>
        /// <exception cref="ArgumentNullException"><paramref name="publicKey"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="privateKey"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="publicKey"/> length is not 32.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="privateKey"/> length is not 32.</exception>
        public CoCKeyPair(byte[] publicKey, byte[] privateKey)
        {
            if (publicKey == null)
                throw new ArgumentNullException("publicKey");
            if (publicKey.Length != PublicKeyBox.PublicKeyBytes)
                throw new ArgumentOutOfRangeException("publicKey", "publicKey must be 32 bytes in length.");

            if (privateKey == null)
                throw new ArgumentNullException("privateKey");
            if (privateKey.Length != PublicKeyBox.SecretKeyBytes)
                throw new ArgumentOutOfRangeException("privateKey", "publicKey must be 32 bytes in length.");

            _keyPair = new KeyPair(publicKey, privateKey);
        }
示例#24
0
 public void WorkWithImageFileTest()
 {
     var rawFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
     var outputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted");
     const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
     const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
     var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
     Console.Write("Encrypting testfile . . .\n");
     var encryptedFile = Cryptor.EncryptFileWithStream(keyPair, Utilities.HexToBinary(publicKey), rawFile,
         outputDirectory, ".test", true);
     Console.Write("Decrypting testfile . . .\n");
     var decryptedFile = Cryptor.DecryptFileWithStream(keyPair, Path.Combine(outputDirectory, encryptedFile),
         outputDirectory);
     Console.Write("Get checksum of testfiles . . .\n");
     Assert.AreEqual(Utils.GetChecksum(rawFile),
         Utils.GetChecksum(Path.Combine(outputDirectory, decryptedFile)));
     //clear garbage 
     File.Delete(Path.Combine(outputDirectory, encryptedFile));
     File.Delete(Path.Combine(outputDirectory, decryptedFile));
 }
        public void KeyPairModuloTest()
        {
            //Don`t copy bobSk for other tests (bad key)!
              //30 byte
              var bobSk = new byte[] {
                0x5d,0xab,0x08,0x7e,0x62,0x4a,0x8a,0x4b,
                0x79,0xe1,0x7f,0x8b,0x83,0x80,0x0e,0xe6,
                0x6f,0x3b,0xb1,0x29,0x26,0x18,0xb6,0xfd,
                0x1c,0x2f,0x8b,0x27,0xff,0x88
            };

              //32 byte
              var bobPk = new byte[] {
                0xde,0x9e,0xdb,0x7d,0x7b,0x7d,0xc1,0xb4,
                0xd3,0x5b,0x61,0xc2,0xec,0xe4,0x35,0x37,
                0x3f,0x83,0x43,0xc8,0x5b,0x78,0x67,0x4d,
                0xad,0xfc,0x7e,0x14,0x6f,0x88,0x2b,0x4f
            };

              var kp = new KeyPair(bobPk, bobSk);

              //we should never get here
              Assert.IsNull(kp);
        }
		public void EncryptionCancellationTestAsync()
		{
			var cancellationTokenSource = new CancellationTokenSource();
			var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			progressEncrypt.ProgressChanged +=
				(s, e) =>
				{
					if (e.ProgressPercentage > 10)
					{
						cancellationTokenSource.Cancel();
					}
					Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n");
				};

			var testfileRaw = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "MyAwesomeChipmunkKiller.jpg");
			const string privateKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
			const string publicKey = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";

			var testKeyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Assert.ThrowsAsync<TaskCanceledException>(async () => await
				Cryptor.EncryptFileWithStreamAsync(testKeyPair, testfileRaw, progressEncrypt,
					Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted"), ".sccef",
					cancellationToken: cancellationTokenSource.Token));
		}
 public void EncryptionNoPrivateKeyInPairTest()
 {
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), null);
     Cryptor.EncryptFileWithStream(testKeyPair, "badfile");
 }
示例#28
0
		public async Task WorkWithStreamTestSmallAsync()
		{
			var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			progressEncrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
			progressDecrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n"); };

			const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
			const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
			var outputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted");
			var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));

			var TESTFILE_RAW = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "verysmallfile.dat");
			const long TESTFILE_SIZE_KB = 1;

			Console.Write("Generating {0} KB testfile . . .\n", TESTFILE_SIZE_KB);
			var fs = new FileStream(TESTFILE_RAW, FileMode.CreateNew);
			fs.Seek(TESTFILE_SIZE_KB*1024, SeekOrigin.Begin);
			fs.WriteByte(0);
			fs.Close();

			var data = File.ReadAllBytes(TESTFILE_RAW);
			var stream = new MemoryStream(data);
			Console.Write("Encrypting MemoryStream . . .\n");
			var encryptedFile =
				await
					Cryptor.EncrypMemoryStreamAsync(keyPair.PrivateKey, keyPair.PublicKey,
						Utilities.HexToBinary(publicKey), TESTFILE_RAW, stream, outputDirectory, ".test", true, progressEncrypt)
						.ConfigureAwait(false);

			Console.Write("Decrypting testfile (" + encryptedFile + ") . . .\n");
			var decryptedFileObject =
				await
					Cryptor.DecryptFileWithStreamAsync(keyPair.PrivateKey, Path.Combine(outputDirectory, encryptedFile),
						progressDecrypt).ConfigureAwait(false);
			Console.Write("Get checksum of testfiles . . .\n");
			Assert.AreEqual(Utils.GetChecksum(TESTFILE_RAW), Utils.GetChecksum(decryptedFileObject.FileData));
			//clear garbage 
			File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile));
			File.Delete(TESTFILE_RAW);
		}
示例#29
0
 /// <summary>
 /// Decrypts a file asynchron into memory with libsodium and protobuf-net.
 /// </summary>
 /// <param name="keyPair">The KeyPair to decrypt the ephemeralKey.</param>
 /// <param name="inputFile">An encrypted file.</param>
 /// <param name="decryptionProgress">StreamCryptorTaskAsyncProgress object.</param>
 /// <param name="cancellationToken">Token to request task cancellation.</param>
 /// <returns>A DecryptedFile object.</returns>
 /// <remarks>This method can throw an OutOfMemoryException when there is not enough ram to hold the DecryptedFile!</remarks>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="FileNotFoundException"></exception>
 /// <exception cref="BadLastFileChunkException"></exception>
 /// <exception cref="BadFileChunkException"></exception>
 /// <exception cref="BadFileFooterException"></exception>
 /// <exception cref="BadFileHeaderException"></exception>
 /// <exception cref="IOException"></exception>
 /// <exception cref="OutOfMemoryException"></exception>
 /// <exception cref="OperationCanceledException"></exception>
 public static async Task<DecryptedFile> DecryptFileWithStreamAsync(KeyPair keyPair, string inputFile, IProgress<StreamCryptorTaskAsyncProgress> decryptionProgress = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     //validate the keyPair
     if (!ValidateKeyPair(keyPair))
     {
         throw new ArgumentOutOfRangeException("keypair", "invalid keypair");
     }
     return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, decryptionProgress, cancellationToken).ConfigureAwait(false);
 }
 public void EncryptionBadFileExtensionTest()
 {
     var TESTFILE_RAW = Path.Combine("Testfiles", "MyAwesomeChipmunkKiller.jpg");
     const string PRIVATE_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     const string PUBLIC_KEY = "1158b1ea7d45919968b87dab6cab27eff5871304ea9856588e9ec02a6d93c42e";
     var testKeyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
     Cryptor.EncryptFileWithStream(testKeyPair, TESTFILE_RAW, Path.Combine("Testfiles", "decrypted"), "hulk");
 }
        public async void DecryptionCancellationTestAsync()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
            var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
            progressEncrypt.ProgressChanged +=
                (s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
            progressDecrypt.ProgressChanged +=
                (s, e) =>
                {
                    if (e.ProgressPercentage > 10)
                    {
                        cancellationTokenSource.Cancel();
                    }
                    Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n");
                };
            var RAW_FILE = Path.Combine("Testfiles", "MyAwesomeChipmunkKiller.jpg");
            var OUTPUT_DIRECTORY = Path.Combine("Testfiles", "decrypted");
            const string PRIVATE_KEY = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
            const string PUBLIC_KEY = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
            var keyPair = new KeyPair(Utilities.HexToBinary(PUBLIC_KEY), Utilities.HexToBinary(PRIVATE_KEY));
            Console.Write("Encrypting testfile . . .\n");
            var encryptedFile =
                await
                    Cryptor.EncryptFileWithStreamAsync(keyPair.PrivateKey, keyPair.PublicKey,
                        Utilities.HexToBinary(PUBLIC_KEY), RAW_FILE, progressEncrypt, OUTPUT_DIRECTORY, ".test", true, cancellationTokenSource.Token);
            Console.Write("Decrypting testfile . . .\n");
            var decryptedFile =
                await
                    Cryptor.DecryptFileWithStreamAsync(keyPair.PrivateKey, Path.Combine(OUTPUT_DIRECTORY, encryptedFile),
                        OUTPUT_DIRECTORY, progressDecrypt, cancellationToken: cancellationTokenSource.Token);

        }
示例#32
0
		public async Task WorkWithVerySmallFileTestAsync()
		{
			var progressEncrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			var progressDecrypt = new Progress<StreamCryptorTaskAsyncProgress>();
			progressEncrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Encrypting: " + e.ProgressPercentage + "%\n"); };
			progressDecrypt.ProgressChanged +=
				(s, e) => { Console.WriteLine("Decrypting: " + e.ProgressPercentage + "%\n"); };
			var testfileRaw = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "verysmallfile.dat");
			var testfileDecryptedFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles", "decrypted",
				"verysmallfile.dat");
			var testfileDecryptedOutputDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "Testfiles",
				"decrypted");
			const string outputDirectory = "Testfiles";
			const long testfileSizeKb = 1;
			const string privateKey = "31d9040b00a170532929b37db0afcb989e4175f96e5f9667ee8cbf5706679a71";
			const string publicKey = "6d0deec730700f9f60687a4e6e8755157ca22ea2f3815b9bf14b1fe9ae6a0b4d";
			var keyPair = new KeyPair(Utilities.HexToBinary(publicKey), Utilities.HexToBinary(privateKey));
			Console.Write("Generating {0} KB testfile . . .\n", testfileSizeKb);
			var fs = new FileStream(testfileRaw, FileMode.CreateNew);
			fs.Seek(testfileSizeKb*1024, SeekOrigin.Begin);
			fs.WriteByte(0);
			fs.Close();
			Console.Write("Encrypting testfile . . .\n");
			var encryptedFile = await Cryptor.EncryptFileWithStreamAsync(keyPair, testfileRaw, progressEncrypt);
			Console.Write("Decrypting testfile . . .\n");
			await
				Cryptor.DecryptFileWithStreamAsync(keyPair,
					Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile),
					testfileDecryptedOutputDirectory, progressDecrypt);
			Console.Write("Get checksum of testfiles . . .\n");
			Assert.AreEqual(Utils.GetChecksum(testfileRaw), Utils.GetChecksum(testfileDecryptedFile));
			//clear garbage 
			File.Delete(testfileRaw);
			File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, outputDirectory, encryptedFile));
			File.Delete(testfileDecryptedFile);
		}
示例#33
0
 /// <summary>
 ///  Makes the public key from the provider private key. 
 /// </summary>
 /// <returns>
 ///  The public key. 
 /// </returns>
 /// <param name='privateKey'>
 ///  Private key. 
 /// </param>
 public byte[] MakePublicKey(byte[] privateKey)
 {
     Keys = PublicKeyAuth.GenerateKeyPair(privateKey);
     return Keys.PublicKey;
 }