diff --git a/src/UglyToad.PdfPig.Tests/Encryption/AesEncryptionHelperTests.cs b/src/UglyToad.PdfPig.Tests/Encryption/AesEncryptionHelperTests.cs new file mode 100644 index 00000000..f4d45f45 --- /dev/null +++ b/src/UglyToad.PdfPig.Tests/Encryption/AesEncryptionHelperTests.cs @@ -0,0 +1,29 @@ +using Xunit; + +namespace UglyToad.PdfPig.Tests.Encryption +{ + using PdfPig.Encryption; + using PdfPig.Util; + + public class AesEncryptionHelperTests + { + [Fact] + public void CanDecryptDateString() + { + var key = new byte[] + { + 54, 109, 249, 186, 109, 210, 209, 44, 94, 28, 227, 232, 73, 86, 128, 186 + }; + + var data = new byte[] + { + 123, 28, 227, 85, 79, 126, 149, 28, 211, 96, 199, 192, 105, 149, 76, 231, + 8, 136, 51, 141, 139, 44, 0, 230, 228, 116, 12, 145, 132, 157, 5, 123, 235, 247, 232, 244, 36, 217, 73, 147, 157, 124, 27, 143, 255, 79, 220, 194 + }; + + var output = AesEncryptionHelper.Decrypt(data, key); + + Assert.Equal("D:20180808103317-07'00'", OtherEncodings.BytesAsLatin1String(output)); + } + } +} diff --git a/src/UglyToad.PdfPig/Content/Pages.cs b/src/UglyToad.PdfPig/Content/Pages.cs index b1de775b..fbf09362 100644 --- a/src/UglyToad.PdfPig/Content/Pages.cs +++ b/src/UglyToad.PdfPig/Content/Pages.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using Geometry; using Logging; using Parser.Parts; using Tokenization.Scanner; diff --git a/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs b/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs new file mode 100644 index 00000000..86399417 --- /dev/null +++ b/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs @@ -0,0 +1,50 @@ +namespace UglyToad.PdfPig.Encryption +{ + using System; + using System.IO; + using System.Security.Cryptography; + + internal static class AesEncryptionHelper + { + public static byte[] Encrypt256() + { + throw new NotImplementedException(); + } + + public static byte[] Decrypt(byte[] data, byte[] finalKey) + { + var iv = new byte[16]; + Array.Copy(data, iv, iv.Length); + + using (var rijndael = Rijndael.Create()) + { + rijndael.Key = finalKey; + rijndael.IV = iv; + + var buffer = new byte[256]; + + using (var decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV)) + using (var input = new MemoryStream(data)) + using (var output = new MemoryStream()) + { + input.Seek(iv.Length, SeekOrigin.Begin); + using (var cryptoStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) + { + int read; + while ((read = cryptoStream.Read(buffer, 0, buffer.Length)) != -1) + { + output.Write(buffer, 0, read); + + if (read < buffer.Length) + { + break; + } + } + + return output.ToArray(); + } + } + } + } + } +} diff --git a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs index 180586d9..93019bad 100644 --- a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs +++ b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs @@ -358,7 +358,7 @@ if (useAes) { - throw new PdfDocumentEncryptedException("Decryption for AES-128 not currently supported.", encryptionDictionary); + return AesEncryptionHelper.Decrypt(data, finalKey); } return RC4.Encrypt(finalKey, data);