mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
* Make AdobeFontMetricsLigature a struct * Make AdobeFontMetricsCharacterSize a struct * Eliminate allocation in CompactFontFormatData * Pass TransformationMatrix by reference * Seal Encoding classes * Make SubTableHeaderEntry a readonly struct * Introduce StringSplitter and eliminate various allocations in GlyphListFactory * Eliminate a few substring allocations * Use char overload on StringBuilder * Eliminate virtual calls on stringIndex * Optimize ReadHelper ReadLong and ReadInt methods * Add additional readonly annotations to PdfRectangle * Optimize NameTokenizer * Eliminate allocation in TrueTypeGlyphTableSubsetter * Use empty arrays * Eliminate allocations in OperationWriteHelper.WriteHex * Use simplified DecryptCbc method on .NET 6+ * Fix windows-1252 encoding not working on net6.0 and 8.0 * Update int buffers to exact unsigned max length and eliminate additional byte allocation * Fix typo * Remove unused constant
60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
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)
|
|
{
|
|
if (data.Length == 0)
|
|
{
|
|
return data;
|
|
}
|
|
|
|
var iv = new byte[16];
|
|
Array.Copy(data, iv, iv.Length);
|
|
|
|
using (var aes = Aes.Create())
|
|
{
|
|
aes.Key = finalKey;
|
|
aes.IV = iv;
|
|
|
|
#if NET8_0_OR_GREATER
|
|
return aes.DecryptCbc(data.AsSpan(iv.Length), iv, PaddingMode.PKCS7);
|
|
#else
|
|
var buffer = new byte[256];
|
|
|
|
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.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;
|
|
do
|
|
{
|
|
read = cryptoStream.Read(buffer, 0, buffer.Length);
|
|
|
|
if (read > 0)
|
|
{
|
|
output.Write(buffer, 0, read);
|
|
}
|
|
} while (read > 0);
|
|
|
|
return output.ToArray();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
}
|