【问题标题】:Padding is invalid and cannot be removed Exception while decrypting string using "AesManaged" C#使用“AesManaged”C# 解密字符串时,填充无效且无法删除异常
【发布时间】:2012-05-06 10:25:24
【问题描述】:

请建议我在哪里需要更新/重构代码以消除异常。当我尝试使用以下代码解密加密字符串时遇到异常。

下面一行是抛出异常

using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
     // Read the decrypted bytes from the decrypting stream
     // and place them in a string.
     plaintext = srDecrypt.ReadToEnd();
}

public string EncryptAuthenticationTokenAes(string plainText)
{

    byte[] encrypted;
    // Create an AesManaged object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
        aesAlg.Padding = PaddingMode.None;
        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }


    // Return the encrypted bytes from the memory stream.
    return Convert.ToBase64String(encrypted);

}

public string DecryptPasswordAes(string encryptedString)
{
    //Convert cipher text back to byte array
    byte[] cipherText = Convert.FromBase64String(encryptedString);

    // Declare the string used to hold
    // the decrypted text.
    string plaintext = null;

    // Create an AesManaged object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        // Create a decrytor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
        aesAlg.Padding = PaddingMode.None;
        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {

                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }

    }

    return plaintext;

}

【问题讨论】:

  • 我发现实际上调用加密和解密方法的问题,因为两者都使用不同的KeyIV。当我通过相同的 KeyIV 问题得到解决。

标签: c# c#-4.0 c#-3.0 aes


【解决方案1】:

使用 CryptoStream 时的标准错误,您忘记强制它加密流的最后一个字节。它将字节保存在内部缓冲区中,直到有足够的字节到达以发出块。您必须强制输出最后几个字节。修复:

    using (var msEncrypt = new MemoryStream())
    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
    using (var swEncrypt = new StreamWriter(csEncrypt)) {
        swEncrypt.Write(plainText);
        csEncrypt.FlushFinalBlock();
        encrypted = msEncrypt.ToArray();
    }

解密时出现异常,因为 encrypted 缺少最终填充。真正的问题是由 using 语句引起的,如果您等到 CryptoStream 关​​闭后才获取加密字节,则不会出现此问题。但这并不能很好地工作,因为 StreamWriter 上的 using 语句也会关闭 CryptoStream 和 MemoryStream。显式使用 FlushFinalBlock() 是最好的解决方法。

【讨论】:

  • 由于某种原因,这种方法在 C# 4.0/4.5 中对我不起作用 - 必须在每个 using( ) 之后放置花括号。比不需要.FlushFinalBlock( ) 呼叫。见stackoverflow.com/questions/29809687/#29835039
  • @Astrogator:这没有任何意义,也不是源/修复。卷曲是可选的,不会(在这种情况下)改变语义。
  • @EdS。不是 this 的原因,而是检查 my linked 问题。我还认为卷发 {} 是不必要的 - 并且被它咬了。它们可能不会改变语义,但它们确实会影响执行顺序,因为恢复它们修复了我的代码。这就是我想要警告的全部内容。
  • @Astrogator:哦,当然;你把一行移到一个完全不同的范围。
  • @EdS。终结器(和 => 对象闭包)的执行顺序受卷曲 {} 的存在或不存在影响,这在我的问题中很重要。我问了,自己回答了,分享了我的经验,并警告了其他偶然发现这个问题的人。4 年后,你刚刚来到这个问题,反对我的评论,然后否决了我的答案不酷,伙计!
猜你喜欢
  • 1970-01-01
  • 2011-12-25
  • 2021-03-15
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
相关资源
最近更新 更多