Java encryption and decryption PDF

This article will show you how to set protection for PDF documents in your Java program. We can display the password to view the document. That is, you can set a password for editing a document that cannot be edited by simply reading the document through this password.

Tools used: Free Spire.PDF for Java V2.0.0

[Example 1] Encrypt a PDF document

import java.util.EnumSet;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class EncryptPDF {
    public static void main(String[] args) {
        //PdfDocument example to create
        PdfDocument doc = new PdfDocument();
        //Load the PDF file
        doc.loadFromFile("sample.pdf");

        //Encrypt PDF files
        PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
        
        //When you open a document, you just look at it.
        String openPassword = "123456";

        //If you open the document, you can edit it.
        String permissionPassword = "test"; 
        EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
        doc.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize);

        //Save the file
        doc.saveToFile("Encrypt.pdf");
        doc.close();             
    }
}

After completing the code, run the program and generate the documentation. When you open a document, you can enter the password to find out the specific status of the document's encryption. As below:

Encrypted.png

[Example 2] Unprotect PDF password

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class DecryptPDF {

    public static void main(String[] args) throws Exception {
        
        //PdfDocument example to create
        PdfDocument doc = new PdfDocument();
        //Load password protected PDF file
        doc.loadFromFile("Encrypt.pdf", "123456");

        //Unprotect passwords in documents
        doc.getSecurity().encrypt("", "", PdfPermissionsFlags.getDefaultPermissions(), PdfEncryptionKeySize.Key_256_Bit, "test");
        
        //Save the file
        doc.saveToFile("Decrypt.pdf");
        doc.close();
    }
}

After running the program, the generated document is no longer password protected.

Recommended Posts

Java encryption and decryption PDF
KMS) Envelope encryption with openssl and java decryption
RSA encryption / decryption with java 8
Output PDF and TIFF with Java 8
Java encryption, decryption of Word documents
Java and JavaScript
XXE and Java
Program PDF headers and footers in Java
Create barcodes and QR codes in Java PDF
RSA key pair creation / encryption / decryption sample (JAVA)
Java enables extraction of PDF text and images
[Java] Convert PDF version
Java compressed PDF document
Java true and false
[Java] String comparison and && and ||
[Java] PDF viewing settings
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Study Java # 2 (\ mark and operator)
RSA key pair creation / encryption / decryption sample (JAVA)
Java encryption and decryption PDF
RSA encryption / decryption with java 8
Java version 8 and later features
Java adds table to PDF
Save Java PDF in Excel
[Java] Difference between == and equals
[Java] Generics classes and generics methods
Java programming (variables and data)
Java and Iterator Part 1 External Iterator
Java if and switch statements
Paging PDF with Java + PDFBox.jar
Java class definition and instantiation
Java turns Excel into PDF
Save Java HTML as PDF
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters
I compared PHP and Java constructors
Differences between "beginner" Java and Kotlin
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java variable declaration, initialization, and types
Java Excel Insertion and Image Extraction
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes