C# AES解密异常更改
我有一个AES密码包装器和单元测试,已经工作了一年多。现在,在安装VS 2012(或者可能是对.net Framework 4的更新)之后,单元测试没有通过。当我在错误传递中传递时,streamreader块正在引发CryptographicException,但没有引发ArgumentNullException 密码在C# AES解密异常更改,c#,.net,c#-4.0,cryptography,streamreader,C#,.net,C# 4.0,Cryptography,Streamreader,我有一个AES密码包装器和单元测试,已经工作了一年多。现在,在安装VS 2012(或者可能是对.net Framework 4的更新)之后,单元测试没有通过。当我在错误传递中传递时,streamreader块正在引发CryptographicException,但没有引发ArgumentNullException 密码在 下面是现在不起作用的单元测试。(BadsoltTest也有同样的问题) 测试结果: 测试方法CryptographyTest.AESTest.BadPasswordTest引
下面是现在不起作用的单元测试。(BadsoltTest也有同样的问题) 测试结果: 测试方法CryptographyTest.AESTest.BadPasswordTest引发异常System.ArgumentNullException,但应为异常System.Security.Cryptography.CryptographyException。异常消息:System.ArgumentNullException:值不能为null。 参数名称: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;
}
你上面发布的代码对我有用(我必须编写一些代码来创建要传递的加密字符串)。使用.Net Framework 4在VS2012中编译并运行 我使用的加密代码是:
private static string EncryptStringAES(string plainText, string password, byte[] salt)
{
RijndaelManaged aesAlg = null;
string cypherText = 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 an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
byte[] bytes = new UTF8Encoding().GetBytes(plainText);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(bytes, 0, bytes.Length);
csEncrypt.FlushFinalBlock();
cypherText = Convert.ToBase64String(msEncrypt.ToArray());
}
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
{
aesAlg.Clear();
}
}
return cypherText;
}
使用以下命令进行方法调用:
byte[] salt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
string test = DecryptStringAES(EncryptStringAES("This is a test", "Test", salt), "Test", salt);
结果字符串(Test)包含“这是一个测试”。在搜索了一段时间后,这确实是一个由Microsoft确认的错误(请参阅)。就个人而言,我不太喜欢建议的解决方案,因为它确实不应该被需要,但我想它会在同一时间完成。这令人困惑。你的意思是“当我传递错误的传递时,streamreader块抛出了一个CryptographicException,但现在抛出了ArgumentNullException。”你确定是streamreader块抛出了异常吗?我想你应该好好看看
EncryptString()
方法,并查看该方法是否返回null
…是的,我的单元测试BadPasswordTest预期会出现加密异常,直到最近才开始工作。不是,它返回了一个异常。请参见上面的测试结果。EncryptString()用于所有单元测试,并且工作正常。我要补充的是,自2011年11月6日以来,它一直在工作,没有改变。查看Github My code works这只是单元测试的两个部分不像今年早些时候那样工作。我在一个单元测试中运行了上述代码(我已经设置了一个),只添加了两个调用行,并从测试资源管理器中调试了所选测试。ArgumentNullException是您指定的位置,就是说csDecrypt在那个点上是空的。您需要逐步检查代码并确定加密流无法创建的原因。好的,不确定您的测试之前为什么工作,但我做了一些挖掘。。。当我将错误密码传递给decrypt方法时,会引发(嵌套)两个异常内部异常是一个ArgumentNullException,该异常在处理CryptoStream时发生(调用System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock时inputBuffer为null)。外部异常是由System.Security.Cryptography.RijndaelManagedTransform.DecryptData引发的CryptographyException“填充无效且无法删除”。是的,这也是我看到的2个异常。有些机器返回CryptException,有些机器返回Null,因此似乎有一些.NET框架更改。所有获得NULLException的机器现在都有VS 2012版本,看起来有一个.net 4的补丁。
byte[] salt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
string test = DecryptStringAES(EncryptStringAES("This is a test", "Test", salt), "Test", salt);