Gabriel Netto
Gabriel Netto

Reputation: 1828

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();
        }
    }

Upvotes: 6

Views: 4938

Answers (2)

Joachim Sauer
Joachim Sauer

Reputation: 308279

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!

Upvotes: 5

Duncan Jones
Duncan Jones

Reputation: 69410

I'm concerned your file reading code may be to blame. The InputStream.available() method only returns an estimate of the number of bytes that can be read. I would suggest you Google alternative methods for reading an entire file to a byte array or consider using a library method such as Apache Commons FileUtils.readFileToByteArray(File file) or IOUtils.toByteArray(InputStream input).

As a secondary check, I would recommend you perform a byte array comparison of your file contents before encryption and after decryption. I suspect they will be identical (further indicating that the file reading and/or writing is to blame).

Upvotes: 4

Related Questions