Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear sir i am using following code in our desktop application.




public static void signPdfFile (string sourceDocument, string destinationPath, Stream privateKeyStream, string keyPassword, string reason, string location)

{

Pkcs12Store pk12=new Pkcs12Store(privateKeyStream, keyPassword.ToCharArray());

privateKeyStream.Dispose();



//then Iterate throught certificate entries to find the private key entry

string alias=null;
foreach (string tAlias in pk12.Aliases)
{

if (pk12.IsKeyEntry(tAlias))

{

alias = tAlias;

break;

}

}

var pk=pk12.GetKey(alias).Key;



// reader and stamper

PdfReader reader = new PdfReader(sourceDocument);

using (FileStream fout = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite))

{

using (PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0'))

{

// appearance

PdfSignatureAppearance appearance = stamper.SignatureAppearance;

//appearance.Image = new iTextSharp.text.pdf.PdfImage();

appearance.Reason = reason;

appearance.Location = location;

appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, ), 1, "Icsi-Vendor");

// digital signature

IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");

MakeSignature.SignDetached(appearance, es, new X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);



stamper.Close();

}

}

}





public static void verifyPdfSignature (string pdfFile, Stream publicKeyStream)

{

var parser=new X509CertificateParser();

var certificate=parser.ReadCertificate(publicKeyStream);

publicKeyStream.Dispose();



PdfReader reader = new PdfReader(pdfFile);

AcroFields af = reader.AcroFields;

var names = af.GetSignatureNames();



if (names.Count == 0)

{

throw new InvalidOperationException("No Signature present in pdf file.");

}


foreach (string name in names)

{

if (!af.SignatureCoversWholeDocument(name))
{

throw new InvalidOperationException(string.Format("The signature: {0} does not covers the whole document.", name));

}



PdfPKCS7 pk = af.VerifySignature(name);

var cal = pk.SignDate;

var pkc = pk.Certificates;



if (!pk.Verify())

{

throw new InvalidOperationException("The signature could not be verified.");

}

if (!pk.VerifyTimestampImprint())

{

throw new InvalidOperationException("The signature timestamp could not be verified.");

}


Object[] fails = CertificateVerification.VerifyCertificates(pkc, new X509Certificate[] { certificate }, null, cal);

if (fails != null)

{

throw new InvalidOperationException("The file is not signed using the specified key-pair.");

}

}

}

but when i used it on Web application it have more error please try to solve it error or sent Another code for signing pdf using e_token .........
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900