Verifying after signing using jsrsasign is not working

844 views Asked by At

I have an html file to test signing and verification using the jsrsasign library. I use a 2048 RSA key to sign a string. Then I turn around and verify the signature. Here is my code:

// initialize
var hex = "48656c6c6f20776f726c6421"; // "Hello world!"
var sig = new KJUR.crypto.Signature({"alg": "SHA256withRSA", "prov": "cryptojs/jsrsa"});
// initialize for signature generation
sig.initSign(this.key); // at this point the key is a private key
// update data
sig.updateHex(hex); 
// calculate signature
var sigValueHex = sig.sign();

...

// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA256withRSA", "prov": "cryptojs/jsrsa"});
// initialize for signature validation
sig.initVerifyByPublicKey(this.key); // at this point the key is a public key
// update data
sig.updateHex(hex);
// verify signature
var isValid = sig.verify(sigValueHex);

// at this point isValid is still false

What am I doing wrong? I've traced through the code down to a method call named "_rsasign_getAlgNameAndHashFromHexDisgestInfo". When it compares the digest info head to the head of the hex bytes digested they don't match so verification fails. Is there something wrong with the way I'm signing? I'm not using the PEM (ASN.1/DER) spec to initialize the key. But the same key worked when i tested encrypting and decrypting.

1

There are 1 answers

0
Colin On

It turns out I was not building my key correctly. I was using a Microsoft XML spec to build out the pieces. My mistake was when I called parseBigInt() I did not specify the radix as 16 so all the arithmetic was off. Once I had the key right everything more or less fell into place.