pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scc lab <androids...@gmail.com>
Subject Issue in certificate based encryption
Date Thu, 11 Aug 2016 09:20:17 GMT
Hello PDFBox team,

I am writing this email to ask for guidance about the use of PDFBox sdk.

I have downloaded PDFBox sdk and tried some samples given with it. I am
interested in the encryption feature. I tried the password encryption and
its working fine. But when I try to do certificate based encryption, it
gives me run-time error. The exception I get is,

java.lang.RuntimeException: Could not find a suitable javax.crypto provider
at
org.apache.pdfbox.pdmodel.encryption.PublicKeySecurityHandler.createDERForRecipient(PublicKeySecurityHandler.java:419)
at
org.apache.pdfbox.pdmodel.encryption.PublicKeySecurityHandler.computeRecipientsField(PublicKeySecurityHandler.java:388)
at
org.apache.pdfbox.pdmodel.encryption.PublicKeySecurityHandler.prepareDocumentForEncryption(PublicKeySecurityHandler.java:322)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1277)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1095)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1067)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1055)
at createpdf.CreatePDF_1.main(CreatePDF_1.java:77)
Caused by: java.security.NoSuchAlgorithmException: 1.2.840.113549.3.2
KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
at
org.apache.pdfbox.pdmodel.encryption.PublicKeySecurityHandler.createDERForRecipient(PublicKeySecurityHandler.java:413)
... 8 more

And the code I have written is,


package createpdf;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import org.apache.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
import org.apache.pdfbox.pdmodel.encryption.PublicKeyRecipient;

import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class CreatePDF_1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String fileName = "EmptyPdf.pdf"; // name of our file
        try{
        PDDocument doc = new PDDocument(); // creating instance of pdfDoc

        doc.addPage(new PDPage()); // adding page in pdf doc file

        /////////////////////////
        AccessPermission ap = new AccessPermission();
        PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
        PublicKeyRecipient recip = new PublicKeyRecipient();
        recip.setPermission(ap);

        CertificateFactory cf = CertificateFactory.getInstance("X.509");

                  InputStream inStream = new FileInputStream("user1.cer");
                  X509Certificate certificate =
(X509Certificate)cf.generateCertificate(inStream);
                  inStream.close();



                  InputStream in = new FileInputStream("user1.cer");

 CertificateFactory cF = CertificateFactory.getInstance("X.509");

 X509Certificate cert = (X509Certificate)cF.generateCertificate(in);

 in.close();


 try{

                        recip.setX509(cert);
                        ppp.addRecipient(recip);
                       ppp.setEncryptionKeyLength(40);
                       doc.protect(ppp);

} catch (Exception e) {

     e.printStackTrace();

 }

        /////////////////////////

        try {

        doc.save(fileName); // saving as pdf file with name perm

        doc.close(); // cleaning memory

        } catch(Exception e) {
            e.printStackTrace();
        }
        }
        catch(Exception e){
        System.out.println(e.getMessage());
        }
    }
}

I have imported the jar file named pdfbox-app-2.0.2.jar as library. The
build environment is NetBeans IDE 8.1 with java version 8. Kindly guide me
about the issue.


Regards,

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message