c# - How do I sign a PDF document using a certificate from the Windows Cert Store? -



c# - How do I sign a PDF document using a certificate from the Windows Cert Store? -

i need sign pdf document using certificate exists in windows certificate store. have been digging around day trying figure out, , so close yet so far away.

all missing this: how iexternalsignature object sign pdf file with?

rahul singla has written beautiful illustration of how sign pdf document using new itext 5.3.0 api - as long as can access .pfx file sitting around on pc somewhere.

there a previous question on signing using certificate windows cert store, except using version of api setcrypto still exists, , signature apparently optional. in itext 5.3.0, api has changed, , setcrypto no longer thing.

here's have far (comments added posterity, since might finish , recent version of how on 'net):

class="lang-cs prettyprint-override">using itextsharp.text.pdf; using itextsharp.text.pdf.security; using bcx509 = org.bouncycastle.x509; using org.bouncycastle.pkcs; using org.bouncycastle.crypto; using dotnetutils = org.bouncycastle.security.dotnetutilities; ... // set pdf io pdfreader reader = new pdfreader(@"some\dir\sometemplate.pdf"); pdfstamper stamper = pdfstamper.createsignature(reader, new filestream(@"some\dir\signedpdf.pdf", filemode.create), '\0'); pdfsignatureappearance sap = stamper.signatureappearance; sap.reason = "for no apparent raisin"; sap.location = "..."; // acquire certificate chain var certstore = new x509store(storename.my, storelocation.localmachine); certstore.open(openflags.readonly); x509certificatecollection certcollection = certstore.certificates.find(x509findtype.findbysubjectname, "my.cert.subject", true); x509certificate cert = certcollection[0]; // itextsharp needs cert bouncycastle x509 object; converts it. bcx509.x509certificate bccert = dotnetutils.fromx509certificate(cert); var chain = new list<bcx509.x509certificate> { bccert }; certstore.close(); // ok, that's certificate chain done. how pks? iexternalsignature signature = null; /* ??? */ // sign pdf file , finish up. makesignature.signdetached(sap, signature, chain, // of import stuff null, null, null, 0, cryptostandard.cms); stamper.close();

as can see: i have signature, , i'm stumped how should obtain it!

x509certificate cert = certcollection[0]; // code x509certificate2 signaturecert = new x509certificate2(cert); var pk = org.bouncycastle.security.dotnetutilities.getkeypair(signaturecert.privatekey).private;

if have pk, can above, create iexternalsignature follows:

iexternalsignature es = new privatekeysignature(pk, "sha-256");

you may find next articles of use:

https://www.simple-talk.com/dotnet/.net-framework/beginning-with-digital-signatures-in-.net-framework/ http://msdn.microsoft.com/en-us/library/ms223098.aspx

c# pdf certificate itextsharp signing

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -