Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Digital Signature different to C#
    text
    copied!<p>I have the following c# code to generate a digital signature from a private key:</p> <pre><code> static string Sign(string text, string certificate) { X509Certificate2 cert = new X509Certificate2(certificate, "TestPassword", X509KeyStorageFlags.Exportable); RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey; // Hash the data SHA1Managed sha1 = new SHA1Managed(); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(text); byte[] hash = sha1.ComputeHash(data); // Sign the hash return System.Convert.ToBase64String(rsa.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"))); } </code></pre> <p>I then created what I thought was the equivalent java code:</p> <pre><code>public static String signData(String dataToSign, String keyFile) { FileInputStream keyfis = null; try { keyfis = new FileInputStream(fileName); KeyStore store = KeyStore.getInstance("PKCS12"); store.load(keyfis, "TestPassword".toCharArray()); KeyStore.PrivateKeyEntry pvk = (KeyStore.PrivateKeyEntry)store. getEntry("testkey", new KeyStore.PasswordProtection("TestPassword".toCharArray())); PrivateKey privateKey = (PrivateKey)pvk.getPrivateKey(); byte[] data = dataToSign.getBytes("US-ASCII"); MessageDigest md = MessageDigest.getInstance("SHA1"); byte[] hashed = md.digest(data); Signature rsa = Signature.getInstance("SHA1withRSA"); rsa.initSign(privateKey); rsa.update(data); return Base64.encode(rsa.sign()); } catch (Exception ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } finally { if ( keyfis != null ) { try { keyfis.close() } catch (Exception ex) { keyfis = null; } } } return null; } </code></pre> <p>Unfortunately the digital signatures do not match.</p> <p>Any assistance will be greatly appreciated. Thanks in advance.</p> <p><strong>EDIT</strong>: If I remove the MessageDigest from the java code then the output is the same. Why? I thought hashing is needed.</p> <p>Regards, Eugene</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload