Exemplo n.º 1
0
        public async Task DecryptAsync(Stream ciphertext, Stream plaintext, SymmetricEncryptionVariables encryptionVariables, CancellationToken cancellationToken = default(CancellationToken))
        {
            var buffer = new byte[ciphertext.Length - ciphertext.Position];
            await ciphertext.ReadAsync(buffer, 0, buffer.Length);

            var oldResult       = new SymmetricEncryptionResult(encryptionVariables, buffer);
            var plaintextBuffer = this.Decrypt(oldResult);
            await plaintext.WriteAsync(plaintextBuffer, 0, plaintextBuffer.Length);
        }
Exemplo n.º 2
0
		public void SymmetricEncryptionCompatTest() {
			var encryptionResult = new SymmetricEncryptionResult(
				Convert.FromBase64String(EncryptCompatTestKey),
				Convert.FromBase64String(EncryptCompatTestIV),
				Convert.FromBase64String(EncryptCompatTestCipher));
			byte[] plaintext = this.CryptoProvider.Decrypt(encryptionResult);
			byte[] expectedPlaintext = Convert.FromBase64String(EncryptCompatTestPlaintext);
			CollectionAssert.AreEqual(expectedPlaintext, plaintext);
		}
Exemplo n.º 3
0
        /// <summary>
        /// Symmetrically decrypts a buffer using the specified key.
        /// </summary>
        /// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
        /// <returns>
        /// The decrypted buffer.
        /// </returns>
        public override byte[] Decrypt(SymmetricEncryptionResult data)
        {
            var parameters = new ParametersWithIV(new KeyParameter(data.Key), data.IV);
            var decryptor  = this.GetCipher();

            decryptor.Init(false, parameters);
            byte[] plaintext = decryptor.DoFinal(data.Ciphertext);
            return(plaintext);
        }
		public void CtorAcceptsValidArguments() {
			var key = new byte[1];
			var iv = new byte[1];
			var ciphertext = new byte[1];
			var result = new SymmetricEncryptionResult(key, iv, ciphertext);
			Assert.That(result.Key, Is.SameAs(key));
			Assert.That(result.IV, Is.SameAs(iv));
			Assert.That(result.Ciphertext, Is.SameAs(ciphertext));
		}
 public void CtorAcceptsValidArguments()
 {
     var key = new byte[1];
     var iv = new byte[1];
     var ciphertext = new byte[1];
     var result = new SymmetricEncryptionResult(key, iv, ciphertext);
     Assert.Same(key, result.Key);
     Assert.Same(iv, result.IV);
     Assert.Same(ciphertext, result.Ciphertext);
 }
Exemplo n.º 6
0
        public void SymmetricEncryptionCompatTest()
        {
            var encryptionResult = new SymmetricEncryptionResult(
                Convert.FromBase64String(EncryptCompatTestKey),
                Convert.FromBase64String(EncryptCompatTestIV),
                Convert.FromBase64String(EncryptCompatTestCipher));

            byte[] plaintext         = this.CryptoProvider.Decrypt(encryptionResult);
            byte[] expectedPlaintext = Convert.FromBase64String(EncryptCompatTestPlaintext);
            Assert.Equal(expectedPlaintext, plaintext);
        }
Exemplo n.º 7
0
        public void CtorAcceptsValidArguments()
        {
            var key        = new byte[1];
            var iv         = new byte[1];
            var ciphertext = new byte[1];
            var result     = new SymmetricEncryptionResult(key, iv, ciphertext);

            Assert.That(result.Key, Is.SameAs(key));
            Assert.That(result.IV, Is.SameAs(iv));
            Assert.That(result.Ciphertext, Is.SameAs(ciphertext));
        }
        public void CtorAcceptsValidArguments()
        {
            var key        = new byte[1];
            var iv         = new byte[1];
            var ciphertext = new byte[1];
            var result     = new SymmetricEncryptionResult(key, iv, ciphertext);

            Assert.Same(key, result.Key);
            Assert.Same(iv, result.IV);
            Assert.Same(ciphertext, result.Ciphertext);
        }
Exemplo n.º 9
0
		public byte[] Decrypt(SymmetricEncryptionResult data) {
			for (int i = 0; i < data.Key.Length; i++) {
				Assert.That(data.Ciphertext[i], Is.EqualTo(data.Key[i]));
			}

			for (int i = 0; i < data.IV.Length; i++) {
				Assert.That(data.Ciphertext[data.Key.Length + i], Is.EqualTo(data.IV[i]));
			}

			var plaintext = new byte[data.Ciphertext.Length - data.Key.Length - data.IV.Length];
			Array.Copy(data.Ciphertext, data.Key.Length + data.IV.Length, plaintext, 0, plaintext.Length);
			return plaintext;
		}
