2
1
I have been looking at Coinapult's API documentation which details the use of PEM format in python-ecdsa. Specifically, private keys and public keys need to be formatted using PEM.
Let's take Coinapult's specified public key:
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEWp9wd4EuLhIZNaoUgZxQztSjrbqgTT0w
LBq8RwigNE6nOOXFEoGCjGfekugjrHWHUi8ms7bcfrowpaJKqMfZXg==
-----END PUBLIC KEY-----
Using python-ecdsa:
vk = ecdsa.VerifyingKey.from_pem(ECC_COINAPULT_PUB)
s = vk.to_string()
s.encode('hex')
>>> "5a9f7077812e2e121935aa14819c50ced4a3adbaa04d3d302c1abc4708a0344ea738e5c51281828c67de92e823ac7587522f26b3b6dc7eba30a5a24aa8c7d95e"
Or if it were a Bitcoin public key, that'd be 045a9f7077812e2e121935aa14819c50ced4a3adbaa04d3d302c1abc4708a0344ea738e5c51281828c67de92e823ac7587522f26b3b6dc7eba30a5a24aa8c7d95e
OK, that makes sense, but when we take the base64 encoded data between the leading and trailing -----BEGIN-----:
base64_data = "".join(ECC_COINAPULT_PUB[27:-26 ].split('\n'))
s = base64.b64decode(base64_data)
result = s.encode("hex")
>>> "3056301006072a8648ce3d020106052b8104000a034200045a9f7077812e2e121935aa14819c50ced4a3adbaa04d3d302c1abc4708a0344ea738e5c51281828c67de92e823ac7587522f26b3b6dc7eba30a5a24aa8c7d95e" # WTF is this??
Note that the PEM format decodes to some sort of DER string, 3056301006072a8648ce3d020106052b8104000a034200045a9f7077812e2e121935aa14819c50ced4a3adbaa04d3d302c1abc4708a0344ea738e5c51281828c67de92e823ac7587522f26b3b6dc7eba30a5a24aa8c7d95e, but what on earth is this DER string? I see 5a9f7077....a8c7d95e is in that string, so what are the leading bytes meaning?
Questions: 1. How do I encode a pubkey to PEM format without using python-ecdsa? 2. Is it the same procedure in #1 for private keys as it is public keys?
Thanks! I figured it out eventually:
der_string = "\x30{totallen}\x02\x01\x01\x04\x20{privkey}\xa0\x07\x06\x05\x2b\x81\x04\x00\x0a\xa1\x44\x03\x42\x00\x04{x}{y}"is for privkeys, andder_string = "\x30{totallen}\x30\x10\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x05\x2b\x81\x04\x00\x0a\x03\x42\x00\x04{pubkey}"for pubkeys – Wizard Of Ozzie – 2015-12-07T08:57:00.427Thanks, this shed a lot of light on things! Just one question: Is the Bitcoin public key needed at all in the message signing process, when using DER encoded PEM-files to sign the message? – Kebman – 2017-09-27T17:45:27.227
1@Kebman: ECDSA signing needs only privatekey not public. But Bitcoin doesn't sign messages, it signs transactions which (always or nearly) involve addresses which are hashed from the publickeys. EC privatekey files created by OpenSSL always include publickey, even though not required by SEC1 (and PKCS8), and in any case the publickey can always be recomputed from the privatekey. – dave_thompson_085 – 2017-09-29T04:18:49.460