How to Encrypt and Password Protect a Zip File using C/C++
Anyone in possession of a regular Zip file can extract and view its contents. Thankfully, we can increase the security of our Zip files by implementing simple encryption and password protection measures.
The below code allows us to encrypt and password protect a Zip file programmatically with a free API. We just need to provide our Zip file contents, password string, and desired encryption algorithm string (options are AES-256, AES-128, and PK-Zip) as outlined in the JSON request example below, and we’ll have a secure Zip file in our response.
{
"InputFileContents": "string",
"Password": "string",
"EncryptionAlgorithm": "string"
}
Before we structure our API call with ready-to-run C/C++ code examples, let’s first install libcurl in our project:
libcurl/7.75.0
Then let’s include the below code in our file:
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/archive/zip/encrypt/advanced");
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, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "InputFileContents=%3Cbyte%3E&Password=%3Cstring%3E&EncryptionAlgorithm=%3Cstring%3E";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
We can authorize our requests with a free-tier API key and perform up to 800 requests per month (you can get a free API key by registering a free account on the Cloudmersive website).