I have a PDF document that needs to be both digitally signed
and encrypted
.
I am using ABCPDF
and when I apply the digital signature to a document that is encrypted the signature gets invalidated.
The error that is provided by Adobe Acrobat Reader
is: "There have been changes made to this document that invalidate the signature"
Source code:
using (Doc doc = new Doc())
{
doc.Read(pdfPath);
if (options.Encrypt)
{
doc.Encryption.Type = 4;
doc.Encryption.SetCryptMethods(CryptMethodType.AESV3);
doc.Encryption.Password = Encryption.Decrypt(options.UserPassword, PdfSecurityOptions.EncryptionPassword);
doc.Encryption.OwnerPassword = Encryption.Decrypt(options.OwnerPassword, PdfSecurityOptions.EncryptionPassword);
}
if (options.Sign)
{
byte[] bytes = Convert.FromBase64String(options.Certificate);
X509Certificate2 certificate = new X509Certificate2(bytes, Encryption.Decrypt(options.CertificatePassword, PdfSecurityOptions.EncryptionPassword), X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
PdfUtils.DigitallySign(
doc,
options.SignatureText,
options.Rectangle,
certificate
);
}
doc.Save(savePath);
}
I have tried:
- Apply the encryption
before
the signing - Apply the encryption
after
the signing - Apply the encryption, save the document and then load and sign it