Post
Topic
Board Development & Technical Discussion
Re: Extracting Privat Key from PEM File
by
dextronomous
on 19/01/2021, 00:54:57 UTC
From the old topic it's a PUBLIC key not a PRIVATE key. Why did you change it?

I don't want to post my private Key online so anyone can take my BTC.  Roll Eyes


Thx for this code with Python HCP. This was what I need.

This gives an cool formated output to see everthing bedder:
Code:
openssl asn1parse -in privatekey.pem

Hex-dump is the Private Key

I found a Code from https://gist.github.com/Jun-Wang-2018/3105e29e0d61ecf88530c092199371a7#file-bitcoin_from_private_key_to_wif-py to convert HEX to WIF:

Code:
# From private key(hex) to Wallet Import Format(WIF)
# Reference: https://medium.freecodecamp.org/how-to-create-a-bitcoin-wallet-address-from-a-private-key-eca3ddd9c05f
#            https://docs.python.org/2/library/hashlib.html
import codecs  #If not installed: "pip3 install codecs"
import hashlib
# PK0 is a demo private key.
PK0 = "3cd0560f5b27591916c643a0b7aa69d03839380a738d2e912990dcc573715d2c"
PK1 = '80'+ PK0
PK2 = hashlib.sha256(codecs.decode(PK1, 'hex'))
PK3 = hashlib.sha256(PK2.digest())
checksum = codecs.encode(PK3.digest(), 'hex')[0:8]
PK4 = PK1 + str(checksum)[2:10]  #I know it looks wierd

# Define base58
def base58(address_hex):
    alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    b58_string = ''
    # Get the number of leading zeros
    leading_zeros = len(address_hex) - len(address_hex.lstrip('0'))
    # Convert hex to decimal
    address_int = int(address_hex, 16)
    # Append digits to the start of string
    while address_int > 0:
        digit = address_int % 58
        digit_char = alphabet[digit]
        b58_string = digit_char + b58_string
        address_int //= 58
    # Add ‘1’ for each 2 leading zeros
    ones = leading_zeros // 2
    for one in range(ones):
        b58_string = '1' + b58_string
    return b58_string

WIF = base58(PK4)
print(WIF)
quoted all dang, to say that matja btc tool does the same true one command and batch format like several keys at once. gl. Wink