Skip to content

Commit 8b5d949

Browse files
committed
Fix bytes retrieval from pdf byte string during PdfEncryption init
DEVSIX-526
1 parent f0e758a commit 8b5d949

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ protected void initCryptoIfSpecified(PdfVersion version) {
277277
EncryptionProperties encryptProps = properties.encryptionProperties;
278278
if (properties.isStandardEncryptionUsed()) {
279279
crypto = new PdfEncryption(encryptProps.userPassword, encryptProps.ownerPassword, encryptProps.standardEncryptPermissions,
280-
encryptProps.encryptionAlgorithm, ByteUtils.getIsoBytes(this.document.getOriginalDocumentId().toString()), version);
280+
encryptProps.encryptionAlgorithm, ByteUtils.getIsoBytes(this.document.getOriginalDocumentId().getValue()), version);
281281
} else if (properties.isPublicKeyEncryptionUsed()) {
282282
crypto = new PdfEncryption(encryptProps.publicCertificates,
283283
encryptProps.publicKeyEncryptPermissions, encryptProps.encryptionAlgorithm, version);

kernel/src/test/java/com/itextpdf/kernel/crypto/PdfEncryptionTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,28 @@ public void encryptAes256Pdf2Permissions() throws InterruptedException, IOExcept
563563
compareEncryptedPdf(filename);
564564
}
565565

566+
@Test
567+
public void encryptWithPasswordAes128NoMetadataCompression() throws Exception {
568+
String srcFilename = "srcEncryptWithPasswordAes128NoMetadataCompression.pdf";
569+
PdfReader reader = new PdfReader(sourceFolder + srcFilename, new ReaderProperties());
570+
WriterProperties props = new WriterProperties()
571+
.setStandardEncryption("superuser".getBytes(), "superowner".getBytes(), EncryptionConstants.ALLOW_PRINTING,
572+
EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
573+
String outFilename = "encryptWithPasswordAes128NoMetadataCompression.pdf";
574+
PdfWriter writer = new PdfWriter(destinationFolder + outFilename, props);
575+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
576+
pdfDoc.close();
577+
578+
CompareTool compareTool = new CompareTool();
579+
compareTool.enableEncryptionCompare();
580+
compareTool.getOutReaderProperties().setPassword("superowner".getBytes());
581+
compareTool.getCmpReaderProperties().setPassword("superowner".getBytes());
582+
String outPdf = destinationFolder + outFilename;
583+
String cmpPdf = sourceFolder + "cmp_" + outFilename;
584+
Assert.assertNull(compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_"));
585+
}
586+
587+
566588
public void encryptWithPassword2(String filename, int encryptionType, int compression) throws XMPException, IOException, InterruptedException {
567589
encryptWithPassword2(filename, encryptionType, compression, false);
568590
}

0 commit comments

Comments
 (0)