Files
PdfPig/src/UglyToad.PdfPig/Encryption/AesEncryptionHelper.cs
Jason Nelson 1ef2e127a6 Improve Code Quality (#818)
* 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
2024-04-18 19:58:40 +01:00

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
}
}
}
}