From 3ab0883cf3b9e73bbc490ff0de28a8f4499fb5b9 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sun, 3 Apr 2022 15:20:42 -0400 Subject: [PATCH] fix bug with handling of more than 256 bytes --- .../Encryption/AesEncryptionHelper.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs b/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs index 7f4a246d..f572a25d 100644 --- a/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs +++ b/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs @@ -1,6 +1,7 @@ namespace UglyToad.PdfPig.Encryption { using System; + using System.Diagnostics; using System.IO; using System.Security.Cryptography; @@ -20,7 +21,7 @@ var iv = new byte[16]; Array.Copy(data, iv, iv.Length); - + using (var rijndael = Rijndael.Create()) { rijndael.Key = finalKey; @@ -28,6 +29,11 @@ var buffer = new byte[256]; + if (data.Length > 256) + { + Debugger.Break(); + } + using (var decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV)) using (var input = new MemoryStream(data)) using (var output = new MemoryStream()) @@ -35,15 +41,15 @@ input.Seek(iv.Length, SeekOrigin.Begin); using (var cryptoStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) { - var offset = 0; int read; do { - read = cryptoStream.Read(buffer, offset, buffer.Length - offset); + read = cryptoStream.Read(buffer, 0, buffer.Length); - output.Write(buffer, offset, read); - - offset += read; + if (read > 0) + { + output.Write(buffer, 0, read); + } } while (read > 0); return output.ToArray();