use of com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method testPDFBox4453.
/**
* PDFBOX-4453: verify that identical encrypted strings are really decrypted each.
*
* @throws IOException
*/
@Test
public void testPDFBox4453() throws IOException {
final int TESTCOUNT = 1000;
File file = new File(testResultsDir, "PDFBOX-4453.pdf");
PDDocument doc = new PDDocument();
doc.addPage(new PDPage());
for (int i = 0; i < TESTCOUNT; ++i) {
// strings must be in different dictionaries so that the actual
// encryption key changes
COSDictionary dict = new COSDictionary();
doc.getPage(0).getCOSObject().setItem(COSName.getPDFName("_Test-" + i), dict);
// need two different keys so that there are both encrypted and decrypted COSStrings
// with value "0"
dict.setString("key1", "3");
dict.setString("key2", "0");
}
// RC4-40
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", new AccessPermission());
spp.setEncryptionKeyLength(40);
spp.setPreferAES(false);
doc.protect(spp);
doc.save(file);
doc.close();
doc = PDDocument.load(file);
Assert.assertTrue(doc.isEncrypted());
for (int i = 0; i < TESTCOUNT; ++i) {
COSDictionary dict = doc.getPage(0).getCOSObject().getCOSDictionary(COSName.getPDFName("_Test-" + i));
assertEquals("3", dict.getString("key1"));
assertEquals("0", dict.getString("key2"));
}
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method encrypt.
// encrypt with keylength and permission, save, check sizes before and after encryption
// reopen, decrypt and return document
private PDDocument encrypt(int keyLength, boolean preferAES, int sizePriorToEncr, PDDocument doc, String prefix, AccessPermission permission, String userpassword, String ownerpassword) throws IOException {
StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerpassword, userpassword, permission);
spp.setEncryptionKeyLength(keyLength);
spp.setPreferAES(preferAES);
// This must have no effect and should only log a warning.
doc.setAllSecurityToBeRemoved(true);
doc.protect(spp);
File pdfFile = new File(testResultsDir, prefix + keyLength + "-bit-" + (preferAES ? "AES" : "RC4") + "-encrypted.pdf");
doc.save(pdfFile);
doc.close();
long sizeEncrypted = pdfFile.length();
Assert.assertTrue(keyLength + "-bit " + (preferAES ? "AES" : "RC4") + " encrypted pdf should not have same size as plain one", sizeEncrypted != sizePriorToEncr);
// test with owner password => full permissions
PDDocument encryptedDoc = PDDocument.load(pdfFile, ownerpassword);
Assert.assertTrue(encryptedDoc.isEncrypted());
Assert.assertTrue(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
// Older encryption allows to get the user password when the owner password is known
PDEncryption encryption = encryptedDoc.getEncryption();
int revision = encryption.getRevision();
if (revision < 5) {
StandardSecurityHandler standardSecurityHandler = new StandardSecurityHandler();
int keyLengthInBytes = encryption.getVersion() == 1 ? 5 : encryption.getLength() / 8;
byte[] computedUserPassword = standardSecurityHandler.getUserPassword(ownerpassword.getBytes(Charsets.ISO_8859_1), encryption.getOwnerKey(), revision, keyLengthInBytes);
assertEquals(userpassword.substring(0, 32), new String(computedUserPassword, Charsets.ISO_8859_1));
}
encryptedDoc.close();
// test with user password => restricted permissions
encryptedDoc = PDDocument.load(pdfFile, userpassword);
Assert.assertTrue(encryptedDoc.isEncrypted());
Assert.assertFalse(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
assertEquals(permission.getPermissionBytes(), encryptedDoc.getCurrentAccessPermission().getPermissionBytes());
return encryptedDoc;
}
use of com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy 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);
}
}
Aggregations