C#:AES 错误:填充无效且无法删除。相同的 key 和一切,帮助

标签 c# aes padding

我是 C# 的新手,所以请耐心等待。我知道这个问题被问了很多次,但我找不到问题的答案。

我正在保存一些数据,在将其写入文件之前,我将其转换为二进制文件并将其存储在数组中,我对其进行加密,然后写入文件。我以 block (32 字节)的形式加密数据。以同样的方式,我以 32 字节的 block 读取数据,然后解密该数据,然后这应该重复直到文件末尾。但是当涉及到解密时抛出以下错误:

Padding is invalid and cannot be removed.

我使用相同的 key 和 iv(硬编码直到我开始工作)

这是我的加密代码,可以正常工作:

        //result
        byte[] data = new byte[32];

        //setup encryption (AES)
        SymmetricAlgorithm aes = Aes.Create();
        byte[] key = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9,50};
        byte[] iv = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
        ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);

        FileStream fStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 1024, false);

        //prepare data to write (byte array 'data') ...

        //encrypt
               MemoryStream m = new MemoryStream();
               using (Stream c = new CryptoStream(m, encryptor, CryptoStreamMode.Write))
                   c.Write(data, 0, data.Length);
               data = m.ToArray();
               fStream.Write(data, 0, data.Length);

这是我的解密代码:

FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);

            //setup encryption (AES)
            SymmetricAlgorithm aes = Aes.Create();
            byte[] key = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9, 50 };
            byte[] iv = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
            ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);

            //result
            byte[] data = new byte[32];

            //loop for reading the whole file ...
            int len = fStream.Read(data, 0, 32);

            //decrypt
                MemoryStream m = new MemoryStream();
                using (Stream c = new CryptoStream(m, decryptor, CryptoStreamMode.Write))
                    c.Write(data, 0, data.Length); //The exception is thrown in this line                  
                data = m.ToArray();

                //using the decrypted data and then looping back to reading and decrypting...

我尝试了所有我能想到的(这并不多,因为我对密码学很陌生),我到处搜索,但找不到解决我问题的方法。我还帮助自己阅读了C# in a Nutshell这本书。

如果有人知道为什么会发生这种情况,我将非常感激,因为我没有任何想法。

感谢您的宝贵时间和回答。

编辑: 看起来加密数据的大小是 48 字节(比原来多了 12 字节)。为什么?我认为它只添加字节,如果它们不是 block 大小的倍数(16 字节,我的数据是 32 字节)。数据是否总是更大,并且不断增加(我需要知道这一点才能正确读取和解密)。

注意:我不能直接使用其他流,因为我需要控制输出格式,我相信在内存中加密也更安全、更快速。

最佳答案

基于您的编辑:

EDIT: It seems that the size of the encrypted data is 48 bytes (12 bytes more than the original). Why is that so? I thought that it only adds bytes if they are not a multiple of the block size (16 bytes, my data is 32 bytes). Is data always larger, and with constant increase (I need to know that in order to properly read and decrypt).

如果加密数据为 48 字节,则比原始数组大 16 字节。这是有道理的,因为算法会填充数据,因为默认值为 PKCS7 (即使大小与 block 大小匹配,因为它填充到 block 大小的 next 倍数)。如果你想保持它恰好是 32 个字节,只需更改 PaddingNone

aes.Padding = PaddingMode.None;

关于C#:AES 错误:填充无效且无法删除。相同的 key 和一切,帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5080267/

相关文章:

c# - 转换日期列表以便在包含连续日期的情况下按组显示的最佳方法是什么?

c# - 我如何预先设置流?

c# - 在 C# 中捕获 MySQL 触发信号

css - 如何删除相邻链接旁边的填充?

c# - 具有异常处理的方法参数

c# - 我可以将 2 个加密字符串加在一起并解密吗?

c++ - 无法使用 Intel AES-NI 示例库以 32 字节 block 大小进行加密/解密

c# - 一个新的随机初始化向量在加密过程中总是产生相同的密文

html - div中文本的奇怪填充/边距

html - 文本右对齐和右填充