PDFium Delphi Component 文件

FPdfEncrypt 单元

单元: FPdfEncrypt
派生 AES-256 加密密钥,并生成 ISO 32000-2 AESV3-CBC 或 ISO/TS 32003 AESV4-GCM 的字符串、流和加密字典

语法

TPdfEncryptionRevision = (erR5, erR6, erR7);

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;
  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 加密

构建自定义保存管线的高级用户可以直接调用这三个原语;应用程序代码通常使用 TPdf.SaveAsEncryptedTPdf.SaveAsEncryptedToStream,它们在内部调用 EncryptDocumentBytes

EnableIntegrityProtection 为 true 时,写入器会生成 PDF 2.0 ISO/TS 32004 独立身份验证码,带有精确覆盖 EOF 的 /ByteRange/KDFSalt、CMS AuthenticatedData、HKDF-SHA256、AES-256 Key Wrap 和 HMAC-SHA256

PDFium 目前未通过其公共 ABI 公开 AESV4 解析。请先调用 DecryptAesGcmDocumentBytes,再将返回的字节传递给 TPdf.LoadDocument;在口令、权限、PDF MAC、字符串标签或流标签校验失败后,该函数不会返回任何部分明文

ReadIncrementalEncryptionContext 对当前加密修订链进行身份验证并恢复继承的安全状态。EncryptIncrementalObjectBody 在继承的安全上下文下加密单个已变更间接对象中的字符串和流载荷,同时保留元数据排除并更新直接的 /Length 值;ClearIncrementalEncryptionContext 清除恢复的密钥材料

函数

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 修订 erR5erR6,或 ISO/TS 32003 AESV4-GCM 修订 erR7
TPdfEncryptOptions记录,携带口令、权限标志、元数据策略、安全处理器修订以及可选的 PDF MAC 完整性保护
TPdfEncryptionKeys记录,保存文件密钥、安全处理器条目和可选的 32 字节 /KDFSalt
TPdfIncrementalEncryptionContext经过身份验证的继承安全状态,用于在不替换现有 /Encrypt 字典的情况下追加加密的间接对象
TPdfMacValidationStatuspmvsValidpmvsNotPresentpmvsInvalidpmvsUnsupported
TPdfMacValidationResult独立 PDF MAC 验证的 Status 和诊断 MessageText

备注

参见

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