Search in sources :

Example 1 with StandardDecryptionMaterial

use of org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial in project camel by apache.

the class PdfAppendTest method testAppendEncrypted.

@Test
public void testAppendEncrypted() throws Exception {
    final String originalText = "Test";
    final String textToAppend = "Append";
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setFont(PDType1Font.HELVETICA, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(20, 400);
    contentStream.drawString(originalText);
    contentStream.endText();
    contentStream.close();
    final String ownerPass = "ownerPass";
    final String userPass = "userPass";
    AccessPermission accessPermission = new AccessPermission();
    accessPermission.setCanExtractContent(false);
    StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
    protectionPolicy.setEncryptionKeyLength(128);
    document.protect(protectionPolicy);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    document.save(output);
    // Encryption happens after saving.
    PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray()));
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, encryptedDocument);
    headers.put(PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME, new StandardDecryptionMaterial(userPass));
    template.sendBodyAndHeaders("direct:start", textToAppend, headers);
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody();
            assertThat(body, instanceOf(ByteArrayOutputStream.class));
            try {
                PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                String text = pdfTextStripper.getText(doc);
                assertEquals(2, doc.getNumberOfPages());
                assertThat(text, containsString(originalText));
                assertThat(text, containsString(textToAppend));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
}
Also used : PDPage(org.apache.pdfbox.pdmodel.PDPage) HashMap(java.util.HashMap) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Predicate(org.apache.camel.Predicate) Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDPageContentStream(org.apache.pdfbox.pdmodel.edit.PDPageContentStream) PDFTextStripper(org.apache.pdfbox.util.PDFTextStripper) Test(org.junit.Test)

Example 2 with StandardDecryptionMaterial

use of org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial in project camel by apache.

the class PdfTextExtractionTest method testExtractTextFromEncrypted.

@Test
public void testExtractTextFromEncrypted() throws Exception {
    final String ownerPass = "ownerPass";
    final String userPass = "userPass";
    AccessPermission accessPermission = new AccessPermission();
    accessPermission.setCanExtractContent(false);
    StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
    protectionPolicy.setEncryptionKeyLength(128);
    PDDocument document = new PDDocument();
    final String expectedText = "Test string";
    PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setFont(PDType1Font.HELVETICA, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(20, 400);
    contentStream.drawString(expectedText);
    contentStream.endText();
    contentStream.close();
    document.protect(protectionPolicy);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    document.save(output);
    // Encryption happens after saving.
    PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray()));
    template.sendBodyAndHeader("direct:start", encryptedDocument, PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME, new StandardDecryptionMaterial(userPass));
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody();
            assertThat(body, instanceOf(String.class));
            assertThat((String) body, containsString(expectedText));
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
    document.isEncrypted();
}
Also used : PDPage(org.apache.pdfbox.pdmodel.PDPage) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Predicate(org.apache.camel.Predicate) Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDPageContentStream(org.apache.pdfbox.pdmodel.edit.PDPageContentStream) Test(org.junit.Test)

Example 3 with StandardDecryptionMaterial

use of org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial in project camel by apache.

the class FopHelper method decryptPDFN.

//decryption requires additional libraries
public static void decryptPDFN(PDDocument document, String password) throws IOException, CryptographyException, BadSecurityHandlerException {
    if (document.isEncrypted()) {
        DecryptionMaterial decryptionMaterial = new StandardDecryptionMaterial(password);
        document.openProtection(decryptionMaterial);
    } else {
        throw new RuntimeException("Document not encrypted");
    }
}
Also used : StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) DecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial) StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial)

Example 4 with StandardDecryptionMaterial

use of org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial in project pdfbox by apache.

the class PDFParser method prepareDecryption.

/**
 * Prepare for decryption.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException if something went wrong
 */
private void prepareDecryption() throws InvalidPasswordException, IOException {
    COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
    if (trailerEncryptItem != null && !(trailerEncryptItem instanceof COSNull)) {
        if (trailerEncryptItem instanceof COSObject) {
            COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
            parseDictionaryRecursive(trailerEncryptObj);
        }
        try {
            encryption = new PDEncryption(document.getEncryptionDictionary());
            DecryptionMaterial decryptionMaterial;
            if (keyStoreInputStream != null) {
                KeyStore ks = KeyStore.getInstance("PKCS12");
                ks.load(keyStoreInputStream, password.toCharArray());
                decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
            } else {
                decryptionMaterial = new StandardDecryptionMaterial(password);
            }
            securityHandler = encryption.getSecurityHandler();
            securityHandler.prepareForDecryption(encryption, document.getDocumentID(), decryptionMaterial);
            accessPermission = securityHandler.getCurrentAccessPermission();
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new IOException("Error (" + e.getClass().getSimpleName() + ") while creating security handler for decryption", e);
        }
    }
}
Also used : DecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial) StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) PublicKeyDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) COSObject(org.apache.pdfbox.cos.COSObject) COSBase(org.apache.pdfbox.cos.COSBase) COSNull(org.apache.pdfbox.cos.COSNull) PublicKeyDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) PDEncryption(org.apache.pdfbox.pdmodel.encryption.PDEncryption) IOException(java.io.IOException) KeyStore(java.security.KeyStore) IOException(java.io.IOException) InvalidPasswordException(org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException)

Aggregations

StandardDecryptionMaterial (org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Exchange (org.apache.camel.Exchange)2 Predicate (org.apache.camel.Predicate)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDPageContentStream (org.apache.pdfbox.pdmodel.edit.PDPageContentStream)2 AccessPermission (org.apache.pdfbox.pdmodel.encryption.AccessPermission)2 DecryptionMaterial (org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial)2 StandardProtectionPolicy (org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 KeyStore (java.security.KeyStore)1 HashMap (java.util.HashMap)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSNull (org.apache.pdfbox.cos.COSNull)1 COSObject (org.apache.pdfbox.cos.COSObject)1 InvalidPasswordException (org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException)1