I have an application and a server that both use signed messages (using bouncycastle) to communicate. For a new application, I want to use JS and sign the message using jsrsasign and then verify it on the server side using bouncycastle again. The keys are generated in bouncycastle as well and then transferred to the JS application.
The signature is always invalid for the JS->Server communication, so I tried to sign and verify the message completely in JS and found that this also doesn't work. I am assuming that I am not using the correct configuration, but I fail to figure out the source of the problem.
I am using ECDSA with the prime256v1 curve.
My keys are generated like this:
val generator = KeyPairGenerator.getInstance("ECDSA")
val spec = ECNamedCurveTable.getParameterSpec("prime256v1")
generator.initialize(spec, SecureRandom())
val keypair = generator.generateKeyPair()
val privateKey = Base64.toBase64String(keypair.private.encoded)
// X962
val publicKey = Base64.toBase64String(Arrays.copyOfRange(keypair.public.encoded, 26, keypair.public.encoded.size))
The generated keys look like this:
privateKey = MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg6xrOGWvvTTh5EfxhDco0xqppyFrSRbFaKUKbZutlynugCgYIKoZIzj0DAQehRANCAAQ6EhELaQWv3z7tBgJXYMGspw93Ni+LvvmY3f0MSBtu84ldBZi3boz+OV3hiiyO+mBx+jCg4s2TDF+nw0Vi3lS7
publicKey = BDoSEQtpBa/fPu0GAldgwaynD3c2L4u++Zjd/QxIG27ziV0FmLdujP45XeGKLI76YHH6MKDizZMMX6fDRWLeVLs=
For testing I take these keys and run this to see if they are valid on JS side:
const message = "Testing";
// Sign
const sig = new KJUR.crypto.Signature({ alg: "SHA512withECDSA" });
sig.init({ d: keyPair.private, curve: "prime256v1" });
sig.updateString(message);
const signature = sig.sign();
// Verify
let sigVerification = new r.Signature({ alg: "SHA512withECDSA" });
sigVerification.init({ xy: keyPair.public, curve: "prime256v1" });
sigVerification.updateString(message);
let isValid = sigVerification.verify(signature);
if(isValid) {
console.log("Signature is valid, keys are matching");
} else {
throw "The signature is invalid. Please make sure the keys match."
}
This fails for the keys taken from server, but when I generated keys with jsrsasign it works (.I need to make the server keys work, though).
I already tried to encode the keys to HEX instead of BASE64, but that didn't help. Since I am fairly new to the whole signing topic and at the end of my wits, any help is deeply appreciated.