C#:AES错误:填充无效,无法删除.相同的键和所有的东西,帮助 [英] C#: AES error: Padding is invalid and cannot be removed. Same key and everything, help

查看:151
本文介绍了C#:AES错误:填充无效,无法删除.相同的键和所有的东西,帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是很陌生,所以请耐心等待我.我知道这个问题经常被问到,但是我找不到解决问题的答案.

I'm quite new to C# so please be patient with me. I know this question was asked a lot if times, but I couldn't find an answer to my problem.

我正在保存一些数据,然后将其写入文件中,然后将其转换为二进制文件并将其存储在数组中,然后对其进行加密,然后再写入文件中.我对数据块(32字节)进行加密.以相同的方式,我读取32字节的数据块,然后解密该数据,然后应重复此操作直到文件末尾.但是当涉及到解密时,会引发以下错误:

I'm saving some data and before writing it to a file I convert it to binary and store it in array, which I encrypt and then write to file. I encrypt data in chunks (32 bytes). In the same way I read data in chunks of 32 bytes and then decrypt that data and then this should repeat till the end of file. But when it comes to decryption the following error is thrown:

填充无效,无法删除.

我使用相同的密钥和iv(直到我开始工作之前对其进行硬编码)

I use the same key and iv (hardcoded just until I get it working)

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

Here is my encryption code, which works without problems:

        //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#简而言之

I tried all I could think of (which is not much because I'm very new to cryptography), I searched everywhere and I couldn't find a solution to my problem. I also helped myself with the book C# in a Nutshell .

如果有人对为什么会发生有任何想法,我将非常感谢,因为我没有想法.

If anyone has ideas on why this could happen I'll be really thankful because I have no ideas.

感谢您的时间和答复.

似乎加密数据的大小为48个字节(比原始数据多12个字节).为什么?我认为,仅当字节不是块大小的倍数时才添加字节(16字节,我的数据是32字节).数据是否总是更大且不断增加(我需要知道以便正确读取和解密).

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).

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

Note: I can't directly use other streams because I need to have control over the output format and I believe it is also safer and faster to encrypt in memory.

推荐答案

基于您的修改:

似乎加密数据的大小为48个字节(比原始数据多12个字节).为什么?我认为,仅当字节不是块大小的倍数时才添加字节(16字节,我的数据是32字节).数据是否总是更大且不断增加(我需要知道以便正确读取和解密).

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 (即使大小与块大小匹配,因为它会填充到块大小的 next 倍数).如果希望将其精确保留为32个字节,只需更改

If the encrypted data is 48 bytes, thats 16 bytes larger than your original array. This makes sense because the algorithm with pad the data because the default is PKCS7 (even if the size matches the block size, because it pads to the next multiple of the block-size). If you wish to keep it exactly 32 bytes, just change the Padding to None

aes.Padding = PaddingMode.None;

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

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