Java - How to Encrypt or Decrypt PDF Documents
Tags: java, pdf, encrypt, decrypt
Published: 2022-07-16 06:12:57

Numerous confidential documents such as contracts, invoices, and other files are saved in PDF format. Sharing these sensitive documents over the internet would be a risk since others may be able to access the document content without your authorization. Encrypting the documents with passwords would be an efficient way to resolve this problem. In this article, I will introduce how to **encrypt or decrypt PDF documents in Java** using [Spire.PDF for Java](https://www.e-iceblue.com/Introduce/pdf-for-java.html) library.

## Add Dependencies
**Method 1:** If you are using maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>8.7.0</version>
    </dependency>
</dependencies>
```
**Method 2:** If you are not using maven, you can download the JAR file from [this link](https://www.e-iceblue.com/Download/pdf-for-java.html), extract the zip file and then import the Spire.Pdf.jar file under the lib folder into your project as a dependency.

## Encrypt PDF Documents using Java
A PDF document can be encrypted with two types of passwords: document open password and permissions password. 

**Document open password**
A document open password, also known as a user password, is required to open the PDF.

**Permissions password**
A permissions password, also known as a master or owner password, is used to change the permission settings of the PDF. This password allows you to restrict certain features in PDF, such as printing, editing, and copying content. 

If a PDF document is encrypted with both types of passwords, recipients can open the PDF with either password. However, if they need to change the restrictions you have set, they will need to enter the permissions password.

Spire.PDF enables developers to encrypt a PDF document with open password and permissions password by using the **PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize)** method. The following are the detail steps to implement this feature:

- Initialize an instance of **PdfDocument** class.
- Load the PDF file using **PdfDocument.loadFromFile()** method.
- Encrypt the PDF with open and permissions passwords using **PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize)** method.
- Save the resultant PDF using **PdfDocument.saveToFile()** method.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

import java.util.EnumSet;

public class Encryption {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the PDF document
        pdf.loadFromFile("Sample.pdf");

        //Encrypt the PDF with open and permissions passwords
        pdf.getSecurity().encrypt("openPassword", "permissionPassword", EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Copy_Content), PdfEncryptionKeySize.Key_256_Bit);

        //Save and close
        pdf.saveToFile("Encryption.pdf");
        pdf.close();
    }
}
```

## Decrypt PDF Documents using Java
You can decrypt an encrypted PDF document by setting its open password and permissions password as empty using the **PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword)** method, as shown in the steps below.

- Initialize an instance of **PdfDocument** class.
- Load the encrypted PDF with password using **PdfDocument.loadFromFile(filePath, password)** method.
- Decrypt the document by setting the open and permissions passwords as empty using **PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword)** method.
- Save the resultant PDF using **PdfDocument.saveToFile()** method.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class Decryption {
    public static void main(String []args) throws Exception {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the encrypted PDF with password
        pdf.loadFromFile("Encryption.pdf", "openPassword");

        //Decrypt the PDF by setting the open and permissions password as empty
        pdf.getSecurity().encrypt("", "", PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key_256_Bit, "permissionPassword");

        //Save and close
        pdf.saveToFile("Decryption.pdf");
        pdf.close();
    }
}
```