title: Equivalent of the method RSACryptoServiceProvider signHash in Java
body: <p>I'm trying to get the equivalent of the following C# method :</p>
  
  <pre><code>public byte[] SignHash(byte[] btHash, string SN)
  {
      string strSignature = string.Empty;
      X509Store x509store = null;
      x509store = new X509Store(StoreLocation.CurrentUser);
      x509store.Open(OpenFlags.ReadOnly);
  
      foreach (X509Certificate2 x509 in x509store.Certificates)
      {
          if (x509.SerialNumber.ToLower().Contains(SN.ToLower()))
          {
              byte[] btSignature = null;
              using (RSACryptoServiceProvider key = new RSACryptoServiceProvider())
              {
  
                  key.FromXmlString(x509.PrivateKey.ToXmlString(true));
                  return key.SignHash(btHash, CryptoConfig.MapNameToOID("SHA256"));
              }
              break;
          }
      }
      return null;
  
  }
  </code></pre>
  
  <p>In Java language. Actually, I've come to this :</p>
  
  <pre><code>private static String SignHash(final byte[] btHash, String SerialNumber) throws Exception
  {
      KeyStore ks = null;
      ks = KeyStore.getInstance("Windows-MY");
      ks.load(null, null);
  
      Boolean noValidCertificate = true;
  
      Enumeration&lt;String&gt; en = ks.aliases();
  
      ArrayList&lt;String&gt; lstAlias = Collections.list(en);
  
      lstErreurs.add(lstAlias.size() + " certificate(s) found");
  
      for (String aliasKey : lstAlias)
      {
          X509Certificate cert = (X509Certificate) ks.getCertificate(aliasKey);
  
          Certificat = Base64Coder.encodeBase64String(cert.getEncoded());
  
          Boolean blnCertificateFound = false;
  
          if (SerialNumber != null &amp;&amp; !SerialNumber.equals(""))
          {
              String SerialNumberCert = cert.getSerialNumber().toString(16);
  
              if (SerialNumber.toLowerCase().contains(SerialNumberCert.toLowerCase())
                      || SerialNumberCert.toLowerCase().contains(SerialNumber.toLowerCase()))
              {
                  blnCertificateFound = true;
              }
          }
  
          if (blnCertificateFound == false)
          {
              continue;
          }
  
          Provider p = ks.getProvider();
  
          boolean isHashToSign = false;
          for (String strToSign : input.split(";")) {
              if(strToSign.length() == 44 &amp;&amp; General.isBase64(strToSign)) {
                  isHashToSign = true;
                  break;
              }
          }
  
          String algorithm = "";
          if(isHashToSign)
          {
              algorithm = "RSA";
          } else {
              algorithm = "SHA256withRSA";
          }
          Signature sig = Signature.getInstance(algorithm, p);
  
          PrivateKey key = (PrivateKey) ks.getKey(aliasKey, "1234".toCharArray());
  
          if (key != null)
          {
              noValidCertificate = false;
  
              sig.initSign(key);
  
              String[] TabToSign = input.split(";");
              String strResultSignature = "";
              String separator = "";
              for (String strToSign : TabToSign)
              {
                  byte[] btToSign = null;
                  if(isHashToSign) {
                      btToSign = General.Base64_Decode_To_ByteArray(strToSign.getBytes(Charset.forName("UTF-8")));
                  } else {
                      btToSign = strToSign.getBytes(Charset.forName("UTF-8"));
                  }
  
                  sig.update(btToSign);
  
                  byte[] res = sig.sign();
  
                  String resB64 = Base64Coder.encodeBase64String(res);
  
                  strResultSignature += separator + resB64;
                  separator = ";";
              }
  
              return strResultSignature;
          }
      }
  
      return null;
  }
  </code></pre>
  
  <p>But getting the algorithm "RSA" does not work for signature. I finally sign the Hash of a Hash in Java. I would like to sign a SHA256 byte array hash without hashing it again. How can I come to this result ? (for information, I'm using the Windows Certificate Store, so I have to work with Sun MSCAPI provider).</p>
  
  <p><strong>EDIT 1 :</strong></p>
  
  <p>I tried with the algorithm "NONEwithRSA" but the signature result is different from the signature in .NET using SignHash method.</p>
  
  <p><strong>EDIT 2 :</strong></p>
  
  <p>The following thread : <a href="https://stackoverflow.com/questions/33305800/difference-between-sha256withrsa-and-sha256-then-rsa">Difference between SHA256withRSA and SHA256 then RSA</a> explains it is actually possible to sign a hash, but that method requires BouncyCastle. </p>
  
  <p>I can not operate with BouncyCastle because I need to use sun MSCAPI provider (Windows Certificate Store). I have to find an alternative to BouncyCastle (unless BC allows us to use the Sun MSCAPI provider).</p>