Lua Requires Chilkat v11.7.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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
local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
local info = chilkat.JsonObject.new()
-- AES-256, the default. Saved as PDF 1.7 with Adobe extension level 8.
local pdf1 = chilkat.Pdf.new()
local success = pdf1:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf1.last_error_text)
return
end
pdf1.user_password = "user-secret"
pdf1.owner_password = "owner-secret"
json:update_string("algorithmChoice", "aes-256")
success = pdf1:protection("encrypt", json, jsonOut)
if not success then
print(pdf1.last_error_text)
return
end
print("aes-256 will be saved as PDF " .. jsonOut:string_of("pending.pdfVersion") .. ", Adobe extension level " .. jsonOut:int_of("pending.adobeExtensionLevel"))
success = pdf1:write_file("qa_output/memo_aes256.pdf")
if not success then
print(pdf1.last_error_text)
return
end
success = pdf1:pre_load_check("qa_output/memo_aes256.pdf", info)
if not success then
print(pdf1.last_error_text)
return
end
print(" saved file: " .. info:string_of("algorithm") .. ", PDF " .. info:string_of("pdfVersion"))
-- AES-128, for readers that predate AES-256. Saved as PDF 1.6.
local pdf2 = chilkat.Pdf.new()
success = pdf2:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf2.last_error_text)
return
end
pdf2.user_password = "user-secret"
pdf2.owner_password = "owner-secret"
json:update_string("algorithmChoice", "aes-128")
success = pdf2:protection("encrypt", json, jsonOut)
if not success then
print(pdf2.last_error_text)
return
end
print("aes-128 will be saved as PDF " .. jsonOut:string_of("pending.pdfVersion"))
success = pdf2:write_file("qa_output/memo_aes128.pdf")
if not success then
print(pdf2.last_error_text)
return
end
success = pdf2:pre_load_check("qa_output/memo_aes128.pdf", info)
if not success then
print(pdf2.last_error_text)
return
end
print(" saved file: " .. info:string_of("algorithm") .. ", PDF " .. info:string_of("pdfVersion"))
-- RC4-128, a broken cipher, only for readers too old for AES. Saved as PDF 1.4.
local pdf3 = chilkat.Pdf.new()
success = pdf3:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf3.last_error_text)
return
end
pdf3.user_password = "user-secret"
pdf3.owner_password = "owner-secret"
json:update_string("algorithmChoice", "rc4-128")
success = pdf3:protection("encrypt", json, jsonOut)
if not success then
print(pdf3.last_error_text)
return
end
print("rc4-128 will be saved as PDF " .. jsonOut:string_of("pending.pdfVersion"))
success = pdf3:write_file("qa_output/memo_rc4_128.pdf")
if not success then
print(pdf3.last_error_text)
return
end
success = pdf3:pre_load_check("qa_output/memo_rc4_128.pdf", info)
if not success then
print(pdf3.last_error_text)
return
end
print(" saved file: " .. info:string_of("algorithm") .. ", PDF " .. info:string_of("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