How to Add Encryption and Password Protection to a Zip File using C# .NET Framework

Cloudmersive
2 min readJan 24, 2024

--

Our zip files aren’t secure when they’re compressed — anyone who comes across these files can extract and decompress their contents. When we add encryption and password protection, however, we limit the pool of potential document viewers to a very restricted group.

Using the below code, we can take advantage of a free API that allows us to add encryption and password protection measures to our zip files with minimal code and simple request parameters. We can use this solution to streamline our zip security workflows, drastically simplifying this step in any of our relevant applications.

Before we begin structuring our API call, we’ll first need to retrieve a free API key to authorize our requests. This will allow us to make up to 800 API calls per month with zero additional commitments (our total will simply reset the following month once we reach it).

Right after that, we can go ahead and install the SDK. To do so via NuGet, let’s go ahead and run the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Next, let’s make sure our encryption request is structured properly. We can follow the application/JSON Example below:

{
"InputFileContents": "string",
"Password": "string",
"EncryptionAlgorithm": "string"
}

Finally, let’s copy the below code into our file to call the function. Let’s enter our API key in the appropriate snippet and supply our zip file contents (along with our password and, optionally, our preferred encryption algorithm) in our encryption request:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

namespace Example
{
public class ZipArchiveZipEncryptAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ZipArchiveApi();
var encryptionRequest = new ZipEncryptionAdvancedRequest(); // ZipEncryptionAdvancedRequest | Encryption request

try
{
// Encrypt and password protect a zip file
Object result = apiInstance.ZipArchiveZipEncryptAdvanced(encryptionRequest);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ZipArchiveApi.ZipArchiveZipEncryptAdvanced: " + e.Message );
}
}
}
}

That’s all there is to it — 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