0

I tried to sign pdf using itextshapr 4.1.6 in c# as per way provided in https://web.archive.org/web/20130329022247/http://itextpdf.sourceforge.net/howtosign.html

I followed the steps given in link: Digitally sign a PDF using iTextSharp 4.1.6.0

Section : "How to sign with an external signature dictionary using BouncyCastle CMS"

CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
generator.addSigner(key, (X509Certificate)chain[0], CMSSignedDataGenerator.DIGEST_SHA1);

I am getting below exception while getting privatekey:

System.Security.Cryptography.CryptographicException
HResult=0x8009000B
Message=Key not valid for use in specified state.

Source=mscorlib
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils._ExportKey(SafeKeyHandle hKey, Int32 blobType, Object cspObject)
at System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(Boolean includePrivateParameters)
at Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(AsymmetricAlgorithm privateKey)
at ITextSharpDemo.IItextSharp416Tester.Main(String[] args) in D:\Projects\IAS\trunk\SourceCode\Demo\ITextSharpDemo4.1.6\ITextSharpDemo\IItextSharp416Tester.cs:line 70

It requires certificate to be installed with keys exportable. We are able to sign pdf after reinstalling certificate with keys exportable.

What to do if certificate is not installed with keys exportable? We need to work with itextsharp 4.1.6 version.

  • I am not too familiar with c# but you can try to load the key like by setting the `X509KeyStorageFlags.Exportable` So e.g. `X509Certificate2 x = new X509Certificate2(@"C:\temp\certs\1\test.pfx", "test", X509KeyStorageFlags.Exportable);` – Lonzak Apr 13 '21 at 19:33
  • As all you show is some BouncyCastle related code, you might want to tag your question accordingly. If you want help from the people following the [tag:iText] tag, consider clarifying and adding your pivotal iText related code. – mkl Apr 22 '21 at 09:03

0 Answers0