title: signature with SHA256
body: <p>I have a smartcard and I need to sign a file with this.
  That is a big problem as I see in stackover.</p>
  
  <p>I couldnt use RSACryptoServiceProvider, bkz it doesnt support RSA-SHA256 alogrithm.</p>
  
  <p>At First I used CAPICOM.dll , like code bellow,</p>
  
  <pre><code>SignedData sed = new SignedData();
  sed.Content = "a"; // data to sign
  Signer ser = new Signer();
  ser.Certificate = cc;
  string singnn = sed.Sign(ser, false, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64);
  </code></pre>
  
  <p>But there isnt a public key to validate my signature value,, I couldnt get a validate key from capicom.dll.</p>
  
  <p>And after , </p>
  
  <p>I used X509Certificate2 , and RSACryptoServiceProvider  like code below,</p>
  
  <pre><code>        X509Certificate2 certificate = new X509Certificate2();
          // Access Personal (MY) certificate store of current user
          X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
          my.Open(OpenFlags.ReadOnly);
  
          // Find the certificate we'll use to sign            
          RSACryptoServiceProvider csp = null;
          foreach (X509Certificate2 cert in my.Certificates)
          {
              if (cert.Subject.Contains(certSubject))
              {
                  // We found it. 
                  // Get its associated CSP and private key
                  certificate = cert;
                  csp = (RSACryptoServiceProvider)cert.PrivateKey;
              }
          }
          if (csp == null)
          {
              throw new Exception("No valid cert was found");
          }
  
          // Hash the data
          SHA1Managed sha1 = new SHA1Managed();
          UnicodeEncoding encoding = new UnicodeEncoding();
          byte[] data = encoding.GetBytes(text);
          byte[] hash = sha1.ComputeHash(data);
  
          //byte[] data = Encoding.UTF8.GetBytes(text);
          //HashAlgorithm sha = new SHA256Managed();
          //byte[] hash = sha.TransformFinalBlock(data, 0, data.Length);
  
          string key = csp.ToXmlString(false);
          // Sign the hash
          csp.PersistKeyInCsp = true;
          byte[] response = csp.SignData(data, CryptoConfig.MapNameToOID("SHA1"));
          string signbase64 = Convert.ToBase64String(response);
  </code></pre>
  
  <p>It works , but I need to sign with RSA-SHA256 algorithm.
  When I changw hash algorithm  like this</p>
  
  <pre><code>byte[] response = csp.SignData(data, CryptoConfig.MapNameToOID("SHA256"));
  </code></pre>
  
  <p>I m getting an </p>
  
  <blockquote>
    <p>error : "unspecified error".</p>
  </blockquote>
  
  <p>Thats my problem,
  What is the sollution , or which library should I use ??</p>
  
  <p>Thanks for any advice..</p>