AndreiGosman
2008-11-12 16:33:40 UTC
Hi,
I was a very big problem using iTextSharp and Aladdin eToken for signing PDF
files. After I sign a pdf, Acrobat Reader gives the following message:
"The document has been altered or corrupted since teh Certification was
applied"
Here is my code:
Does someone know what is wrong?
X509Certificate2 card = GetCertificate(certSerial);
Org.BouncyCastle.X509.X509CertificateParser cp = new
Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new
Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.RawData)
};
PdfReader reader = new PdfReader(fileToSign,
Encoding.ASCII.GetBytes(ownerPassword));
PdfStamper stp = PdfStamper.CreateSignature(reader, new
FileStream(signedFileName, FileMode.Create), '\0', null, true);
PdfSignatureAppearance sap = stp.SignatureAppearance;
sap.SignDate = signDate;
sap.SetCrypto(null, chain, null,
PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = reason;
sap.Location = location;
sap.Acro6Layers = true;
sap.Render =
PdfSignatureAppearance.SignatureRender.GraphicAndDescription;
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100,
250, 150), 1, null);
sap.Render =
PdfSignatureAppearance.SignatureRender.NameAndDescription;
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
PdfName.ADBE_PKCS7_DETACHED);
dic.Date = new PdfDate(sap.SignDate);
dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
if (sap.Reason != null)
dic.Reason = sap.Reason;
if (sap.Location != null)
dic.Location = sap.Location;
sap.CryptoDictionary = dic;
int csize = 4000;
Hashtable exc = new Hashtable();
exc[PdfName.CONTENTS] = csize * 2 +2;
sap.PreClose(exc);
//sap.PreClose();
HashAlgorithm sha = new SHA1CryptoServiceProvider();
Stream s = sap.RangeStream;
int read = 0;
byte[] buff = new byte[8192];
while ((read = s.Read(buff, 0, 8192)) > 0)
{
sha.TransformBlock(buff, 0, read, buff, 0);
}
sha.TransformFinalBlock(buff, 0, 0);
byte[] pk = SignMsg(sha.Hash, card,false);
byte[] outc = new byte[csize];
PdfDictionary dic2 = new PdfDictionary();
try
{
Array.Copy(pk, 0, outc, 0, pk.Length);
dic2.Put(PdfName.CONTENTS, new
PdfString(outc).SetHexWriting(true));
}
finally
{
sap.Close(dic2);
}
Regards
Andrei Gosman
I was a very big problem using iTextSharp and Aladdin eToken for signing PDF
files. After I sign a pdf, Acrobat Reader gives the following message:
"The document has been altered or corrupted since teh Certification was
applied"
Here is my code:
Does someone know what is wrong?
X509Certificate2 card = GetCertificate(certSerial);
Org.BouncyCastle.X509.X509CertificateParser cp = new
Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new
Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.RawData)
};
PdfReader reader = new PdfReader(fileToSign,
Encoding.ASCII.GetBytes(ownerPassword));
PdfStamper stp = PdfStamper.CreateSignature(reader, new
FileStream(signedFileName, FileMode.Create), '\0', null, true);
PdfSignatureAppearance sap = stp.SignatureAppearance;
sap.SignDate = signDate;
sap.SetCrypto(null, chain, null,
PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = reason;
sap.Location = location;
sap.Acro6Layers = true;
sap.Render =
PdfSignatureAppearance.SignatureRender.GraphicAndDescription;
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100,
250, 150), 1, null);
sap.Render =
PdfSignatureAppearance.SignatureRender.NameAndDescription;
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
PdfName.ADBE_PKCS7_DETACHED);
dic.Date = new PdfDate(sap.SignDate);
dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
if (sap.Reason != null)
dic.Reason = sap.Reason;
if (sap.Location != null)
dic.Location = sap.Location;
sap.CryptoDictionary = dic;
int csize = 4000;
Hashtable exc = new Hashtable();
exc[PdfName.CONTENTS] = csize * 2 +2;
sap.PreClose(exc);
//sap.PreClose();
HashAlgorithm sha = new SHA1CryptoServiceProvider();
Stream s = sap.RangeStream;
int read = 0;
byte[] buff = new byte[8192];
while ((read = s.Read(buff, 0, 8192)) > 0)
{
sha.TransformBlock(buff, 0, read, buff, 0);
}
sha.TransformFinalBlock(buff, 0, 0);
byte[] pk = SignMsg(sha.Hash, card,false);
byte[] outc = new byte[csize];
PdfDictionary dic2 = new PdfDictionary();
try
{
Array.Copy(pk, 0, outc, 0, pk.Length);
dic2.Put(PdfName.CONTENTS, new
PdfString(outc).SetHexWriting(true));
}
finally
{
sap.Close(dic2);
}
Regards
Andrei Gosman
--
View this message in context: http://www.nabble.com/PDFSignature---%22Document-has-been-altered-since-signature-was-applied%22-tp20463953p20463953.html
Sent from the iText - General mailing list archive at Nabble.com.
View this message in context: http://www.nabble.com/PDFSignature---%22Document-has-been-altered-since-signature-was-applied%22-tp20463953p20463953.html
Sent from the iText - General mailing list archive at Nabble.com.