PDF files are widely used to store and share sensitive information, from financial statements to legal documents. However, these files can be vulnerable to unauthorized access and editing, which is why encrypting and password-protecting PDFs is crucial to maintain their security. In this blog post, we will explore how to encrypt and password-protect PDF files using Python-based REST APIs. By following the step-by-step instructions outlined in this guide, you will be able to add an extra layer of security to your PDF files and ensure they remain safe from prying eyes. So whether you need to protect confidential business documents or personal files, read on to learn how to encrypt, protect, and secure your PDF files with ease.

REST API to Protect PDF

Aspose.PDF Cloud SDK for Python is a powerful tool that allows you to add password protection to your PDF files easily. With just a few lines of code, you can encrypt your PDF files and restrict access to authorized individuals. The SDK provides several encryption algorithms to choose from, including 40-bit RC4, 128-bit RC4, 128-bit AES, and 256-bit AES.

Now, in order to get started with Python SDK, the first step is its installation. It is available for download over PIP and GitHub repository. So please execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

 pip install asposepdfcloud

Client Credentials

After the installation, the next major step is a free subscription to our cloud services on Aspose.Cloud dashboard. Simply Sig-up using GitHub or Google account by clicking the Create a new Account button and provide the required information. Then login with newly subscribed account and obtain your Client Credentials.

Client Credentials

Image 2:- Client credentials on Aspose.Cloud dashboard.

Encrypt PDF using Python

The API enables you to set two types of passwords i.e. Document open password (user password) and Permission password (owner password).

Document open password

A Document Open password (also known as a user password) requires a user to type a password to open the PDF.

Permissions password

A permission’s password (also known as a master/owner password) is required to change permission settings. Using a permissions’ password, you can restrict printing, editing, and copying content in the PDF. This password is required to change the restrictions you have already applied.

If the PDF is secured with both types of passwords, it can be opened with either password.

Also, please note that the API accepts the owner and user passwords in Base64encoded format. In the following code snippet, the ownerPassword (b3duZXJcLy8/ICQxMl5QYXNzd29yZCEm) and userPassword (dXNlciAkXlBhc3N3b3JkISY=) are specified. Please follow the steps given below to encrypt PDF files using Python code snippet.

  • Create an instance of ApiClient class while providing Client ID & Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as an input argument
  • Now call the method post_encrypt_document_in_storage(..) method of PdfApi class while passing the name of input PDF file, user and owner passwords (in Base64 encoding) and a Cryptographic algorithm as arguments.

That’s it! With just a few lines of code, we have learned the steps to password-protect PDF files using Aspose.PDF Cloud SDK for Python.

Please note that you can use either of the cryptographic algorithm value during the PDF encryption process

Name Description
RC4x40 RC4 with key length 40.
RC4x128 RC4 with key length 128.
AESx128 AES with key length 128.
AESx256 AES with key length 256.

The input PDF file used in the above example can be downloaded from awesomeTable.pdf.

Encrypt PDF using cURL Command

The REST APIs are also accessible via cURL commands on any platform. We can use the command prompt/terminal window to execute the cURL commands. Since Aspose.PDF Cloud is also developed as per REST architecture, so we can also use the cURL command for encrypting the PDF files. But before proceeding further, we need to generate a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. Please execute the following command to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now, once we have the JWT token, we need to execute the following command to encrypt the PDF document.

curl -v -X POST "https://api.aspose.cloud/v3.0/pdf/awesomeTable.pdf/encrypt?userPassword=dXNlciAkXlBhc3N3b3JkISY%3D&ownerPassword=b3duZXJcLy8%2FICQxMl5QYXNzd29yZCEm&cryptoAlgorithm=AESx128&permissionsFlags=AssembleDocument&usePdf20=false" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

In conclusion, using a REST API to encrypt PDF files is a quick and effective way to ensure the security and privacy of your important documents. Whether you need to lock PDF from editing or add password protection, these methods provide a convenient solution that is both user-friendly and secure. By following the steps outlined in this blog post, you can easily secure your PDF files and rest assured that your valuable information is protected.

Please note that our cloud SDKs are built under an MIT license, so you may download the complete code snippet from GitHub. Furthermore, we highly recommend exploring the Developer Guide to learn more about other exciting features of the API.

Lastly, if you encounter any issue or have any related query while using the API, please feel free to contact us via the free customer support forum.

We also suggest going through the following articles to learn more about