|
Hi,I'm trying to use SignedCms -object to sign messages. I 'm using certificate stored on smart card and don't want to prompt dialog for user to set PIN (because program will run on server). here's code I'm using: byte[] buffer = "test data to sign"; ContentInfo contentInfo = new ContentInfo(buffer); SignedCms signedCms = new SignedCms(contentInfo); CspParameters cspparams = new CspParameters(1,"Personal CSP"); System.Security.SecureString pwstr = new System.Security.SecureString(); pwstr.AppendChar('1'); pwstr.AppendChar('2'); pwstr.AppendChar('3'); pwstr.AppendChar('4'); pwstr.AppendChar('5'); cspparams.KeyPassword = pwstr; cspparams.Flags = CspProviderFlags.NoPrompt; CmsSigner cmsSigner = new CmsSigner(cspparams); cmsSigner.Certificate = m_X509Certificate;
// Sign the CMS/PKCS #7 message. try { signedCms.ComputeSignature(cmsSigner, true); } // Encode the CMS/PKCS #7 message. byte[] signature = signedCms.Encode();
Now the problem is that initializing of CmsSigner object throws an CryptographicException with message "bad key". Code works fine if I replace line CmsSigner cmsSigner = new CmsSigner(cspparams); with CmsSigner cmsSigner = new CmsSigner(m_X509Certificate).
Only then it prompts PIN dialog. I believe there must be easy solution for this, but just can't find it.. So I would like to know if my way is even correct way to try to set PIN programmatically?
Thanks in advance, -Juksa
ps. I know quite similar topics exists allready, but those didn't help me. |