public void TestSimpleEncryptDecrypt() { using (AESCryptoKey k1 = new AESCryptoKey()) { byte[] test = new byte[240]; byte[] cypher1 = k1.Encrypt(test); byte[] cypher2 = k1.Encrypt(test); Assert.AreEqual(cypher1, cypher2); Assert.AreEqual(test, k1.Decrypt(cypher1)); } }
public void TestDisposal() { string test; CryptoKey key = new AESCryptoKey(); test = key.Encrypt("Test"); key.Dispose(); key.Decrypt(test); Assert.Fail(); }
public void TestCopyKeyAndIV() { byte[] ivrandom = new byte[16]; new Random().NextBytes(ivrandom); using (AESCryptoKey k1 = new AESCryptoKey()) using (AESCryptoKey k2 = new AESCryptoKey(k1.Key, k1.IV)) using (AESCryptoKey kBadIv = new AESCryptoKey(k1.Key, ivrandom)) { Assert.AreEqual(k1.Key, k2.Key); Assert.AreEqual(k1.IV, k2.IV); Assert.AreEqual("test", k2.Decrypt(k1.Encrypt("test"))); Assert.AreEqual(k1.Key, kBadIv.Key); Assert.AreNotEqual(k1.IV, kBadIv.IV); try { //one of two possible outcomes, junk or exception Assert.AreNotEqual("test", kBadIv.Decrypt(k1.Encrypt("test"))); } catch (System.Security.Cryptography.CryptographicException) { } } }