add support for aes-128 decryption #34

This commit is contained in:
Eliot Jones
2019-06-08 15:23:21 +01:00
parent a19122478d
commit 21a4ba597e
4 changed files with 80 additions and 2 deletions

View File

@@ -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));
}
}
}

View File

@@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using Geometry;
using Logging;
using Parser.Parts;
using Tokenization.Scanner;

View File

@@ -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();
}
}
}
}
}
}

View File

@@ -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);