PDFium Delphi Component Docs

หน่วย FPdfEncrypt

Unit: FPdfEncrypt
หา key เข้ารหัส AES-256 และสร้างสตริง, stream และ encryption dictionary รูปแบบ ISO 32000-2 AESV3-CBC หรือ ISO/TS 32003 AESV4-GCM

Syntax

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;

Description

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 ที่กู้คืนมาออก

Functions

FunctionDescription
DeriveEncryptionKeysรันการ derive key จากรหัสผ่าน แล้วคืน file key และรายการ dictionary ทั้งห้ารายการ
BuildEncryptDictionaryserialize 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 ที่สืบทอดมา

Types

TypeDescription
TPdfEncryptionRevisionenumeration เลือกรีวิชัน 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 เดิม
TPdfMacValidationStatuspmvsValid, pmvsNotPresent, pmvsInvalid หรือ pmvsUnsupported
TPdfMacValidationResultสถานะ Status ของการตรวจ PDF MAC แบบ standalone และ MessageText เชิงวินิจฉัย

Remarks

See Also

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