I have an AES Cryptography wrapper and unit tests that has been working for over a year. Now after installing VS 2012 (or maybe an update to .net Framework 4) the unit tests do not pass. The streamreader block was throwing a CryptographicException when I passed in a bad pass but is not throwing a ArgumentNullException.
The code is up at. https://github.com/jnaus/Cryptography
Here is the unit test that now does not work. (BadSaltTest has the same problem)
[TestMethod]
[ExpectedException(typeof(CryptographicException),
"Bad password was inappropriately allowed")]
public void BadPasswordTest()
{
var cipherText = EncryptString();
var decryptedText = AESCryptography.DecryptStringAES
(cipherText,"A bad password", salt);
}
Test Result: Test method CryptographyTest.AESTest.BadPasswordTest threw exception System.ArgumentNullException, but exception System.Security.Cryptography.CryptographicException was expected. Exception message: System.ArgumentNullException: Value cannot be null. Parameter name: inputBuffer
Decrypt code.
public static string DecryptStringAES(string cipherText,
string password, byte[] salt)
{
RijndaelManaged aesAlg = null;
string plaintext = null;
try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize/8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize/8);
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key,
aesAlg.IV);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
//StreamReader now gives ArgumentNullException
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
{
aesAlg.Clear();
}
}
return plaintext;
}