У меня есть оболочка AES Cryptography и модульные тесты, которые работают уже более года. Теперь после установки VS 2012 (а может и обновления до .net Framework 4) юнит-тесты не проходят. Блок streamreader генерировал CryptographicException, когда я передал неверный проход, но не генерировал ArgumentNullException.
Код находится в. https://github.com/jnaus/Cryptography
Вот модульный тест, который сейчас не работает. (У BadSaltTest та же проблема)
[TestMethod]
[ExpectedException(typeof(CryptographicException),
"Bad password was inappropriately allowed")]
public void BadPasswordTest()
{
var cipherText = EncryptString();
var decryptedText = AESCryptography.DecryptStringAES
(cipherText,"A bad password", salt);
}
Результат теста: Метод теста CryptographyTest.AESTest.BadPasswordTest вызвал исключение System.ArgumentNullException, но ожидалось исключение System.Security.Cryptography.CryptographicException. Сообщение об исключении: System.ArgumentNullException: значение не может быть нулевым. Имя параметра: inputBuffer
Расшифровать код.
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;
}
EncryptString()в своем тестовом примере и посмотреть, возвращает ли онnull... - person Maarten Bodewes   schedule 22.09.2012