ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – AES解密异常更改

2019-06-30 00:53:58  阅读:375  来源: 互联网

标签:c net cryptography c-4-0 streamreader


我有一个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,net,cryptography,c-4-0,streamreader
来源: https://codeday.me/bug/20190630/1331554.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有