Search in sources :

Example 6 with AccessPermission

use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.

the class TestPublicKeyEncryption method testMultipleRecipients.

/**
 * Protect the document for 2 recipients and try to open it.
 *
 * @throws Exception If there is an error during the test.
 */
@Test
public void testMultipleRecipients() throws Exception {
    PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
    policy.addRecipient(recipient1);
    policy.addRecipient(recipient2);
    policy.setEncryptionKeyLength(keyLength);
    document.protect(policy);
    // open first time
    File file = save("testMultipleRecipients");
    PDDocument encryptedDoc1 = reload(file, password1, getKeyStore(keyStore1));
    try {
        AccessPermission permission = encryptedDoc1.getCurrentAccessPermission();
        Assert.assertFalse(permission.canAssembleDocument());
        Assert.assertFalse(permission.canExtractContent());
        Assert.assertTrue(permission.canExtractForAccessibility());
        Assert.assertFalse(permission.canFillInForm());
        Assert.assertFalse(permission.canModify());
        Assert.assertFalse(permission.canModifyAnnotations());
        Assert.assertFalse(permission.canPrint());
        Assert.assertFalse(permission.canPrintDegraded());
    } finally {
        encryptedDoc1.close();
    }
    // open second time
    PDDocument encryptedDoc2 = reload(file, password2, getKeyStore(keyStore2));
    try {
        AccessPermission permission = encryptedDoc2.getCurrentAccessPermission();
        Assert.assertFalse(permission.canAssembleDocument());
        Assert.assertFalse(permission.canExtractContent());
        Assert.assertTrue(permission.canExtractForAccessibility());
        Assert.assertFalse(permission.canFillInForm());
        Assert.assertFalse(permission.canModify());
        Assert.assertFalse(permission.canModifyAnnotations());
        Assert.assertTrue(permission.canPrint());
        Assert.assertFalse(permission.canPrintDegraded());
    } finally {
        encryptedDoc2.close();
    }
}
Also used : PublicKeyProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) File(java.io.File) Test(org.junit.Test)

Example 7 with AccessPermission

use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.

the class TestPublicKeyEncryption method setUp.

/**
 * {@inheritDoc}
 */
@Before
public void setUp() throws Exception {
    if (Cipher.getMaxAllowedKeyLength("AES") != Integer.MAX_VALUE) {
        // we need strong encryption for these tests
        fail("JCE unlimited strength jurisdiction policy files are not installed");
    }
    testContext = InstrumentationRegistry.getInstrumentation().getContext();
    PDFBoxResourceLoader.init(testContext);
    testResultsDir = new File(testContext.getCacheDir(), "pdfbox-test-output/crypto");
    testResultsDir.mkdirs();
    permission1 = new AccessPermission();
    permission1.setCanAssembleDocument(false);
    permission1.setCanExtractContent(false);
    permission1.setCanExtractForAccessibility(true);
    permission1.setCanFillInForm(false);
    permission1.setCanModify(false);
    permission1.setCanModifyAnnotations(false);
    permission1.setCanPrint(false);
    permission1.setCanPrintDegraded(false);
    permission2 = new AccessPermission();
    permission2.setCanAssembleDocument(false);
    permission2.setCanExtractContent(false);
    permission2.setCanExtractForAccessibility(true);
    permission2.setCanFillInForm(false);
    permission2.setCanModify(false);
    permission2.setCanModifyAnnotations(false);
    // it is true now !
    permission2.setCanPrint(true);
    permission2.setCanPrintDegraded(false);
    recipient1 = getRecipient("test1.der", permission1);
    recipient2 = getRecipient("test2.der", permission2);
    password1 = "test1";
    password2 = "test2";
    keyStore1 = "test1.pfx";
    keyStore2 = "test2.pfx";
    document = PDDocument.load(testContext.getAssets().open(path + "test.pdf"));
    text = new PDFTextStripper().getText(document);
    producer = document.getDocumentInformation().getProducer();
    document.setVersion(1.7f);
}
Also used : AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) File(java.io.File) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper) Before(org.junit.Before)

Example 8 with AccessPermission

use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.

the class MainActivity method createEncryptedPdf.

/**
 * Creates a simple pdf and encrypts it
 */
public void createEncryptedPdf(View v) {
    String path = root.getAbsolutePath() + "/crypt.pdf";
    // 128 bit is the highest currently supported
    int keyLength = 128;
    // Limit permissions of those without the password
    AccessPermission ap = new AccessPermission();
    ap.setCanPrint(false);
    // Sets the owner password and user password
    StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "hi", ap);
    // Setups up the encryption parameters
    spp.setEncryptionKeyLength(keyLength);
    spp.setPermissions(ap);
    BouncyCastleProvider provider = new BouncyCastleProvider();
    Security.addProvider(provider);
    PDFont font = PDType1Font.HELVETICA;
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    try {
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        // Write Hello World in blue text
        contentStream.beginText();
        contentStream.setNonStrokingColor(15, 38, 192);
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(100, 700);
        contentStream.showText("Hello World");
        contentStream.endText();
        contentStream.close();
        // Save the final pdf document to a file
        // Apply the protections to the PDF
        document.protect(spp);
        document.save(path);
        document.close();
        tv.setText("Successfully wrote PDF to " + path);
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while creating PDF for encryption", e);
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) StandardProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) IOException(java.io.IOException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

AccessPermission (com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission)8 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)5 File (java.io.File)5 Test (org.junit.Test)4 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)2 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)2 PublicKeyProtectionPolicy (com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy)2 StandardProtectionPolicy (com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy)2 IOException (java.io.IOException)2 Before (org.junit.Before)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)1 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1