@@ -27,6 +27,8 @@ This file is part of the iText (R) project.
27
27
import com .itextpdf .commons .bouncycastle .operator .AbstractOperatorCreationException ;
28
28
import com .itextpdf .commons .bouncycastle .pkcs .AbstractPKCSException ;
29
29
import com .itextpdf .io .font .constants .StandardFonts ;
30
+ import com .itextpdf .kernel .exceptions .KernelExceptionMessageConstant ;
31
+ import com .itextpdf .kernel .exceptions .PdfException ;
30
32
import com .itextpdf .kernel .font .PdfFontFactory ;
31
33
import com .itextpdf .kernel .logs .KernelLogMessageConstant ;
32
34
import com .itextpdf .kernel .pdf .CompressionConstants ;
@@ -82,6 +84,8 @@ public class PdfEncryptionManuallyPortedTest extends ExtendedITextTest {
82
84
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/crypto/PdfEncryptionManuallyPortedTest/" ;
83
85
84
86
public static final char [] PRIVATE_KEY_PASS = "testpassphrase" .toCharArray ();
87
+ // There is also test.pfx to add to Acrobat to be able to open result pdf files. Password for it is also
88
+ // testpassphrase
85
89
public static final String CERT = sourceFolder + "test.cer" ;
86
90
public static final String PRIVATE_KEY = sourceFolder + "test.pem" ;
87
91
@@ -180,6 +184,27 @@ public void encryptWithCertificateAes256NoCompression() throws IOException, Inte
180
184
encryptWithCertificate (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
181
185
}
182
186
187
+ @ Test
188
+ @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
189
+ ignore = true ))
190
+ public void openEncryptedDocWithWrongPrivateKey ()
191
+ throws IOException , GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
192
+ try (PdfReader reader = new PdfReader (sourceFolder + "encryptedWithCertificateAes128.pdf" ,
193
+ new ReaderProperties ()
194
+ .setPublicKeySecurityParams (
195
+ getPublicCertificate (CERT ),
196
+ PemFileHelper .readPrivateKeyFromPemFile (
197
+ new FileInputStream (sourceFolder + "wrong.pem" ), PRIVATE_KEY_PASS ),
198
+ FACTORY .getProviderName (),
199
+ null ))) {
200
+
201
+ Exception e = Assert .assertThrows (PdfException .class ,
202
+ () -> new PdfDocument (reader )
203
+ );
204
+ Assert .assertEquals (KernelExceptionMessageConstant .PDF_DECRYPTION , e .getMessage ());
205
+ }
206
+ }
207
+
183
208
public void encryptWithCertificate (String filename , int encryptionType , int compression ) throws IOException ,
184
209
InterruptedException , GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
185
210
ITextTest .removeCryptographyRestrictions ();
0 commit comments