Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.7.0+

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  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 = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$oInfo = ObjCreate("Chilkat.JsonObject")

;  AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
$oPdf1 = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf1.LoadFile("qa_data/pdf/two_page_memo.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf1.LastErrorText & @CRLF)
    Exit
EndIf

$oPdf1.UserPassword = "user-secret"
$oPdf1.OwnerPassword = "owner-secret"
$oJson.UpdateString("algorithmChoice","aes-256")
$bSuccess = $oPdf1.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf1.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("aes-256 will be saved as PDF " & $oJsonOut.StringOf("pending.pdfVersion") & ", Adobe extension level " & $oJsonOut.IntOf("pending.adobeExtensionLevel") & @CRLF)
$bSuccess = $oPdf1.WriteFile("qa_output/memo_aes256.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf1.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oPdf1.PreLoadCheck("qa_output/memo_aes256.pdf",$oInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf1.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("  saved file: " & $oInfo.StringOf("algorithm") & ", PDF " & $oInfo.StringOf("pdfVersion") & @CRLF)

;  AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
$oPdf2 = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf2.LoadFile("qa_data/pdf/two_page_memo.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf2.LastErrorText & @CRLF)
    Exit
EndIf

$oPdf2.UserPassword = "user-secret"
$oPdf2.OwnerPassword = "owner-secret"
$oJson.UpdateString("algorithmChoice","aes-128")
$bSuccess = $oPdf2.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf2.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("aes-128 will be saved as PDF " & $oJsonOut.StringOf("pending.pdfVersion") & @CRLF)
$bSuccess = $oPdf2.WriteFile("qa_output/memo_aes128.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf2.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oPdf2.PreLoadCheck("qa_output/memo_aes128.pdf",$oInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf2.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("  saved file: " & $oInfo.StringOf("algorithm") & ", PDF " & $oInfo.StringOf("pdfVersion") & @CRLF)

;  RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
$oPdf3 = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf3.LoadFile("qa_data/pdf/two_page_memo.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf3.LastErrorText & @CRLF)
    Exit
EndIf

$oPdf3.UserPassword = "user-secret"
$oPdf3.OwnerPassword = "owner-secret"
$oJson.UpdateString("algorithmChoice","rc4-128")
$bSuccess = $oPdf3.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf3.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("rc4-128 will be saved as PDF " & $oJsonOut.StringOf("pending.pdfVersion") & @CRLF)
$bSuccess = $oPdf3.WriteFile("qa_output/memo_rc4_128.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf3.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oPdf3.PreLoadCheck("qa_output/memo_rc4_128.pdf",$oInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf3.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("  saved file: " & $oInfo.StringOf("algorithm") & ", PDF " & $oInfo.StringOf("pdfVersion") & @CRLF)

;  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