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、salt 與金鑰

函式

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以 AES-256 與 RFC 3394 完整性保護包裝金鑰資料
Aes256KeyUnwrap驗證並解開 RFC 3394 金鑰資料,完整性失敗時不傳回明文
AesGenerateRandomBytes以密碼學隨機位元組填滿緩衝區

類型

TypeDescription
TAesBlock一個 16 位元組的 AES 資料區塊,是單區塊基本操作與 CBC 鏈接步驟的處理單位,同時也是 CBC 包裝函式的 IV 型別
TAes128KeyAes128KeyExpansion 接受的 16 位元組 AES-128 金鑰
TAes128ExpandedKey由 Aes128KeyExpansion 產生、並交給 Aes128EncryptBlock 使用的 176 位元組 AES-128 回合金鑰排程
TAes256KeyAes256KeyExpansion、CBC 包裝函式與 RFC 3394 金鑰包裝函式接受的 32 位元組 AES-256 金鑰
TAes256ExpandedKey涵蓋全部 14 個回合的 240 位元組 AES-256 回合金鑰排程,由 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