weixin_39533052
weixin_39533052
2021-01-06 04:26

256 bit AES Encryption doesn't work

256 bit AES encryption doesn't work, an error message of "System.ArgumentException: 'Specified initialization vector (IV) does not match the block size for this algorithm.'" is generated on line 87 of ZipAESTransform.cs during the creation of the encryptor. 128 bit AES encryption works as expected.

C#
_encryptor = rm.CreateEncryptor(byteKey1, byteKey2);

Steps to reproduce

Try and encrypt file with 256bit AES encryption

C#
class Program
{
    static void Main(string[] args)
    {
        ZipFile(@"C:\TEMP\test\test.txt", @"C:\TEMP\test\test.zip", "password");
    }

    public static void ZipFile(string srcPath, string outPath, string password)
    {
        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(outPath)))
        {
            zipStream.SetLevel(9); // 0-9, 9 being the highest level of compression

            // optional. Null is the same as not setting. Required if using AES.
            zipStream.Password = password; 

            ZipEntry zipEntry = new ZipEntry(Path.GetFileName(srcPath));

            // Specifying the AESKeySize triggers AES encryption. 
            // Allowable values are 0 (off), 128 or 256. 
            // A password on the ZipOutputStream is required if using AES.
            zipEntry.AESKeySize = 256; 
            zipEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(zipEntry);

            byte[] buffer = new byte[4096]; // Zip the file in buffered chunks

            using (FileStream fileStream = File.OpenRead(sourceFilePath))
            {
                // Using a fixed size buffer here makes no noticeable difference
                // for output but keeps a lid on memory usage.
                int sourceBytes = fileStream.Read(buffer, 0, buffer.Length);

                while (sourceBytes > 0)
                {
                    zipStream.Write(buffer, 0, sourceBytes);
                    sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
                }
            }
        }
    }
}

Expected behavior

Password protected zip file should be created.

Actual behavior

Fails to encrypt zip file.

Version of SharpZipLib

0.86.0.518

Obtained from (place an x between the brackets for all that apply)

  • Compiled from source
  • branch: master
  • commit: be53259

该提问来源于开源项目:icsharpcode/SharpZipLib

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

15条回答