Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I encrypt a PDF using Java?

I have been trying to encrypt a PDF using Java. So far I can successfully encrypt other file types (.txt, .png, etc.). When I do it with PDF it breaks the info on in when I decrypt it.

This is what I'm using to encrypt it:

public byte[] cryptograph(Key key, byte[] content){
    Cipher cipher;
    byte[] cryptograph = null;
    try {
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cryptograph = cipher.doFinal(content);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cryptograph;

}

And this to decrypt it:

public byte[] decrypt(Key key,byte[] textCryp){
    Cipher cipher;
    byte[] decrypted = null;
    try {
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        decrypted = cipher.doFinal(textCryp);
    } catch (Exception e) {         
        e.printStackTrace();
    } 

    return decrypted;
}

Update:

This is what I use to read the files:

public byte[] getFile(){
    byte[] content = null;
    try {
        InputStream is = new FileInputStream("test.pdf");
        BufferedInputStream vf = new BufferedInputStream(is);
        content = new byte[vf.available()];
        vf.read(content);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return content;
}

Using this to rewrite the files

public static void saveDecrypt(byte[] bytes) throws IOException{
        Document doc=new Document();
        try {
            PdfWriter.getInstance(doc,new FileOutputStream("fileDecrypted.pdf"));
            doc.open(); 
            doc.add(new Paragraph(new String(bytes)));
            doc.close();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
like image 959
Gabriel Netto Avatar asked Nov 07 '12 13:11

Gabriel Netto


People also ask

How do I make a PDF encrypted?

Add a password to Adobe Acrobat (pdf)Open the PDF and choose Tools > Protect > Encrypt > Encrypt with Password. If you receive a prompt, click Yes to change the security. Select Require a Password to Open the Document, then type the password in the corresponding field.

How can I Encrypt a PDF for free?

In Preview, open the PDF file you want to encrypt. Select “File” and then “Export.” Click “Encrypt,” select a password, and then retype it to verify. Click “Save.”


1 Answers

Your saveDecrypt method seems to use iText as a PDF library. You don't need to do this, in fact you should not! You treat the PDF file simply as a series of bytes when reading, so you should do the exact same thing when writing.

Simply take the bytes you decrypted and write them to a file using a FileOutputStream!

like image 77
Joachim Sauer Avatar answered Sep 18 '22 02:09

Joachim Sauer