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 <CkCertW.h>
#include <CkPdfW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool 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.
CkCertW recipient;
success = recipient.LoadFromFile(L"qa_data/certs/recipient.cer");
if (success == false) {
wprintf(L"%s\n",recipient.lastErrorText());
return;
}
CkPdfW pdf;
success = pdf.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// Call AddEncryptCert once for each recipient.
success = pdf.AddEncryptCert(recipient);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
CkJsonObjectW json;
json.UpdateString(L"method",L"certificate");
json.UpdateBool(L"recipientPermissions[0].changeSecurity",true);
CkJsonObjectW jsonOut;
success = pdf.Protection(L"encrypt",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"method: %s\n",jsonOut.stringOf(L"pending.method"));
wprintf(L"algorithm: %s\n",jsonOut.stringOf(L"pending.algorithm"));
wprintf(L"recipients: %d\n",jsonOut.SizeOfArray(L"pending.recipients"));
success = pdf.WriteFile(L"qa_output/memo_for_recipient.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// PreLoadCheck says how the file is protected, without loading it.
CkPdfW reader;
CkJsonObjectW info;
success = reader.PreLoadCheck(L"qa_output/memo_for_recipient.pdf",info);
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
wprintf(L"saved file, method: %s\n",info.stringOf(L"method"));
wprintf(L"saved file, recipients: %d\n",info.SizeOfArray(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.)
CkCertW myCert;
success = myCert.LoadPfxFile(L"qa_data/pfx/recipient.pfx",L"pfx-password");
if (success == false) {
wprintf(L"%s\n",myCert.lastErrorText());
return;
}
success = reader.SetDecryptCert(myCert);
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
success = reader.LoadFile(L"qa_output/memo_for_recipient.pdf");
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
CkJsonObjectW empty;
CkJsonObjectW loadedInfo;
success = reader.Protection(L"getInfo",empty,loadedInfo);
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
wprintf(L"opened with: %s\n",loadedInfo.stringOf(L"loaded.openedWith"));
wprintf(L"pages: %d\n",reader.get_NumPages());
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
}