Classic ASP Requires Chilkat v11.7.0+
Classic ASP
Encrypt a PDF for a Certificate Instead of a Password
See more PDF Protection Examples
Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 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
' The recipient's certificate. Only the public certificate is needed to encrypt.
set recipient = Server.CreateObject("Chilkat.Cert")
success = recipient.LoadFromFile("qa_data/certs/recipient.cer")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( recipient.LastErrorText) & "</pre>"
Response.End
End If
set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
Response.End
End If
' Call AddEncryptCert once for each recipient.
success = pdf.AddEncryptCert(recipient)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
Response.End
End If
' recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
' recipient owner rights, so the file can be managed later.
set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("method","certificate")
success = json.UpdateBool("recipientPermissions[0].changeSecurity",1)
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Protection("encrypt",json,jsonOut)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "method: " & jsonOut.StringOf("pending.method")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "algorithm: " & jsonOut.StringOf("pending.algorithm")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "recipients: " & jsonOut.SizeOfArray("pending.recipients")) & "</pre>"
success = pdf.WriteFile("qa_output/memo_for_recipient.pdf")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
Response.End
End If
' PreLoadCheck says how the file is protected, without loading it.
set reader = Server.CreateObject("Chilkat.Pdf")
set info = Server.CreateObject("Chilkat.JsonObject")
success = reader.PreLoadCheck("qa_output/memo_for_recipient.pdf",info)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "saved file, method: " & info.StringOf("method")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "saved file, recipients: " & info.SizeOfArray("recipients")) & "</pre>"
' The recipient opens it with the certificate and its private key, here from a .pfx file.
' (On Windows, a certificate that is installed in the Windows certificate store with its private
' key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
set myCert = Server.CreateObject("Chilkat.Cert")
success = myCert.LoadPfxFile("qa_data/pfx/recipient.pfx","pfx-password")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( myCert.LastErrorText) & "</pre>"
Response.End
End If
success = reader.SetDecryptCert(myCert)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
Response.End
End If
success = reader.LoadFile("qa_output/memo_for_recipient.pdf")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
Response.End
End If
set empty = Server.CreateObject("Chilkat.JsonObject")
set loadedInfo = Server.CreateObject("Chilkat.JsonObject")
success = reader.Protection("getInfo",empty,loadedInfo)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "opened with: " & loadedInfo.StringOf("loaded.openedWith")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "pages: " & reader.NumPages) & "</pre>"
' Sample output:
' method: certificate
' algorithm: aes-256
' recipients: 1
' saved file, method: certificate
' saved file, recipients: 1
' opened with: certificate
' pages: 2
%>
</body>
</html>