public void sign()
{
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(this.getCertificate().RawData) };
bool bHasPrivateKey;
bHasPrivateKey = this.getCertificate().HasPrivateKey;
if (!bHasPrivateKey)
{
MessageBox.Show("El certificado seleccionado no es válido para firmar digitalmente.");
return;
}
PdfReader reader = new PdfReader(this.getOriginalFileName());
FileStream signedFileStream = new FileStream(this.getSignedFileName(), FileMode.Create);
PdfStamper stp = PdfStamper.CreateSignature(reader, signedFileStream, '\0', null, true);
sap = stp.SignatureAppearance;
appearanceHandler.setAppearanceStyleTo(sap, appearanceStyle, new System.Drawing.Rectangle(100, 100, 200, 300));
sap.SetCrypto(null, chain, null, null);
sap.Location = PdfPKCS7.GetSubjectFields(chain[0]).GetField("L");
sap.Contact = this.contactInfo;
sap.Reason = this.getReason();
sap.Acro6Layers = true;
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
dic.Date = new PdfDate(sap.SignDate);
dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
if (sap.Reason != null)
dic.Reason = sap.Reason;
if (appearanceHandler.showLocation)
dic.Location = sap.Location;
if (appearanceHandler.showContact)
dic.Contact = sap.Contact;
sap.CryptoDictionary = dic;
int csize = 15000;
Hashtable exc = new Hashtable();
exc[PdfName.CONTENTS] = csize * 2 + 2;
sap.PreClose(exc);
try
{
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) this.certificate.PrivateKey;
SHA1Managed sha = new SHA1Managed();
byte[] hashedData = sha.ComputeHash(sap.RangeStream);
byte[] sig = rsa.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
bool verified = rsa.VerifyHash(hashedData, "SHA1", sig);
MessageBox.Show("Verified: " + verified);
byte[] outc = new byte[csize];
PdfDictionary dic2 = new PdfDictionary();
Array.Copy(sig, 0, outc, 0, sig.Length);
dic2.Put(PdfName.CONTENTS, new PdfString(outc).SetHexWriting(true));
sap.Close(dic2);
signedFileStream.Close();
}
catch (CryptographicException ce)
{
MessageBox.Show(ce.Message);
}
}
|