简体   繁体   English

使用AES256 C#解密字节数组时出错

[英]Error while decrypting a bytes array using AES256 C#

I was doing a program for encrypting private files on another computer apart, where this same formula worked correctly, I don't understand why this part of code doesn't work, even though it worked before: 我当时正在编写一个程序,用于加密另一台计算机上的私人文件,在该计算机上,相同的公式正常工作,我不理解为什么这部分代码不起作用,即使以前也可以起作用:

public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
    byte[] decryptedBytes = null;

    byte[] saltBytes = new byte[] { 2, 0, 0, 4, 0, 3, 0, 3 };

    using (MemoryStream ms = new MemoryStream())
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 256;
            AES.Padding = PaddingMode.PKCS7;
            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);

            AES.Mode = CipherMode.CFB;

            using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                cs.Close();
            }

            decryptedBytes = ms.ToArray();
        }
    }

    return decryptedBytes;
}

错误样本

I've been trying to find out for about 2 hours because it gives me that error when decrypting, but counting on the little encryption knowledge that I have have it's absolutely impossible. 我一直试图找出大约2个小时,因为它在解密时会给我这个错误,但是依靠我所拥有的一点加密知识绝对是不可能的。

First of all, when i say ChunkSize i mean ChunkSize, because first of all I split the file into 5Mb bytes array, then i encrypt or decrypt the Byte Array and write the data into the file. 首先,当我说ChunkSize时,我的意思是ChunkSize,因为首先将文件拆分为5Mb字节数组,然后对字节数组进行加密或解密,然后将数据写入文件中。 Problem was, that ChunkSize was like less than 1Mb and thats why CryptographicException arises. 问题是,ChunkSize小于1Mb,这就是为什么CryptographicException出现的原因。


                if (File.Exists(FileToDecrypt)) {
                    byte[] PasswordBytes;
                    if (IsFileTypePassword) {
                        PasswordBytes = File.ReadAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"K3ys\\" + PasswordToDecrypt));
                    } else {
                        PasswordBytes = Encoding.UTF8.GetBytes(PasswordToDecrypt);
                    }
                    DecryptFile(5000000, FileToDecrypt, PasswordBytes); // Here was the problem
                    DecThread.Abort();
                }

And here is the function that splits the file, its a little bit different than mine but it does the same, also this piece of code is grabbed from another Question form StackOverflow. 这是分割文件的函数,它与我的文件有些许不同,但功能相同,这部分代码也是从另一个Question窗体StackOverflow抓取的。

public static void SplitFile(string inputFile, int chunkSize, string path)
{
    byte[] buffer = new byte[chunkSize];

    using (Stream input = File.OpenRead(inputFile))
    {
        int index = 0;
        while (input.Position < input.Length)
        {
            using (Stream output = File.Create(path + "\\" + index))
            {
                int chunkBytesRead = 0;
                while (chunkBytesRead < chunkSize)
                {
                    int bytesRead = input.Read(buffer, 
                                               chunkBytesRead, 
                                               chunkSize - chunkBytesRead);

                    if (bytesRead == 0)
                    {
                        break;
                    }
                    chunkBytesRead += bytesRead;
                }
                output.Write(buffer, 0, chunkBytesRead);
            }
            index++;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM