mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-23 04:36:44 +08:00
add support for aes-128 decryption #34
This commit is contained in:
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -2,7 +2,6 @@
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Geometry;
|
|
||||||
using Logging;
|
using Logging;
|
||||||
using Parser.Parts;
|
using Parser.Parts;
|
||||||
using Tokenization.Scanner;
|
using Tokenization.Scanner;
|
||||||
|
50
src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs
Normal file
50
src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -358,7 +358,7 @@
|
|||||||
|
|
||||||
if (useAes)
|
if (useAes)
|
||||||
{
|
{
|
||||||
throw new PdfDocumentEncryptedException("Decryption for AES-128 not currently supported.", encryptionDictionary);
|
return AesEncryptionHelper.Decrypt(data, finalKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
return RC4.Encrypt(finalKey, data);
|
return RC4.Encrypt(finalKey, data);
|
||||||
|
Reference in New Issue
Block a user