public void DecryptsToOriginalPlainText() { byte[] plaintextBytes = Encoding.UTF8.GetBytes("This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least."); byte[] decryptedBytes; using (SymmetricAlgorithm algorithm = new AesManaged()) { byte[] wrongDecryptionKey = algorithm.Key; algorithm.GenerateKey(); byte[] encryptionKey = algorithm.Key; Assert.AreNotEqual(encryptionKey, wrongDecryptionKey); byte[] ciphertextBytes, iv; using (Encryptor encryptor = algorithm.CreateEncryptor(encryptionKey, out iv)) { Assert.AreEqual(encryptionKey, encryptor.Algorithm.Key); Assert.AreEqual(iv, encryptor.Algorithm.IV); ciphertextBytes = encryptor.Encrypt(plaintextBytes); } using (Decryptor decryptor = new Decryptor(algorithm, encryptionKey, iv, Encryption.DefaultOptions)) { Assert.AreEqual(encryptionKey, decryptor.Algorithm.Key); Assert.AreEqual(iv, decryptor.Algorithm.IV); decryptedBytes = decryptor.Decrypt(ciphertextBytes); } } Assert.AreEqual(plaintextBytes, decryptedBytes); }
public void CausesDecryptionToReturnNonsense() { byte[] plaintextBytes = Encoding.UTF8.GetBytes("This is a test!"); byte[] decryptedBytes, decryptedBytesFromWrongKey = null; using (SymmetricAlgorithm algorithm = new AesManaged()) { byte[] wrongDecryptionKey = algorithm.Key; algorithm.GenerateKey(); byte[] encryptionKey = algorithm.Key; Assert.AreNotEqual(encryptionKey, wrongDecryptionKey); byte[] ciphertextBytes, iv; using (Encryptor encryptor = algorithm.CreateEncryptor(encryptionKey, out iv)) { Assert.AreEqual(encryptionKey, encryptor.Algorithm.Key); Assert.AreEqual(iv, encryptor.Algorithm.IV); ciphertextBytes = encryptor.Encrypt(plaintextBytes); } using (Decryptor decryptorWithWrongKey = new Decryptor(algorithm, wrongDecryptionKey, iv, Encryption.DefaultOptions)) { Assert.AreEqual(wrongDecryptionKey, decryptorWithWrongKey.Algorithm.Key); Assert.AreEqual(iv, decryptorWithWrongKey.Algorithm.IV); try { decryptedBytesFromWrongKey = decryptorWithWrongKey.Decrypt(ciphertextBytes); } catch (CryptographicException e) { // "Padding is invalid and cannot be removed." Assert.IsNull(decryptedBytesFromWrongKey); Console.WriteLine(e.Message); } } using (Decryptor decryptor = new Decryptor(algorithm, encryptionKey, iv, Encryption.DefaultOptions)) { Assert.AreEqual(encryptionKey, decryptor.Algorithm.Key); Assert.AreEqual(iv, decryptor.Algorithm.IV); decryptedBytes = decryptor.Decrypt(ciphertextBytes); } } Assert.AreNotEqual(decryptedBytes, decryptedBytesFromWrongKey); Assert.AreEqual(plaintextBytes, decryptedBytes); }
public static byte[] Decrypt <T>( this byte[] encryptedBytes, [NotNull] byte[] key, [NotNull] byte[] iv, EncryptionOptions options = Encryption.DefaultOptions) where T : SymmetricAlgorithm { Contract.Requires <ArgumentNullException>(key != null); Contract.Requires <ArgumentNullException>(iv != null); byte[] decryptedBytes; using (Decryptor <T> decryptor = new Decryptor <T>(key, iv, options)) { decryptedBytes = decryptor.Decrypt(encryptedBytes); } return(decryptedBytes); }