Encryption is a famous and reliable approach to protect the content of PDF documents. When a document is encrypted, its contents become unreadable, and only authorized users can decrypt the document to obtain access to the contents. When you protect documents with a password, people who enter the correct password can access the file or folder. A stronger security method is encryption, with common types being 128-bit key or 256-bit AES encryption. Please note that Encryption turns document content into unreadable ciphertext. To decrypt the document, authorized users need a key, which is typically a password or a digital certificate.
In this blog, we are going to discuss the steps and details pertinent to encryption and decryption of PDF documents using Java REST API.
- Java REST API
- Encrypt PDF using the cURL command
- Encrypt PDF files in Java
- Decrypt PDF using the cURL command
- Decrypt PDF files in Java
Java REST API
Aspose.PDF Cloud SDK for Java is a wrapper around Aspose.PDF Cloud that enables Java programmers to take full advantage of the features currently being offered in our Cloud API. So right within your Java code, you get the capabilities to create, edit and transform PDF files to various Supported Document Formats.
In order to use the SDK, the first step is to install it on your system. The Cloud SDK is available for download over Maven and GitHub. Now add the following details in your pom.xml file to download and use Aspose.Pdf.jar in your Maven build project.
<repositories> <repository> <id>aspose-cloud</id> <name>artifact.aspose-cloud-releases</name> <url>http://artifact.aspose.cloud/repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf-cloud</artifactId> <version>21.1.0</version> <scope>compile</scope> </dependency> </dependencies>
We also recommend you to visit the following link for detailed information on How to install Aspose.Cloud SDKs.
Free subscription on Cloud dashboard
Aspose.PDF Cloud SDK For Java is an open-source free Cloud SDK for PDF file processing. When we mention free, it means the complete source code of the SDK is available for download on the GitHub repository. So you can modify its code as per your requirements. But, in order to ensure data confidentiality and integrity, the Cloud APIs are only accessible to authorized people and they only access their particular cloud storage.
Therefore, you first need to visit Aspose.Cloud dashboard and if you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information.
Encrypt PDF using the cURL command
The cURL commands are one of the easiest and coolest ways to access the REST APIs. So let’s talk about adding the different annotations using the cURL commands. So in order to use the cURL commands, we need to first generate a JWT access authorization token. The JSON Web Token (JWT) is based on Client ID and Client Secret details which are unique keys generated for each customer. So, login to the Aspose.Cloud dashboard using your credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.
curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4&client_secret=d87269aade6a46cdc295b711e26809af" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"
Before proceeding further, please note that when we encrypt the PDF files, we set User and owner passwords.
Document open password
A Document Open password (also known as a user password) requires a user to type a password to open the PDF.
A permissions 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.
Please note that the API accepts the owner and user passwords in Base64encoded format. In the following cURL command, the ownerPassword (b3duZXJcLy8/ICQxMl5QYXNzd29yZCEm) and userPassword (dXNlciAkXlBhc3N3b3JkISY=) are specified.
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithTable.pdf/encrypt?userPassword=dXNlciAkXlBhc3N3b3JkISY%3D&ownerPassword=b3duZXJcLy8%2FICQxMl5QYXNzd29yZCEm&cryptoAlgorithm=AESx128&permissionsFlags=AssembleDocument&usePdf20=false" \ -H "accept: application/json" \ -H "authorization: Bearer <JWT Token>"
Encrypt PDF files in Java
We do not simply add password protection to the PDF files, but a stronger security method is encryption is used. The Java REST API enables you to select one of the following encryption methods which turn the document content into unreadable ciphertext.
|RC4x40||RC4 with key length 40.|
|RC4x128||RC4 with key length 128.|
|AESx128||AES with key length 128.|
|AESx256||AES with key length 256.|
Use the following code snippet to add userPassword and ownerPassword to PDF files already available on cloud storage.
- The first step is to create a PdfApi instance while passing clientID and clientSecret details as arguments
- Upload the PDF file to cloud storage using uploadFile(…) method of PdfApi
- Finally call postEncryptDocumentInStorage(…) method of PdfApi which takes input PDF file name, user and owner passwords, and a value from CryptoAlgorithm enumeration as arguments
When you will try to open resultant file, a prompt to provide password details will appear.
Decrypt PDF using the cURL command
The cURL command can also be used to decrypt an existing encoded PDF document. In the example given below, the PostDecryptDocumentInStorage API is accessed which takes the password details as an argument.
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithTable.pdf/decrypt?password=dXNlciAkXlBhc3N3b3JkISY%3D" \ -H "accept: application/json" \ -H "authorization: Bearer <JWT Token>"
Decrypt PDF files in Java
In order to decrypt the PDF files using Java, please follow the steps explained below
- Create PdfApi instance while passing clientSecret and clientID as arguments
- Create string object defining input PDF file
- Upload the file to cloud storage using uploadFile(….) method of PdfApi class
- Finally call postDecryptDocumentInStorage(…) method while providing input PDF file name and decryption password as arguments
The decrypted file used in above example can be downloaded from PdfWithTable-Encrypted.pdf.
In this blog, we have discussed the steps on how to encrypt or decrypt PDF files with fewer code lines. So now you can easily encrypt your source files and share them over the internet without any fear of data theft or document misuse. Other than encryption features, the Aspose.PDF Cloud SDK for Java provides a plethora of features related to PDF format. You may consider visiting the following link for further details on Aspose.PDF Cloud Features.
We also recommend you to read following blog for information on