Xbase++ Requires Chilkat v11.7.0+
Xbase++
Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4
See more PDF Protection Examples
Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oJsonOut
LOCAL oInfo
LOCAL oPdf1
LOCAL oPdf2
LOCAL oPdf3
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at two_page_memo.pdf
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
oInfo := CreateObject("Chilkat.JsonObject")
// AES-256, the default. Saved as PDF 1.7 with Adobe extension level 8.
oPdf1 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf1:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf1:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
RETURN
ENDIF
oPdf1:UserPassword := "user-secret"
oPdf1:OwnerPassword := "owner-secret"
oJson:UpdateString("algorithmChoice", "aes-256")
nSuccess := oPdf1:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf1:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
RETURN
ENDIF
? "aes-256 will be saved as PDF " + oJsonOut:StringOf("pending.pdfVersion") + ", Adobe extension level " + Str(oJsonOut:IntOf("pending.adobeExtensionLevel"))
nSuccess := oPdf1:WriteFile("qa_output/memo_aes256.pdf")
IF (nSuccess == 0)
? oPdf1:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
RETURN
ENDIF
nSuccess := oPdf1:PreLoadCheck("qa_output/memo_aes256.pdf", oInfo)
IF (nSuccess == 0)
? oPdf1:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
RETURN
ENDIF
? " saved file: " + oInfo:StringOf("algorithm") + ", PDF " + oInfo:StringOf("pdfVersion")
// AES-128, for readers that predate AES-256. Saved as PDF 1.6.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf2:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
RETURN
ENDIF
oPdf2:UserPassword := "user-secret"
oPdf2:OwnerPassword := "owner-secret"
oJson:UpdateString("algorithmChoice", "aes-128")
nSuccess := oPdf2:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf2:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
RETURN
ENDIF
? "aes-128 will be saved as PDF " + oJsonOut:StringOf("pending.pdfVersion")
nSuccess := oPdf2:WriteFile("qa_output/memo_aes128.pdf")
IF (nSuccess == 0)
? oPdf2:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
RETURN
ENDIF
nSuccess := oPdf2:PreLoadCheck("qa_output/memo_aes128.pdf", oInfo)
IF (nSuccess == 0)
? oPdf2:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
RETURN
ENDIF
? " saved file: " + oInfo:StringOf("algorithm") + ", PDF " + oInfo:StringOf("pdfVersion")
// RC4-128, a broken cipher, only for readers too old for AES. Saved as PDF 1.4.
oPdf3 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf3:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf3:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()
RETURN
ENDIF
oPdf3:UserPassword := "user-secret"
oPdf3:OwnerPassword := "owner-secret"
oJson:UpdateString("algorithmChoice", "rc4-128")
nSuccess := oPdf3:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf3:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()
RETURN
ENDIF
? "rc4-128 will be saved as PDF " + oJsonOut:StringOf("pending.pdfVersion")
nSuccess := oPdf3:WriteFile("qa_output/memo_rc4_128.pdf")
IF (nSuccess == 0)
? oPdf3:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()
RETURN
ENDIF
nSuccess := oPdf3:PreLoadCheck("qa_output/memo_rc4_128.pdf", oInfo)
IF (nSuccess == 0)
? oPdf3:LastErrorText
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()
RETURN
ENDIF
? " saved file: " + oInfo:StringOf("algorithm") + ", PDF " + oInfo:StringOf("pdfVersion")
// Sample output:
// aes-256 will be saved as PDF 1.7, Adobe extension level 8
// saved file: aes-256, PDF 1.7
// aes-128 will be saved as PDF 1.6
// saved file: aes-128, PDF 1.6
// rc4-128 will be saved as PDF 1.4
// saved file: rc4-128, PDF 1.4
oJson:destroy()
oJsonOut:destroy()
oInfo:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()