iText pdf library
Website search

Certificate encryption

EncryptWithCertificate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written by Bruno Lowagie.
 */
package com.itextpdf.samples.sandbox.security;
 
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
public class EncryptWithCertificate {
    public static final String DEST
            = "./target/sandbox/security/encrypt_with_certificate.pdf";
    public static final String SRC
            = "./src/test/resources/pdfs/hello.pdf";
    public static final String PUBLIC
            = "./src/test/resources/encryption/test.cer";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new EncryptWithCertificate().manipulatePdf(DEST);
    }
 
    public Certificate getPublicCertificate(String path) throws IOException, CertificateException {
        try (FileInputStream is = new FileInputStream(path)) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
            return cert;
        }
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
 
        // The file created by this example can not be opened, unless
        // you import the private key stored in test.p12 in your certificate store.
        // The password for the p12 file is kspass.
        Certificate cert = getPublicCertificate(PUBLIC);
 
        PdfDocument pdfDoc = new PdfDocument(
                new PdfReader(SRC),
                new PdfWriter(dest, new WriterProperties().setPublicKeyEncryption(
                        new Certificate[] {cert},
                        new int[] {EncryptionConstants.ALLOW_PRINTING},
                        // Due to import control restrictions by the governments of a few countries,
                        // the encryption libraries shipped by default with the Java SDK restrict
                        // the length, and as a result the strength, of encryption keys. Be aware
                        // that in this sample you need to replace the default security JARs in your
                        // Java installation with the Java Cryptography Extension (JCE) Unlimited
                        // Strength Jurisdiction Policy Files. These JARs are available for download
                        // from http://java.oracle.com/ in eligible countries.
                        EncryptionConstants.ENCRYPTION_AES_256))
        );
        pdfDoc.close();
 
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now