Exemplo n.º 10
0
        /// <summary>
        /// Symmetrically decrypts a buffer using the specified key.
        /// </summary>
        /// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
        /// <returns>
        /// The decrypted buffer.
        /// </returns>
        public override byte[] Decrypt(SymmetricEncryptionResult data)
        {
            using (var alg = SymmetricAlgorithm.Create(this.SymmetricEncryptionConfiguration.AlgorithmName)) {
                alg.Mode    = (CipherMode)Enum.Parse(typeof(CipherMode), this.SymmetricEncryptionConfiguration.BlockMode);
                alg.Padding = (PaddingMode)Enum.Parse(typeof(PaddingMode), this.SymmetricEncryptionConfiguration.Padding);
                using (var decryptor = alg.CreateDecryptor(data.Key, data.IV)) {
                    using (var plaintextStream = new MemoryStream()) {
                        using (var cryptoStream = new CryptoStream(plaintextStream, decryptor, CryptoStreamMode.Write)) {
                            cryptoStream.Write(data.Ciphertext, 0, data.Ciphertext.Length);
                        }

                        return(plaintextStream.ToArray());
                    }
                }
            }
        }
Exemplo n.º 11
0
        public byte[] Decrypt(SymmetricEncryptionResult data)
        {
            for (int i = 0; i < data.Key.Length; i++)
            {
                Assert.That(data.Ciphertext[i], Is.EqualTo(data.Key[i]));
            }

            for (int i = 0; i < data.IV.Length; i++)
            {
                Assert.That(data.Ciphertext[data.Key.Length + i], Is.EqualTo(data.IV[i]));
            }

            var plaintext = new byte[data.Ciphertext.Length - data.Key.Length - data.IV.Length];

            Array.Copy(data.Ciphertext, data.Key.Length + data.IV.Length, plaintext, 0, plaintext.Length);
            return(plaintext);
        }
Exemplo n.º 12
0
		/// <summary>
		/// Symmetrically decrypts a buffer using the specified key.
		/// </summary>
		/// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
		/// <returns>
		/// The decrypted buffer.
		/// </returns>
		public override byte[] Decrypt(SymmetricEncryptionResult data) {
			var symmetricKey = SymmetricAlgorithm.CreateSymmetricKey(data.Key.ToBuffer());
			return CryptographicEngine.Decrypt(symmetricKey, data.Ciphertext.ToBuffer(), data.IV.ToBuffer()).ToArray();
		}
Exemplo n.º 13
0
		/// <summary>
		/// Symmetrically decrypts a buffer using the specified key.
		/// </summary>
		/// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
		/// <returns>
		/// The decrypted buffer.
		/// </returns>
		public override byte[] Decrypt(SymmetricEncryptionResult data) {
			var parameters = new ParametersWithIV(new KeyParameter(data.Key), data.IV);
			var decryptor = this.GetCipher();
			decryptor.Init(false, parameters);
			byte[] plaintext = decryptor.DoFinal(data.Ciphertext);
			return plaintext;
		}
Exemplo n.º 14
0
		/// <summary>
		/// Symmetrically decrypts a buffer using the specified key.
		/// </summary>
		/// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
		/// <returns>
		/// The decrypted buffer.
		/// </returns>
		public override byte[] Decrypt(SymmetricEncryptionResult data) {
			using (var alg = SymmetricAlgorithm.Create(this.SymmetricEncryptionConfiguration.AlgorithmName)) {
				alg.Mode = (CipherMode)Enum.Parse(typeof(CipherMode), this.SymmetricEncryptionConfiguration.BlockMode);
				alg.Padding = (PaddingMode)Enum.Parse(typeof(PaddingMode), this.SymmetricEncryptionConfiguration.Padding);
				using (var decryptor = alg.CreateDecryptor(data.Key, data.IV)) {
					using (var plaintextStream = new MemoryStream()) {
						using (var cryptoStream = new CryptoStream(plaintextStream, decryptor, CryptoStreamMode.Write)) {
							cryptoStream.Write(data.Ciphertext, 0, data.Ciphertext.Length);
						}

						return plaintextStream.ToArray();
					}
				}
			}
		}
Exemplo n.º 15
0
        /// <summary>
        /// Symmetrically decrypts a buffer using the specified key.
        /// </summary>
        /// <param name="data">The encrypted data and the key and IV used to encrypt it.</param>
        /// <returns>
        /// The decrypted buffer.
        /// </returns>
        public override byte[] Decrypt(SymmetricEncryptionResult data)
        {
            var symmetricKey = SymmetricAlgorithm.CreateSymmetricKey(data.Key.ToBuffer());

            return(CryptographicEngine.Decrypt(symmetricKey, data.Ciphertext.ToBuffer(), data.IV.ToBuffer()).ToArray());
        }