填充无效,无法删除 使用“AesManaged”C# 解密字符串时出现异常

本文关键字:字符串 解密 异常 AesManaged 无效 删除 使用 填充 | 更新日期: 2023-09-27 18:33:16

请告诉我需要更新/重构代码以摆脱异常的地方。当我尝试使用以下代码解密加密字符串时,我遇到异常。

以下行引发异常

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;
}

填充无效,无法删除 使用“AesManaged”C# 解密字符串时出现异常

使用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();
    }

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

    swEncrypt.Write(plainText);
    swEncrypt.Flush();
    csEncrypt.FlushFinalBlock();
    encrypted = msEncrypt.ToArray();

csEncrypt.FlushFinalBlock()之前打电话给swEncrypt.Flush()解决了我的问题。