Verifying Java signature in C#
Verifying Java signature in C#
I have signature created in Java by following code
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
signature = (Signature) rsaSha256.getCipher();
signature.initSign(privateKey);
signature.update(binaryData);
signatureBytes = signature.sign();
By verifying signature in C#, im always getting false. Following code use BouncyCastle library
ISigner signer = SignerUtilities.GetSigner("SHA256withRSA");
using (TextReader sr = new StringReader(publicKey))
PemReader pr = new PemReader(sr);
RsaKeyParameters keys = (RsaKeyParameters)pr.ReadObject();
signer.Init(false, keys);
signer.BlockUpdate(value, 0, value.Length);
bool isValid = signer.VerifySignature(signature);
return isValid;
Following code return false too
private static bool VerifyWithPublicKey(byte data, byte sig, string publicKey)
RSACryptoServiceProvider rsa;
using (var keyreader = new StringReader(publicKey))
var pemReader = new PemReader(keyreader);
var y = (RsaKeyParameters)pemReader.ReadObject();
RSAParameters p1 = DotNetUtilities.ToRSAParameters(y);
rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(p1);
byte hash;
using (var sha256 = SHA256.Create())
hash = sha256.ComputeHash(data);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(rsa);
RSADeformatter.SetHashAlgorithm("SHA256");
//Verify the hash and display the results to the console.
if (RSADeformatter.VerifySignature(hash, sig))
Console.WriteLine("The signature was verified.");
else
Console.WriteLine("The signature was NOT verified.");
// This always returns false
return rsa.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA256"), sig);
Im out of ideas. Anyone done something similar? If so, can you share your code please
javax.crypto.Signature#sign
@lockcmpxchg8b The X.509/DER vs IEEE-P1363 encoding only applies to DSA and ECDSA, not RSA. (And in DSA and ECDSA .NET wants the IEEE-P1363 version, not the DER version :))
– bartonjs
Sep 6 at 16:24
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Per this answer it looks like the C# verifier is expecting a DER-encoded signature. I can't seem to find documentation on whether there's a standard output format for JCE's
javax.crypto.Signature#sign
method. You could always drop the output you get from Java here to see if it decodes properly as a DER-encoded ASN.1 signature structure.– lockcmpxchg8b
Sep 1 at 18:09