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.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
PDFium 目前未通过其公共 ABI 公开 AESV4 解析。请先调用 DecryptAesGcmDocumentBytes,再将返回的字节传递给 TPdf.LoadDocument;在口令、权限、PDF MAC、字符串标签或流标签校验失败后,该函数不会返回任何部分明文
ReadIncrementalEncryptionContext 对当前加密修订链进行身份验证并恢复继承的安全状态。EncryptIncrementalObjectBody 在继承的安全上下文下加密单个已变更间接对象中的字符串和流载荷,同时保留元数据排除并更新直接的 /Length 值;ClearIncrementalEncryptionContext 清除恢复的密钥材料
| Function | Description |
|---|---|
DeriveEncryptionKeys | 执行基于口令的密钥派生,返回文件密钥和五个字典条目 |
BuildEncryptDictionary | 将密钥与选项序列化为 /Encrypt 字典文本 |
EncryptStreamPayload | 使用文件密钥以 AES-256-CBC 加密单个对象流 |
EncryptDocumentBytes | 一次性辅助函数:派生密钥、重写流,并将 /Encrypt 字典注入整个 PDF 字节数组 |
DecryptAesGcmDocumentBytes | 对 AESV4-GCM 输出进行身份验证并解密为 PDFium 可加载的明文字节 |
ValidateEncryptedDocumentMac | 使用用户或所有者口令对照原始加密字节验证独立 PDF MAC |
ReadIncrementalEncryptionContext | 对加密修订链进行身份验证,并恢复继承的文件密钥、修订、元数据策略、/Encrypt 引用和 PDF MAC 状态 |
ClearIncrementalEncryptionContext | 安全清除恢复的增量加密密钥材料 |
EncryptIncrementalObjectBody | 在继承的安全上下文下加密单个已变更间接对象体中的字符串和流 |
| Type | Description |
|---|---|
TPdfEncryptionRevision | 枚举,选择 AESV3-CBC 修订 erR5 或 erR6,或 ISO/TS 32003 AESV4-GCM 修订 erR7 |
TPdfEncryptOptions | 记录,携带口令、权限标志、元数据策略、安全处理器修订以及可选的 PDF MAC 完整性保护 |
TPdfEncryptionKeys | 记录,保存文件密钥、安全处理器条目和可选的 32 字节 /KDFSalt |
TPdfIncrementalEncryptionContext | 经过身份验证的继承安全状态,用于在不替换现有 /Encrypt 字典的情况下追加加密的间接对象 |
TPdfMacValidationStatus | pmvsValid、pmvsNotPresent、pmvsInvalid 或 pmvsUnsupported |
TPdfMacValidationResult | 独立 PDF MAC 验证的 Status 和诊断 MessageText |
DecryptAesGcmDocumentBytes 跟随加密增量写入器生成的经典 /Prev 修订链,并解析最新的活动对象代次;本版本不提供原生 PDFium AESV4 加载和外部 xref 流 AESV4 输入Permissions 使用 ISO 32000-1 表 22 的位布局(/P 标志)