pythoncryptographyecdsa

python ecdsa get private and public key


This question may seem simple for those who know the library. How do I get the actual value of the private and public keys? I have

private_key = SigningKey.generate(SECP256k1)
public_key = private_key.get_verifying_key()
print("private_key:")
print(private_key)
print("public_key:")
print(public_key)

and it prints

generate_keys() private_key: <ecdsa.keys.SigningKey object at 0x7fd0a4ef56a0> public_key: VerifyingKey.from_string(b'\x029q\xfd\xe9\x1dL\xc0\xab\xb1\xd2GG\xef8\xcb\x89\xce\xbb\xa8\x10*\xfa\xda\x0c\x92\x12\xa5\xa0\x81\xef\x07\x9e', SECP256k1, sha1) (<ecdsa.keys.SigningKey object at 0x7fd0a4ef56a0>, VerifyingKey.from_string(b'\x029q\xfd\xe9\x1dL\xc0\xab\xb1\xd2GG\xef8\xcb\x89\xce\xbb\xa8\x10*\xfa\xda\x0c\x92\x12\xa5\xa0\x81\xef\x07\x9e', SECP256k1, sha1))

I need the private_key and public_key real values. How do I get them?


Solution

  • You have generated the private and public keys correctly. You now have class instances. These instances are not necessarily printing the way you expect - I think that is your only issue.

    If you want to see PEM format, you should do this:

    private_key = SigningKey.generate(SECP256k1)
    public_key = private_key.get_verifying_key()
    print("private_key:")
    print(private_key.to_pem())
    print("public_key:")
    print(public_key.to_pem())
    

    For DER format, use to_der(). For raw bytes, use to_string().

    If you are communicating a key to someone, to a wallet, to openssl, etc, you probably want PEM format.