首页 / C# / c# – AES解密异常更改
c# – AES解密异常更改
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – AES解密异常更改,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2172字,纯文字阅读大概需要4分钟。
内容图文
我有一个AES加密包装和单元测试,已经工作了一年多.现在安装VS 2012(或者可能是对.net Framework 4的更新)后,单元测试没有通过.当我传入一个错误的传递但是没有抛出ArgumentNullException时,streamreader块抛出了CryptographicException.
代码到了. 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:值不能为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;
}
解决方法:
在搜索了一下之后,这确实看起来确实是微软所诟病的一个错误(见Microsoft Connect).个人不是建议的解决方法的忠实粉丝,因为它确实不应该被需要,但它会在我想的同时做.
内容总结
以上是互联网集市为您收集整理的c# – AES解密异常更改全部内容,希望文章能够帮你解决c# – AES解密异常更改所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。