HelloWorld
HelloWorld

Reputation: 3749

How to verify ECDSA signature in ASN.1 format and public key in DER using BouncyCastle?

I have a byte array that contains the public key, and a byte array that contains the ASN.1 formatted signature. Using ECDSA P-256.

The following code loads the public key:

var publicKey = PublicKeyFactory.CreateKey(publicKeyDERBytes);

And the following code verifies the signature:

var signer = SignerUtilities.GetSigner("ECDSA");

signer.Init(false, publicKey);
signer.BlockUpdate(signatureASN1Bytes, 0, signatureASN1Bytes.Length);
Console.WriteLine(signer.VerifySignature(signature));

But it always writes false. What could be wrong?

Using BouncyCastle.NetCore 1.8.3 on .NET Core.

Upvotes: 1

Views: 825

Answers (1)

HelloWorld
HelloWorld

Reputation: 3749

Turns out I was not getting the correct signer.

Instaead of:

var signer = SignerUtilities.GetSigner("ECDSA");

It should be:

var signer = SignerUtilities.GetSigner("SHA256withECDSA");

Upvotes: 1

Related Questions