Certificate encryption

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

EncryptWithCertificate.java
  1. /*
  2.  * This example was written by Bruno Lowagie.
  3.  */
  4. package sandbox.security;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Paragraph;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10.  
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.security.Security;
  17. import java.security.cert.Certificate;
  18. import java.security.cert.CertificateException;
  19. import java.security.cert.CertificateFactory;
  20. import java.security.cert.X509Certificate;
  21.  
  22. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  23.  
  24. /**
  25.  * The file created using this example can not be opened, unless
  26.  * you import the private key stored in test.p12 in your certificate store.
  27.  * The password for the p12 file is kspass.
  28.  */
  29. public class EncryptWithCertificate {
  30.    
  31.     public static final String PUBLIC = "resources/misc/test.cer";
  32.     public static final String DEST = "results/security/encrypted.pdf";
  33.    
  34.     public static void main(String[] args) throws IOException, DocumentException, CertificateException, NoSuchAlgorithmException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new EncryptWithCertificate().createPdf(DEST);
  38.     }
  39.    
  40.     public void createPdf(String dest) throws IOException, DocumentException, CertificateException, NoSuchAlgorithmException {    
  41.  
  42.         Security.addProvider(new BouncyCastleProvider());
  43.        
  44.         // step 1
  45.         Document document = new Document();
  46.         // step 2
  47.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  48.  
  49.         Certificate cert = getPublicCertificate(PUBLIC);
  50.         writer.setEncryption(
  51.                 new Certificate[]{cert},
  52.                 new int[]{PdfWriter.ALLOW_PRINTING},
  53.                 PdfWriter.ENCRYPTION_AES_256);
  54.  
  55.         // step 3
  56.         document.open();
  57.         // step 4
  58.         document.add(new Paragraph("My secret hello"));
  59.         // step 5
  60.         document.close();
  61.     }
  62.    
  63.     public Certificate getPublicCertificate(String path)
  64.         throws IOException, CertificateException {
  65.         FileInputStream is = new FileInputStream(path);
  66.         CertificateFactory cf = CertificateFactory.getInstance("X.509");
  67.         X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
  68.         return cert;
  69.     }
  70. }

Resources

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