C#AES - 填充无效,无法删除 [英] C# AES - Padding is Invalid and cannot be removed

查看:3693
本文介绍了C#AES - 填充无效,无法删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题几天。我已经阅读所有的帖子有关这个填充问题 - 这可能是由一个不正确的密钥(可能是这种情况下,但我没有看到它。)

I have been struggling with this problem for several days now. I have read all the posts out there about this padding issue - which can often be caused by an incorrect key (possibly the case here - but I'm not seeing it.

代码下面:

internal class AESEncryptionManager
{
    private byte[] keyBytes { get; set; }

    private byte[] ivBytes { get; set; }

    private static readonly byte[] SALT = new byte[]
        {0x26, 0xdc, 0xff, 0x12, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x02, 0xaf, 0x4d, 0x08, 0x22, 0x3c};

    private Rfc2898DeriveBytes keyDerivationFunction { get; set; }
    private AesManaged aesManaged;

    public AESEncryptionManager(string key)
    {   
        aesManaged = new AesManaged();
        aesManaged.Padding = PaddingMode.PKCS7;
        keyDerivationFunction = new Rfc2898DeriveBytes(key, SALT);

        aesManaged.KeySize = 256;
        aesManaged.BlockSize = 128;

        byte[] newKey = keyDerivationFunction.GetBytes(aesManaged.KeySize >> 3);
        byte[] newIv = keyDerivationFunction.GetBytes(aesManaged.BlockSize >> 3);

        keyBytes = newKey;
        ivBytes = newIv;

        aesManaged.Key = keyBytes;
        aesManaged.IV = ivBytes;

    }

    public byte[] EncryptToBytes(byte[] message)
    {
        ICryptoTransform encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes);

        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                csEncrypt.Write(message, 0, message.Length);
                csEncrypt.Close();
                return msEncrypt.ToArray();
            }
        }
    }

    public byte[] DecryptToBytes(byte[] message)
    {
        byte[] newKey = keyDerivationFunction.GetBytes(aesManaged.KeySize >> 3);
        byte[] newIv = keyDerivationFunction.GetBytes(aesManaged.BlockSize >> 3);

        ICryptoTransform decryptor = aesManaged.CreateDecryptor(newKey, newIv);

        using (MemoryStream msDecrypt = new MemoryStream())
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
            {
                csDecrypt.Write(message, 0, message.Length);
                csDecrypt.Close();
                return msDecrypt.ToArray();
            }
        }
    }

像清除流等。任何帮助尚未提供在MSDN或堆栈溢出将是有帮助的。

I have tried the usual things like flushing the stream, etc. Any help not already provided on MSDN or Stack Overflow would be helpful.

推荐答案

问题是 DecryptToBytes()

byte[] newKey = keyDerivationFunction.GetBytes(aesManaged.KeySize >> 3);
byte[] newIv = keyDerivationFunction.GetBytes(aesManaged.BlockSize >> 3);

ICryptoTransform decryptor = aesManaged.CreateDecryptor(newKey, newIv);

您使用与加密不同的密钥和初始化向量来创建解密器;您正在从用于导出密钥和初始化向量以进行加密的相同密钥导出函数请求新字节。因为加密和解密密钥不匹配解密产生损坏的数据,特别是损坏的填充。将以下三行替换为下面的一行,它将工作。

You are creating the decryptor with a different key and initialization vector than you used for encryption; you are requesting new bytes from the same key derivation function you used for deriving the key and initialization vector for encryption. Because encryption and decryption keys don't match decryption yields corrupted data and especially corrupted padding. Replace the three lines with the following one and it will work.

ICryptoTransform decryptor = aesManaged.CreateDecryptor();

注意,我没有看到任何更接近的代码,它会工作 bug会被解决,它并不意味着其他部分的实现也是好的。

Note that I did not look any closer at the code and "it will work" only means that this bug will be resolved, it does not imply that other parts of the implementation are okay, too.

这篇关于C#AES - 填充无效,无法删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