if (PdfName.V2.equals(dic.get(PdfName.CFM)))
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_128;
else if (PdfName.AESV2.equals(dic.get(PdfName.CFM)))
cryptoMode = PdfWriter.ENCRYPTION_AES_128;
else
throw new UnsupportedPdfException(MessageLocalization.getComposedMessage("no.compatible.encryption.found"));
PdfObject em = enc.get(PdfName.ENCRYPTMETADATA);
if (em != null && em.toString().equals("false"))
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
break;
case 5:
cryptoMode = PdfWriter.ENCRYPTION_AES_256;
PdfObject em5 = enc.get(PdfName.ENCRYPTMETADATA);
if (em5 != null && em5.toString().equals("false"))
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
break;
default:
throw new UnsupportedPdfException(MessageLocalization.getComposedMessage("unknown.encryption.type.r.eq.1", rValue));
}
}
else if (filter.equals(PdfName.PUBSEC)) {
boolean foundRecipient = false;
byte[] envelopedData = null;
PdfArray recipients = null;
o = enc.get(PdfName.V);
if (!o.isNumber())
throw new InvalidPdfException(MessageLocalization.getComposedMessage("illegal.v.value"));
int vValue = ((PdfNumber)o).intValue();
switch(vValue) {
case 1:
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_40;
lengthValue = 40;
recipients = (PdfArray)enc.get(PdfName.RECIPIENTS);
break;
case 2:
o = enc.get(PdfName.LENGTH);
if (!o.isNumber())
throw new InvalidPdfException(MessageLocalization.getComposedMessage("illegal.length.value"));
lengthValue = ( (PdfNumber) o).intValue();
if (lengthValue > 128 || lengthValue < 40 || lengthValue % 8 != 0)
throw new InvalidPdfException(MessageLocalization.getComposedMessage("illegal.length.value"));
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_128;
recipients = (PdfArray)enc.get(PdfName.RECIPIENTS);
break;
case 4:
PdfDictionary dic = (PdfDictionary)enc.get(PdfName.CF);
if (dic == null)
throw new InvalidPdfException(MessageLocalization.getComposedMessage("cf.not.found.encryption"));
dic = (PdfDictionary)dic.get(PdfName.DEFAULTCRYPTFILTER);
if (dic == null)
throw new InvalidPdfException(MessageLocalization.getComposedMessage("defaultcryptfilter.not.found.encryption"));
if (PdfName.V2.equals(dic.get(PdfName.CFM))) {
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_128;
lengthValue = 128;
}
else if (PdfName.AESV2.equals(dic.get(PdfName.CFM))) {
cryptoMode = PdfWriter.ENCRYPTION_AES_128;
lengthValue = 128;
}
else
throw new UnsupportedPdfException(MessageLocalization.getComposedMessage("no.compatible.encryption.found"));
PdfObject em = dic.get(PdfName.ENCRYPTMETADATA);
if (em != null && em.toString().equals("false"))
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
recipients = (PdfArray)dic.get(PdfName.RECIPIENTS);
break;
default:
throw new UnsupportedPdfException(MessageLocalization.getComposedMessage("unknown.encryption.type.v.eq.1", rValue));
}
for (int i = 0; i<recipients.size(); i++) {
PdfObject recipient = recipients.getPdfObject(i);
strings.remove(recipient);
CMSEnvelopedData data = null;
try {
data = new CMSEnvelopedData(recipient.getBytes());
Iterator<RecipientInformation> recipientCertificatesIt = data.getRecipientInfos().getRecipients().iterator();
while (recipientCertificatesIt.hasNext()) {
RecipientInformation recipientInfo = recipientCertificatesIt.next();
if (recipientInfo.getRID().match(certificate) && !foundRecipient) {
envelopedData = PdfEncryptor.getContent(recipientInfo, (PrivateKey)certificateKey, certificateKeyProvider);
foundRecipient = true;
}
}
}
catch (Exception f) {
throw new ExceptionConverter(f);
}
}
if(!foundRecipient || envelopedData == null) {
throw new UnsupportedPdfException(MessageLocalization.getComposedMessage("bad.certificate.and.key"));
}
MessageDigest md = null;
try {