# How can i check if a public key match with a private key

Is there a way to check, (with openssl command for example), if a given public key match with a private key ?

Thanks

• What kind of key pair? – Maeher May 3 at 11:53
• I want to learn how bitcoin works. So it is rsa key pairs – Bob5421 May 3 at 11:58
• Erm... bitcoin does not use RSA. – Maeher May 3 at 12:05
• Sorry, i mean ECDSA (i was thinking it was a sub fonctionnality of RSA). But in both cases how can i check ? – Bob5421 May 3 at 12:14
• The question “OpenSSL ECDSA sign and verify file” on Super User is likely relevant if you're looking for an OpenSSL command line tool in particular. Note that openssl dgst -sign will create a ASN.1 metadata around the actual signature, so it may not be immediately usable for Bitcoin-related tasks specifically. – xorhash May 3 at 12:44

One method works with any signature scheme and any program including OpenSSL: make a signature of a file with the private key, and check signature and file against the public key.

If the check is OK, then private and public key match (or the signature scheme is broken).

If there is some failure, then private and public key do not match (or the program is misused, incomptible with the key formats, or broken).

Example with openssl, showing Verified OK for matched private/public keys tstpri.pem/tstpub.pem; Verification Failure for unmatched keys altpri.pem/tstpub.pem.

# generate an secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out tstpri.pem
# extract the public key
openssl ec -in tstpri.pem -pubout -out tstpub.pem >/dev/null
# generate an alternate secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out altpri.pem

# create a file fil (any content will do)
echo foo >fil

# sign fil with private key
openssl dgst -sha256 -sign tstpri.pem -out tst.sig fil
# verify the signature with matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verified OK

# sign fil with alternate private key
openssl dgst -sha256 -sign altpri.pem -out tst.sig fil
# verify the signature with non-matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verification Failure


Assuming you have the EC private key file in pem format (private.key), and the EC public key file in pem format (publick.key), you can verify that the two files match by deriving the public key from the private key file, like so:

openssl ec -pubout -in private.key


The output of this command should match the contents of public.key.

It appears we're talking about digital signature key pairs. If that's the case it's very easy to do. Say we have a public verification key $$\mathsf{vk}$$ and a candidate private signing key $$\mathsf{sk}$$

1. Choose some random message $$m$$.
2. Sign the message $$m$$ using $$\mathsf{sk}$$ resulting in signature $$\sigma$$.
3. Verify $$(m,\sigma)$$ using $$\mathsf{pk}$$.
4. If the signature verifies, then $$\mathsf{sk}$$ and $$\mathsf{pk}$$ match.

As long as the signature scheme is unforgeable, a signature produced using an unrelated signing key will not verify with overwhelming probability.

• Is there a way to check with openssl command ? – Bob5421 May 3 at 12:14
• I'm sure openssl has commands for signing and verifying, yes. – Maeher May 3 at 12:15