PDFium Delphi Component Docs

FPdfEncrypt 單元

單元: FPdfEncrypt
衍生 AES-256 加密金鑰,並產出 ISO 32000-2 AESV3-CBC 或 ISO/TS 32003 AESV4-GCM 的字串、串流與加密字典

語法

TPdfEncryptionRevision = (erR5, erR6, erR7, erR2, erR3, erR4);
TPdfLegacyCryptMethod = (lcmRc4, lcmAesV2);

TPdfEncryptOptions = record
  UserPassword: AnsiString;
  OwnerPassword: AnsiString;
  Permissions: Cardinal;
  EncryptMetadata: Boolean;
  Revision: TPdfEncryptionRevision;
  EnableIntegrityProtection: Boolean;
  class function Default: TPdfEncryptOptions; static;
end;

TPdfEncryptionKeys = record
  FileEncryptionKey: TBytes;
  OEntry: TBytes;
  UEntry: TBytes;
  OEEntry: TBytes;
  UEEntry: TBytes;
  PermsEntry: TBytes;
  KdfSalt: TBytes;
end;

TPdfIncrementalEncryptionContext = record
  FileEncryptionKey: TBytes;
  KdfSalt: TBytes;
  Revision: TPdfEncryptionRevision;
  EncryptMetadata: Boolean;
  EncryptObjectNumber: Integer;
  EncryptGeneration: Integer;
  HasAuthenticationCode: Boolean;
end;

procedure DeriveEncryptionKeys(const Options: TPdfEncryptOptions;
  out Keys: TPdfEncryptionKeys);
function BuildEncryptDictionary(const Keys: TPdfEncryptionKeys;
  const Options: TPdfEncryptOptions): AnsiString;
function EncryptStreamPayload(const FileKey: TBytes;
  const Plain: TBytes): TBytes;
function EncryptDocumentBytes(const PlainPdf: TBytes;
  const Options: TPdfEncryptOptions): TBytes;
function DecryptAesGcmDocumentBytes(const EncryptedPdf: TBytes;
  const Password: AnsiString; out PlainPdf: TBytes;
  out ErrorText: string): Boolean;
function ValidateEncryptedDocumentMac(const EncryptedPdf: TBytes;
  const Password: AnsiString): TPdfMacValidationResult;
function ReadIncrementalEncryptionContext(const EncryptedPdf: TBytes;
  const Password: AnsiString;
  out Context: TPdfIncrementalEncryptionContext;
  out ErrorText: string): Boolean;
procedure ClearIncrementalEncryptionContext(
  var Context: TPdfIncrementalEncryptionContext);
function EncryptIncrementalObjectBody(const PlainBody: TBytes;
  ObjectNumber, Generation: Integer;
  const Context: TPdfIncrementalEncryptionContext;
  out EncryptedBody: TBytes;
  out ErrorText: string): Boolean;

說明

FPdfEncrypt 實作 ISO 32000-2 第 7.6 節定義的 PDF AES-256 安全處理器,以及 ISO/TS 32003 定義的 AESV4-GCM 擴充。EncryptDocumentBytes 以選定的修訂將明文 PDF 位元組陣列轉為加密文件

DeriveEncryptionKeys 執行以密碼為基礎的金鑰衍生,產生 32 位元組檔案加密金鑰以及 /O、/U、/OE、/UE 與 /Perms 字典項目。BuildEncryptDictionary 將這些項目序列化為 /Encrypt 字典文字。EncryptStreamPayload 使用檔案金鑰對單一物件串流套用 AES-256-CBC 加密

TPdfEncryptOptions 驅動整個衍生流程:UserPassword 與 OwnerPassword 做為雜湊種子,擁有者密碼留空時回退使用使用者密碼,Revision 選擇 erR5、erR6 或 erR7 處理器,EncryptMetadata 為 true 時 XMP 中繼資料串流維持加密

TPdfEncryptOptions.Default 以空白的使用者與擁有者密碼、全部允許的 $FFFFFFFC 權限(位元 1 與 2 保留為 0,位元 3 以上的每個權限位元都是 1)、EncryptMetadata true、修訂 erR5 與 EnableIntegrityProtection false 起始

建置自訂儲存管線的進階使用者可直接呼叫這三個基本操作;應用程式程式碼通常使用 TPdf.SaveAsEncrypted 或 TPdf.SaveAsEncryptedToStream,它們會在內部呼叫 EncryptDocumentBytes

當 EnableIntegrityProtection 為 true 時,寫入器會產出 PDF 2.0 ISO/TS 32004 獨立驗證碼,包含精確涵蓋 EOF 的 /ByteRange、/KDFSalt、CMS AuthenticatedData、HKDF-SHA256、AES-256 Key Wrap 與 HMAC-SHA256

