Post
Topic
Board Development & Technical Discussion
Merits 1 from 1 user
Re: Extracting Privat Key from PEM File
by
HCP
on 16/01/2021, 01:27:56 UTC
⭐ Merited by ETFbitcoin (1)
How can I do that with CMD and Editor? Is this the right way to get the correct bitcoin address?
I don't think you can... because the steps outlined in that post are for going from public key to address... not from the private key...

Also, it should be noted that (for the public key):
- http://blockexplorer.com/q/hashtoaddress no longer works... but you can use http://blockchain.info/q/hashtoaddress
- you only want the last 65 bytes from the output of the base64 -d command:

Code:
wget -O - -q http://blockchain.info/q/hashtoaddress/$(grep -v 'PUBLIC KEY' <<<"
-----BEGIN PUBLIC KEY-----
MIH1MIGuBgcqhkjOPQIBMIGiAgEBMCwGByqGSM49AQECIQD/////////////////
///////////////////+///8LzAGBAEABAEHBEEEeb5mfvncu6xVoGKVzocLBwKb
/NstzijZWfKBWxb4F5hIOtp3JqPEZV2k+/wOEQio/Re0SKaFVBmcR9CP+xDUuAIh
AP////////////////////66rtzmr0igO7/SXozQNkFBAgEBA0IABJJ6TBhmiWm4
Y1ACBVJVn0oyG9Ay5IzEZq8cPyrs1PERl963YQh5UrGOT0NodynfHswkz8bUpaJW
FsowR/l9wXc=
-----END PUBLIC KEY-----" |
base64 -d |
tail -c 65 |
openssl dgst -sha256 |
cut -d\  -f2 |
xxd -r -p |
openssl dgst -rmd160 |
cut -d\  -f2)

This will return the example BTC address: 1Hy9dexzNzjvQYkYy6zKRVZMU8k2j5vuPt
So given a bitcoin address such as :
btc=1Hy9dexzNzjvQYkYy6zKRVZMU8k2j5vuPt


These commands only work on Linux... So you'll either need a Linux box, a Linux virtual machine, or you can even use "Windows Subsystem for Linux"...




As for your situation of having a private key .pem file...

Theoretically, you should probably be able to decode the private key from the .pem file output in a similar fashion... but you'd need to know exactly which parts of the Base64 encoded text were actually the hex private key (like how you only want the last 65 bytes of the "base64 -d" command above to get the public key from the .pem file.)

Looking at this post on StackExchange: https://bitcoin.stackexchange.com/questions/66594/signing-transaction-with-ssl-private-key-to-pem

We can see that if you can find the byte sequence: "30740201010420" in the decoded base64 output... then the next 32 bytes (64 chars) should be the HEX private key... from that stack exchange post, we can see the example hex private key they started with was:
Code:
3cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2c

And the privkey.pem is:
Code:
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIDzQVg9bJ1kZFsZDoLeqadA4OTgKc40ukSmQ3MVzcV0soAcGBSuBBAAK
oUQDQgAEvzUNKCE3UVimCLUePomOUH/kfy0ujHdN5Kmn7ez3TtokJDy5ksVnOgf6
WzpmzY46zvKAnQ44Cgx5Kdqx5dVDiw==
-----END EC PRIVATE KEY-----


If we use Python... we can decode that and view the bytes:
Code:
hardcorepawn@HardCorePC:~/$ python
Python 2.7.17 (default, Jul 20 2020, 15:37:01)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> import binascii
>>> base64_message = "MHQCAQEEIDzQVg9bJ1kZFsZDoLeqadA4OTgKc40ukSmQ3MVzcV0soAcGBSuBBAAKoUQDQgAEvzUNKCE3UVimCLUePomOUH/kfy0ujHdN5Kmn7ez3TtokJDy5ksVnOgf6WzpmzY46zvKAnQ44Cgx5Kdqx5dVDiw=="
>>> message_bytes = base64.b64decode(base64_message)
>>> binascii.hexlify(message_bytes)
'307402010104203cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2ca00706052b8104000aa14403420004bf350d2821375158a608b51e3e898e507fe47f2d2e8c774de4a9a7edecf74eda24243cb992c5673a07fa5b3a66cd8e3acef2809d0e380a0c7929dab1e5d5438b'

From this we can see the decoded Base64 is:
Code:
307402010104203cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2ca00706052b8104000aa14403420004bf350d2821375158a608b51e3e898e507fe47f2d2e8c774de4a9a7edecf74eda24243cb992c5673a07fa5b3a66cd8e3acef2809d0e380a0c7929dab1e5d5438b

We can see the sequence "30740201010420" right at the start (highlighted "Red")... so the next 32 bytes (64 chars) (highlighted "orange") should be the hex private key:
Quote
307402010104203cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2ca00706052b8104000aa14403420004bf350d2821375158a608b51e3e898e507fe47f2d2e8c774de 4a9a7edecf74eda24243cb992c5673a07fa5b3a66cd8e3acef2809d0e380a0c7929dab1e5d5438b

Giving us a hex private key of:
Code:
3cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2c

Which matches the HEX private key the example started with! Smiley


So, try base64 decoding your .pem file contents... and see if you can find the sequence "30740201010420"... if you can, the next 32 bytes (64 chars) should be the private key.

Let me know if you need any help.