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 implement security handler AES-256 ของ PDF ที่นิยามใน ISO 32000-2 มาตรา 7.6 และส่วนขยาย AESV4-GCM ที่นิยามโดย ISO/TS 32003 EncryptDocumentBytes แปลงอาร์เรย์ไบต์ PDF ธรรมดาให้เป็นเอกสารที่เข้ารหัสด้วยรีวิชันที่เลือกไว้
DeriveEncryptionKeys รันการ derive key จากรหัสผ่านที่ผลิต file encryption key ขนาด 32 ไบต์พร้อมรายการ dictionary /O, /U, /OE, /UE และ /Perms BuildEncryptDictionary serialize รายการเหล่านี้เป็นข้อความ dictionary /Encrypt EncryptStreamPayload ใช้การเข้ารหัส AES-256-CBC กับ stream ของอ็อบเจกต์หนึ่งตัวโดยใช้ file key
ผู้ใช้ระดับสูงที่สร้าง save pipeline ของตัวเองเรียกใช้ primitive ทั้งสามตัวได้โดยตรง; โค้ดระดับแอปพลิเคชันปกติใช้ TPdf.SaveAsEncrypted หรือ TPdf.SaveAsEncryptedToStream ซึ่งเรียก EncryptDocumentBytes ภายในอยู่แล้ว
เมื่อ EnableIntegrityProtection เป็น true ตัวเขียนจะปล่อย authentication code แบบ standalone ของ ISO/TS 32004 สำหรับ PDF 2.0 พร้อม /ByteRange ที่คลุมถึง EOF พอดี, /KDFSalt, CMS AuthenticatedData, HKDF-SHA256, AES-256 Key Wrap และ HMAC-SHA256
PDFium ยังไม่เปิดเผยการ parse AESV4 ผ่าน ABI สาธารณะในตอนนี้ เรียก DecryptAesGcmDocumentBytes ก่อนแล้วส่งไบต์ที่ได้ไปให้ TPdf.LoadDocument; ฟังก์ชันนี้จะไม่คืน plaintext บางส่วนเมื่อรหัสผ่าน, permission, PDF MAC, string tag หรือ stream tag ผิดพลาด
ReadIncrementalEncryptionContext ตรวจยืนยันลูกโซ่รีวิชันที่เข้ารหัสปัจจุบันแล้วกู้คืนสถานะ security ที่สืบทอดมา EncryptIncrementalObjectBody เข้ารหัสสตริงและเพย์โหลดของ stream ใน indirect object ที่เปลี่ยนแปลงหนึ่งตัว พร้อมคงข้อยกเว้นของ metadata และอัปเดต /Length แบบ direct; ClearIncrementalEncryptionContext ล้าง key material ที่กู้คืนมาออก
| Function | Description |
|---|---|
DeriveEncryptionKeys | รันการ derive key จากรหัสผ่าน แล้วคืน file key และรายการ dictionary ทั้งห้ารายการ |
BuildEncryptDictionary | serialize key กับ options เป็นข้อความ dictionary /Encrypt |
EncryptStreamPayload | เข้ารหัส object stream หนึ่งตัวด้วย AES-256-CBC โดยใช้ file key |
EncryptDocumentBytes | ตัวช่วยแบบ one-shot ที่ derive key, เขียน stream ใหม่ และแทรก dictionary /Encrypt ลงอาร์เรย์ไบต์ PDF ทั้งไฟล์ |
DecryptAesGcmDocumentBytes | ตรวจยืนยันและถอดรหัสเอาต์พุต AESV4-GCM เป็นไบต์ plaintext ที่ PDFium โหลดได้ |
ValidateEncryptedDocumentMac | ตรวจสอบ PDF MAC แบบ standalone เทียบกับไบต์ที่เข้ารหัสต้นฉบับโดยใช้รหัสผ่าน user หรือ owner |
ReadIncrementalEncryptionContext | ตรวจยืนยันลูกโซ่รีวิชันที่เข้ารหัสแล้วกู้คืน file key, รีวิชัน, นโยบาย metadata, reference /Encrypt และสถานะ PDF MAC ที่สืบทอดมา |
ClearIncrementalEncryptionContext | ล้าง key material ของการเข้ารหัสแบบ incremental ที่กู้คืนมาอย่างปลอดภัย |
EncryptIncrementalObjectBody | เข้ารหัสสตริงและ stream ในเนื้อความของ indirect object ที่เปลี่ยนแปลงหนึ่งตัว ภายใต้บริบท security ที่สืบทอดมา |
| Type | Description |
|---|---|
TPdfEncryptionRevision | enumeration เลือกรีวิชัน AESV3-CBC erR5 หรือ erR6 หรือรีวิชัน AESV4-GCM ตาม ISO/TS 32003 คือ erR7 |
TPdfEncryptOptions | เรกคอร์ดที่ถือรหัสผ่าน, permission flag, นโยบาย metadata, รีวิชันของ security handler และการป้องกันความสมบูรณ์แบบ PDF MAC เมื่อต้องการ |
TPdfEncryptionKeys | เรกคอร์ดที่ถือ file key, รายการของ security handler และ /KDFSalt ขนาด 32 ไบต์เมื่อต้องการ |
TPdfIncrementalEncryptionContext | สถานะ security ที่สืบทอดมาซึ่งผ่านการตรวจยืนยัน ใช้ต่อท้าย indirect object ที่เข้ารหัสโดยไม่ต้องแทนที่ dictionary /Encrypt เดิม |
TPdfMacValidationStatus | pmvsValid, pmvsNotPresent, pmvsInvalid หรือ pmvsUnsupported |
TPdfMacValidationResult | สถานะ Status ของการตรวจ PDF MAC แบบ standalone และ MessageText เชิงวินิจฉัย |
DecryptAesGcmDocumentBytes เดินตามลูกโซ่รีวิชันแบบ classic /Prev ที่ตัวเขียน incremental แบบเข้ารหัสปล่อยออกมา และ resolve object generation ที่ active ใหม่ล่าสุด; การโหลด AESV4 แบบ native ของ PDFium และการรับอินพุต AESV4 แบบ xref stream ภายนอกยังไม่พร้อมใช้ในรุ่นนี้Permissions ใช้ layout บิตของ ISO 32000-1 Table 22 (flag /P)