自 v3.383.0 起,隨附的 pdfium 二進位檔也能直接解析 AESV4(V=6/R=7),因為本機的安全處理器修補加入了 AES-256-GCM,所以 TPdf.LoadDocument 不需先行解密即可開啟這類文件;未帶此修補的原版 pdfium 建置仍會拒絕,此時改由 DecryptAesGcmDocumentBytes 先驗證並解密位元組,再交給 TPdf.LoadDocument。該函式在密碼、權限、PDF MAC、字串標籤或串流標籤驗證失敗時,不會回傳任何部分明文

ReadIncrementalEncryptionContext 驗證目前的加密修訂鏈並還原繼承的安全性狀態。EncryptIncrementalObjectBody 在單一變更的間接物件中加密字串與串流承載,同時保留中繼資料排除並更新直接的 /Length 值;ClearIncrementalEncryptionContext 清除還原的金鑰材料

還原出的 TPdfIncrementalEncryptionContext 攜帶附加物件所用的 FileEncryptionKey、隨獨立驗證碼一併出現的 KdfSalt、決定採用 AESV3-CBC 或 AESV4-GCM 處理的繼承 Revision、EncryptMetadata 政策、記錄為 EncryptObjectNumber 與 EncryptGeneration 的 Trailer /Encrypt 參考,以及 PDF MAC 存在且通過驗證時設定的 HasAuthenticationCode。若是在舊式 R2/R3/R4 來源上做增量更新,LegacyCryptMethod 記錄承載資料的加密方式(lcmRc4 或 lcmAesV2),LegacyKeyLength 記錄檔案金鑰長度(以位元組計,5..16),而 FirstFileId 保留舊式金鑰衍生所用的第一個 /ID 元素

函式

FunctionDescription
DeriveEncryptionKeys執行以密碼為基礎的金鑰衍生,傳回檔案金鑰與五個字典項目
BuildEncryptDictionary將金鑰與選項序列化為 /Encrypt 字典文字
EncryptStreamPayload使用檔案金鑰以 AES-256-CBC 加密單一物件串流
EncryptDocumentBytes一次性輔助函式:衍生金鑰、重寫串流,並將 /Encrypt 字典注入整份 PDF 位元組陣列
DecryptAesGcmDocumentBytes驗證並解密 AESV4-GCM 輸出,成為 PDFium 可載入的明文位元組
ValidateEncryptedDocumentMac使用使用者或擁有者密碼,針對原始加密位元組驗證獨立 PDF MAC
ReadIncrementalEncryptionContext驗證加密修訂鏈,並還原繼承的檔案金鑰、修訂、中繼資料政策、/Encrypt 參照與 PDF MAC 狀態
ClearIncrementalEncryptionContext安全清除還原的增量加密金鑰材料
EncryptIncrementalObjectBody在繼承的安全性內容下,加密單一變更間接物件本體中的字串與串流

類型

TypeDescription
TPdfEncryptionRevision列舉,選擇 AESV3-CBC 修訂 erR5 或 erR6,或 ISO/TS 32003 AESV4-GCM 修訂 erR7;舊版標準安全處理器修訂 erR2、erR3 與 erR4 排在其後,讓既有序數保持穩定,且為唯讀——當增量更新替 RC4 或 AESV2 來源的物件重新密封時,由 TPdfIncrementalEncryptionContext 回報;用它們加密新文件會拋出例外
TPdfLegacyCryptMethod舊版 R2/R3/R4 來源的承載方法:lcmRc4 為 RC4(V1、V2 或 CFM V2),lcmAesV2 為帶隨機 IV 前綴的 AES-128-CBC(CFM AESV2)
TPdfEncryptOptions記錄,攜帶密碼、權限旗標、中繼資料政策、安全處理器修訂,以及選用的 PDF MAC 完整性保護
TPdfEncryptionKeys記錄,保存檔案金鑰、安全處理器項目與選用的 32 位元組 /KDFSalt
TPdfIncrementalEncryptionContext經驗證的繼承安全性狀態,用於附加加密間接物件而不取代現有的 /Encrypt 字典
TPdfMacValidationStatuspmvsValid、pmvsNotPresent、pmvsInvalid 或 pmvsUnsupported
TPdfMacValidationResult獨立 PDF MAC 驗證的 Status 與診斷 MessageText;TPdfMacValidationResult.IsValid 只有在 pmvsValid 時為 true,因此 MAC 缺失、無效與不支援都回報 false

備註

另請參閱

FPdfAes, FPdfSha256, FPdfHmac, FPdfMac, TPdf.SaveAsEncrypted, TPdf.SaveAsEncryptedToStream