Unicode C Requires Chilkat v11.7.0+
Unicode C
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 Unicode C Downloads
#include <C_CkCertW.h>
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCertW recipient;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
HCkPdfW reader;
HCkJsonObjectW info;
HCkCertW myCert;
HCkJsonObjectW empty;
HCkJsonObjectW loadedInfo;
success = 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
// The recipient's certificate. Only the public certificate is needed to encrypt.
recipient = CkCertW_Create();
success = CkCertW_LoadFromFile(recipient,L"qa_data/certs/recipient.cer");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(recipient));
CkCertW_Dispose(recipient);
return;
}
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/two_page_memo.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
return;
}
// Call AddEncryptCert once for each recipient.
success = CkPdfW_AddEncryptCert(pdf,recipient);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
return;
}
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"method",L"certificate");
CkJsonObjectW_UpdateBool(json,L"recipientPermissions[0].changeSecurity",TRUE);
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_Protection(pdf,L"encrypt",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"method: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.method"));
wprintf(L"algorithm: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.algorithm"));
wprintf(L"recipients: %d\n",CkJsonObjectW_SizeOfArray(jsonOut,L"pending.recipients"));
success = CkPdfW_WriteFile(pdf,L"qa_output/memo_for_recipient.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// PreLoadCheck says how the file is protected, without loading it.
reader = CkPdfW_Create();
info = CkJsonObjectW_Create();
success = CkPdfW_PreLoadCheck(reader,L"qa_output/memo_for_recipient.pdf",info);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
return;
}
wprintf(L"saved file, method: %s\n",CkJsonObjectW_stringOf(info,L"method"));
wprintf(L"saved file, recipients: %d\n",CkJsonObjectW_SizeOfArray(info,L"recipients"));
// 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.)
myCert = CkCertW_Create();
success = CkCertW_LoadPfxFile(myCert,L"qa_data/pfx/recipient.pfx",L"pfx-password");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(myCert));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
CkCertW_Dispose(myCert);
return;
}
success = CkPdfW_SetDecryptCert(reader,myCert);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
CkCertW_Dispose(myCert);
return;
}
success = CkPdfW_LoadFile(reader,L"qa_output/memo_for_recipient.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
CkCertW_Dispose(myCert);
return;
}
empty = CkJsonObjectW_Create();
loadedInfo = CkJsonObjectW_Create();
success = CkPdfW_Protection(reader,L"getInfo",empty,loadedInfo);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
CkCertW_Dispose(myCert);
CkJsonObjectW_Dispose(empty);
CkJsonObjectW_Dispose(loadedInfo);
return;
}
wprintf(L"opened with: %s\n",CkJsonObjectW_stringOf(loadedInfo,L"loaded.openedWith"));
wprintf(L"pages: %d\n",CkPdfW_getNumPages(reader));
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
CkCertW_Dispose(recipient);
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
CkCertW_Dispose(myCert);
CkJsonObjectW_Dispose(empty);
CkJsonObjectW_Dispose(loadedInfo);
}