How to Encrypt and Password Protect a PDF using C/C++

Cloudmersive
2 min readAug 4, 2023

--

Creating applications for PDF file processing workflows can involve writing a lot more code than we want to. Thankfully, specialized utility APIs can make that process easier AND more efficient at the same time.

For example, you can use the below code to take advantage of a free PDF Encryption and Password-Protection API that allows you to set both user & owner passwords and choose from 128-bit RC4 and 256-bit AES encryption algorithms. This will make it easy to secure PDF documents at scale in any file processing application.

To structure your API call, start by installing Libcurl in your project:

libcurl/7.75.0

Next up, use the below examples to structure your request, and then supply a free-tier API key to authenticate (this allows up to 800 API calls per month with no additional commitment):

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/edit/pdf/encrypt");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "userPassword: <string>");
headers = curl_slist_append(headers, "ownerPassword: <string>");
headers = curl_slist_append(headers, "encryptionKeyLength: <string>");
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);

Easy, right? No more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet