Reputation: 125
I have a signed PDF. The signature covers the entire documents and it's valid.
I want to extract the original pdf to compare its hash with that of the unsigned pdf.
I extract original pdf using the following code:
PdfReader reader = new PdfReader(FILESIGNED);
AcroFields acrofields = reader.getAcroFields();
//pdf have a unique signature
String signatureName = acrofields.getSignatureNames().get(0);
FileOutputStream os = new FileOutputStream(FILEORIGINAL);
InputStream ip = acrofields.extractRevision(signatureName);
int n = 0;
byte bb[] = new byte[1028];
while ((n = ip.read(bb)) > 0)
os.write(bb, 0, n);
os.close();
ip.close();
reader.close();
But the extracted pdf is not the same as the original. I would extract revision before signature? Is it possible?
Thanks for help. Sara
Upvotes: 4
Views: 2471
Reputation: 95918
I want to extract the original pdf to compare its hash with that of the unsigned pdf.
In general this is not possible.
When iText (or other PDF signing libraries or applications) sign a PDF, they:
Thus, in general the "original pdf" cannot be extracted anymore from the signed PDF file because the changes described above may have fundamentally changed the internal structure of the PDF.
There is one exception, though: If those changes were applied as an incremental update (in iText lingo: in append mode), it usually is possible to retrieve the original by cutting off that incremental update.
For this one merely has to search the latest end-of-file marker before the signature and cut off thereafter. (Actually there is a small amount of insecurity, a final end-of-line marker may or may not be part of the original PDF.)
Upvotes: 6