public static void TestInvalidArguments() { AesKey key = new AesKey(); AesIV iv = new AesIV(); Assert.Throws<ArgumentNullException>(() => { if (new AesCrypto(null) == null) { } }); Assert.Throws<ArgumentNullException>(() => { if (new AesCrypto(null, iv, CipherMode.CBC, PaddingMode.None) == null) { } }); Assert.Throws<ArgumentNullException>(() => { if (new AesCrypto(key, null, CipherMode.CBC, PaddingMode.None) == null) { } }); Assert.DoesNotThrow(() => { if (new AesCrypto(key, iv, CipherMode.CBC, PaddingMode.None) == null) { } }); }
public static void TestMethods() { AesIV zeroIV = AesIV.Zero; Assert.That(zeroIV.GetBytes(), Is.EquivalentTo(new byte[16]), "The IV 'zero' should consist of all zeros."); AesIV iv = new AesIV(new byte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); Assert.That(iv.GetBytes(), Is.EquivalentTo(new byte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }), "An IV specified should consist of just those bytes."); iv = new AesIV(); Assert.That(iv.GetBytes(), Is.Not.EquivalentTo(new byte[16]), "A random iv will in practice never be all zeros."); }
/// <summary> /// Instantiate a transformation /// </summary> /// <param name="key">The key</param> /// <param name="iv">Initial Vector</param> /// <param name="cipherMode">Mode of operation, typically CBC</param> /// <param name="paddingMode">Padding mode, typically PCS7</param> public AesCrypto(AesKey key, AesIV iv, CipherMode cipherMode, PaddingMode paddingMode) { if (key == null) { throw new ArgumentNullException("key"); } if (iv == null) { throw new ArgumentNullException("iv"); } _aes = new AesManaged(); _aes.Key = key.GetBytes(); _aes.Mode = cipherMode; _aes.IV = iv.GetBytes(); _aes.Padding = paddingMode; }
public static void TestDoubleDispose() { AesKey key = new AesKey(); AesIV iv = new AesIV(); AesCrypto crypto = new AesCrypto(key, iv, CipherMode.CBC, PaddingMode.None); crypto.Dispose(); Assert.Throws<ObjectDisposedException>(() => { crypto.CreateDecryptingTransform(); }); crypto.Dispose(); Assert.Throws<ObjectDisposedException>(() => { crypto.CreateDecryptingTransform(); }); }
public static void TestInvalidArguments() { AesIV iv = null; Assert.Throws<ArgumentNullException>(() => { iv = new AesIV(null); }); Assert.Throws<InternalErrorException>(() => { iv = new AesIV(new byte[0]); }); Assert.Throws<InternalErrorException>(() => { iv = new AesIV(new byte[32]); }); // Use the instance to avoid FxCop errors. Object.Equals(iv, null); }