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 往上每个权限位都置位),EncryptMetadata 为 true,revision 为 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),因为本地 security-handler 补丁加入了 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 元素
| 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;旧式 Standard Security Handler 修订 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 字典的情况下追加加密的间接对象 |
TPdfMacValidationStatus | pmvsValid、pmvsNotPresent、pmvsInvalid 或 pmvsUnsupported |
TPdfMacValidationResult | 独立 PDF MAC 验证的 Status 和诊断 MessageText;TPdfMacValidationResult.IsValid 仅在 pmvsValid 时为 true,因此缺失、无效和不受支持的 MAC 都会报告 false |
DecryptAesGcmDocumentBytes 跟随加密增量写入器生成的经典 /Prev 修订链,并解析最新的活动对象代次;本版本不提供原生 PDFium AESV4 加载和外部 xref 流 AESV4 输入Permissions 使用 ISO 32000-1 表 22 的位布局(/P 标志)