PDFium Delphi Component Docs

FPdfAes 单元

单元: FPdfAes
纯 Pascal 实现的 AES-128 与 AES-256 分组密码,包含密钥扩展、单分组加解密,以及 PDF 加密处理器使用的 AES-256-CBC 流包装器

语法

TAesBlock = array[0..15] of Byte;
TAes128Key = array[0..15] of Byte;
TAes128ExpandedKey = array[0..175] of Byte;
TAes256Key = array[0..31] of Byte;
TAes256ExpandedKey = array[0..239] of Byte;

procedure Aes128KeyExpansion(const Key: TAes128Key; var Expanded: TAes128ExpandedKey);
procedure Aes128EncryptBlock(const Expanded: TAes128ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
procedure Aes256KeyExpansion(const Key: TAes256Key; var Expanded: TAes256ExpandedKey);
procedure Aes256EncryptBlock(const Expanded: TAes256ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
procedure Aes256DecryptBlock(const Expanded: TAes256ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
function Aes256CbcEncryptPKCS7(const Key: TAes256Key; const IV: TAesBlock;
  const Plain: TBytes): TBytes;
function Aes256CbcEncryptNoPad(const Key: TAes256Key; const IV: TAesBlock;
  const Plain: TBytes): TBytes;
function Aes256CbcDecryptPKCS7(const Key: TAes256Key; const IV: TAesBlock;
  const Cipher: TBytes): TBytes;
function Aes256CbcDecryptNoPad(const Key: TAes256Key; const IV: TAesBlock;
  const Cipher: TBytes): TBytes;
function Aes256KeyWrap(const KeyEncryptionKey: TAes256Key;
  const PlainKeyData: TBytes): TBytes;
function Aes256KeyUnwrap(const KeyEncryptionKey: TAes256Key; const WrappedKeyData: TBytes;
  out PlainKeyData: TBytes): Boolean;
procedure AesGenerateRandomBytes(Buffer: Pointer; Count: Integer);

说明

FPdfAes 是一个无依赖的 AES 实现,使 PDFium Delphi Component 可以在 Delphi、C++Builder 和 Free Pascal 上加解密 PDF 流,而无需依赖外部加密库。它被 FPdfEncrypt 中的 PDF AES-256 安全处理器以及 FPdfAesGcm 中的 GCM 模式所使用

分组原语通过扩展的轮密钥对固定 16 字节的分组进行运算。CBC 包装器在可变长度的字节数组上处理 PKCS#7 填充或无填充,对应 PDF 规范要求的两种流加密模式

AesGenerateRandomBytes 用加密随机的字节填充缓冲区,用于 IV、盐值和密钥

函数

FunctionDescription
Aes128KeyExpansion将 128 位密钥扩展为 176 字节的轮密钥调度
Aes128EncryptBlock用扩展的 AES-128 密钥加密一个 16 字节的分组
Aes256KeyExpansion将 256 位密钥扩展为 240 字节的轮密钥调度
Aes256EncryptBlock用扩展的 AES-256 密钥加密一个 16 字节的分组
Aes256DecryptBlock用扩展的 AES-256 密钥解密一个 16 字节的分组
Aes256CbcEncryptPKCS7在 CBC 模式下使用 PKCS#7 填充加密字节数组
Aes256CbcEncryptNoPad在 CBC 模式下不加填充地加密字节数组(输入长度必须是分组的整数倍)
Aes256CbcDecryptPKCS7解密 CBC 密文并去除 PKCS#7 填充
Aes256CbcDecryptNoPad解密 CBC 密文但不去除填充
Aes256KeyWrap使用 RFC 3394 完整性保护对密钥数据执行 AES-256 包装
Aes256KeyUnwrap验证并解包 RFC 3394 密钥数据,完整性校验失败时不返回任何明文
AesGenerateRandomBytes用加密随机的字节填充缓冲区

类型

类型说明
TAesBlock一个 16 字节的 AES 数据分组,既是单分组原语和 CBC 链接步骤的操作单元,也是 CBC 包装函数的 IV 类型
TAes128Key16 字节的 AES-128 密钥,供 Aes128KeyExpansion 接受
TAes128ExpandedKey176 字节的 AES-128 轮密钥调度,由 Aes128KeyExpansion 生成,供 Aes128EncryptBlock 消费
TAes256Key32 字节的 AES-256 密钥,供 Aes256KeyExpansion、CBC 包装函数和 RFC 3394 密钥包装函数接受
TAes256ExpandedKey240 字节的 AES-256 轮密钥调度,覆盖全部 14 轮,由 Aes256KeyExpansion 生成,供分组加解密例程和 CBC 例程消费

常量

ConstantValueDescription
AES_BLOCK_SIZE16AES 分组大小(字节)
AES128_KEY_SIZE16AES-128 密钥大小(字节)
AES128_ROUND_KEY_SIZE176扩展后的 AES-128 轮密钥大小(字节)
AES256_KEY_SIZE32AES-256 密钥大小(字节)
AES256_ROUND_KEY_SIZE240扩展后的 AES-256 轮密钥大小(字节)

备注

参见

FPdfAesGcm, FPdfEncrypt