DigitalSign with Smart Card (SHA256 and CADES)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,
I Have any problem with Digital Sign and SmartCard Certificate in C#.Net (<span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px OS: Vista Sp1 32bit)
Recently was changed parameters in Digital Sign, and now, i have to realize this with a SHA256 to encrypt and CADES.
Here my code:
//Choose the Certificate in MY Store
<span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px
<p style="
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; static X509Certificate2 GetCertificate()

{

X509Store st = <span style="color:Blue; new X509Store(StoreName.My, StoreLocation.CurrentUser);

st.Open(OpenFlags.ReadOnly);

X509Certificate2Collection col = st.Certificates.Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.NonRepudiation, <span style="color:Blue; true);



X509Certificate2 card = <span style="color:Blue; null;

X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, <span style="color:#A31515; "Certificates", <span style="color:#A31515; "Select one to sign", X509SelectionFlag.SingleSelection);

<span style="color:Blue; if (sel.Count > 0)

{

X509Certificate2Enumerator en = sel.GetEnumerator();

en.MoveNext();

card = en.Current;

}

st.Close();

<span style="color:Blue; return card;

}
[/code]

<p style=" This certificate have a private key (cert.hasPrivateKey is true) but cert.privateKey give me a error: "Bad Tipe". I dont say why!!
<p style=" Then i realized a signature :
<p style="
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; byte[] Sign(<span style="color:Blue; byte[] data, X509Certificate2 certificate)

{

CmsSigner cmsSigner = <span style="color:Blue; new CmsSigner(certificate);

cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;

cmsSigner.DigestAlgorithm = <span style="color:Blue; new Oid(<span style="color:#A31515; "2.16.840.1.101.3.4.2.1"); <span style="color:Green; //OID SHA256

cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;

SignedCms signedCms = <span style="color:Blue; new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber, <span style="color:Blue; new ContentInfo(<span style="color:Blue; new Oid(<span style="color:#A31515; "1.2.840.113549.1.7.2"), data), <span style="color:Blue; true); <span style="color:Green; //OID SignedData (CADES)

signedCms.ComputeSignature(cmsSigner, <span style="color:Blue; false);

<span style="color:Blue; return signedCms.Encode();

}
[/code]

<p style=" When i call signedCms.ComputeSignature(cmsSigner, false); give me an error : "<span style="font-family:arial,sans-serif; line-height:15px; font-size:small; color:#000000 NTE_BAD_PUBLIC_KEY. Invalid provider public key."
<p style=" Ihis is quite strange , i suppose that i receive errors related to the Private Key (cert.privateKey) or for the non supported algorithm.
<p style=" Moreover i say that automatically , when a SmartCard is in the reader, i have to receive a request to insert a PIN, but until the error, nothing appear, anyone can confirm that?
<p style=" Is my code Wrong, or error is determined to other reasons?
<p style="
<p style=" http://social.msdn.microsoft.com/Forums/it-IT/visualcsharpit/thread/d4f4c062-eeba-4e77-86ae-37c66b645a81/" style="font-weight:inherit; font-style:inherit; font-family:inherit; outline-width:0px; outline-style:initial; outline-color:initial; color:#0066dd; text-decoration:none; list-style-type:none; padding:0px; margin:0px; border:0px none initial http://social.msdn.microsoft.com/Forums/it-IT/visualcsharpit/thread/d4f4c062-eeba-4e77-86ae-37c66b645a81/
<p style=" and
<p style=" http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0cc90bdd-35f9-4a7d-8025-89f7ea9f9704" style="font-weight:inherit; font-style:inherit; font-family:inherit; outline-width:0px; outline-style:initial; outline-color:initial; color:#0066dd; text-decoration:none; list-style-type:none; padding:0px; margin:0px; border:0px none initial http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0cc90bdd-35f9-4a7d-8025-89f7ea9f9704
<p style=" they have a similar problem, and i see that they use the same code, but i dont know if their problems are solved.

<p style="
<p style=" Thanks to all.
<p style="



View the full article
 
Back
Top