<h1>converts WIF private key back to basic private key format</h1>
<pre><code>import hashlib
import base58
import binascii

private_key_WIF = input(&quot;WIF: &quot;)
first_encode = base58.b58decode(private_key_WIF)
private_key_full = binascii.hexlify(first_encode)
private_key = private_key_full[2:-8]
print(private_key)
</code></pre>

CC BY-SA 3.0
2017-11-04T23:49:28.870Z
Welcome to Bitcoin.SE! While the code might be what is needed, it would be great if you could explain the issue and how it's fixed in your answer too :)
meshcollider
CC BY-SA 4.0
2018-12-12T07:26:23.477Z
Why did you do private_key_full[2:-8]?
Five Points
CC BY-SA 4.0
2018-12-12T07:36:02.787Z
Useful resource for others: http://learnmeabitcoin.com/glossary/wif
Five Points
CC BY-SA 4.0
2018-12-14T14:12:22.090Z
https://en.bitcoin.it/wiki/Private_key#An_example_private_key
george7n
CC BY-SA 4.0
2018-12-14T14:13:11.233Z
this code above is for private keys associated with uncompressed public keys, they are 51 characters and always start with the number 5 on mainnet (9 on testnet).
george7n
CC BY-SA 4.0
2018-12-14T14:16:10.343Z
[2:-8] gets rid of the version byte prefix, the Compression Byte suffix and the Checksum....returns only a private key
george7n
CC BY-SA 4.0
2017-11-04T22:46:04.640Z
2020-06-21T00:27:43.217Z
2020-06-21T00:27:43.217Z
george7n
<p>simple way:</p>
<pre><code>import bitcoin

private_key = &quot;EF235AACF90D9F4AADD8C92E4B2562E1D9EB97F0DF9BA3B508258739CB013DB2&quot;
print(bitcoin.decode_privkey(private_key, 'hex'))
</code></pre>

CC BY-SA 4.0
2022-12-09T20:39:13.450Z
2022-12-09T20:39:13.450Z
Abolfazl Rastgou
<p>simplest way</p>
<pre class="lang-py prettyprint-override"><code>from bit import Key

# Import wif format:
privKey = Key.('L1VotKmtZRLZSnSPhLhQxfts2aqBMru2APTs4Yuc8TYJ4jNhQoGB')



# Export hex format:

privKey = Key.to_hex()

# Export int format:

privKey = Key.to_int()


# Export to bytes
privKey = Key.to_bytes()

</code></pre>

CC BY-SA 4.0
2022-12-15T16:21:55.503Z
2022-12-15T16:21:55.503Z
fredsta98
<p>Don't do this: <code>binascii.hexlify(private_key_WIF)</code>. That's not how you use <code>binascii.hexlify</code>. There is no hex here, and the string is not a bytes-like object. <code>private_key_WIF</code> is just a string. You want to pass that string directly into <code>base58.b58decode</code> because you want to decode the WIF (which is base58).</p>

CC BY-SA 3.0
2017-08-15T18:59:02.397Z
Something like this gives you the byte array to manipulate (compare results in http://gobittest.appspot.com/PrivateKey):
private_key_WIF = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
first_encode = base58.b58decode(private_key_WIF)
print ([hex(ord(c)) for c in first_encode])
KOld
CC BY-SA 3.0
2017-08-15T17:46:13.150Z
2017-08-15T17:46:13.150Z
Ava Chow
<p>I am aware how to convert the private key into WIF format in python, now I am trying to reverse this process and convert a WIF formatted private key back into a 256-bit private key, following this guide: <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="noreferrer">https://en.bitcoin.it/wiki/Wallet_import_format</a></p>

<p>This is the code to convert from 256-bit private key into WIF format:</p>

<pre><code>import hashlib
import base58
import binascii


private_key_static = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"
extended_key = "80"+private_key_static
first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest()
second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()

# add checksum to end of extended key
final_key = extended_key+second_sha256[:8]

# Wallet Import Format = base 58 encoded final_key
WIF = base58.b58encode(binascii.unhexlify(final_key))

print (WIF)
</code></pre>

<p>Now my attempt to reverse this process looks like this:</p>

<pre><code>import hashlib
import base58
import binascii

private_key_WIF = 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
first_encode = base58.b58decode(binascii.hexlify(private_key_WIF))
print (first_encode)
</code></pre>

<p>Instead of receiving the byte string, I get following error: </p>

<pre><code>TypeError: a bytes-like object is required, not 'str'
</code></pre>

<p>(All private keys used are sample keys taken from Bitcoin Wiki.)</p>

<private-key><python><wallet-import-format>
Convert WIF to private key
Fabulous Job
# Convert WIF to private key

(Asked by: username_0 on 2017-08-15)

I am aware how to convert the private key into WIF format in python, now I am trying to reverse this process and convert a WIF formatted private key back into a 256-bit private key, following this guide: https://en.bitcoin.it/wiki/Wallet_import_format
This is the code to convert from 256-bit private key into WIF format:
import hashlib
import base58
import binascii


private_key_static = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"
extended_key = "80"+private_key_static
first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest()
second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()

# add checksum to end of extended key
final_key = extended_key+second_sha256[:8]

# Wallet Import Format = base 58 encoded final_key
WIF = base58.b58encode(binascii.unhexlify(final_key))

print (WIF)

Now my attempt to reverse this process looks like this:
import hashlib
import base58
import binascii

private_key_WIF = 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
first_encode = base58.b58decode(binascii.hexlify(private_key_WIF))
print (first_encode)

Instead of receiving the byte string, I get following error: 
TypeError: a bytes-like object is required, not 'str'

(All private keys used are sample keys taken from Bitcoin Wiki.)

**Tags:** private-key, python, wallet-import-format


---

## 4 answers

### Answer by username_6 on 2017-08-15. Score: 4 | Accepted answer

Don't do this: `binascii.hexlify(private_key_WIF)`. That's not how you use `binascii.hexlify`. There is no hex here, and the string is not a bytes-like object. `private_key_WIF` is just a string. You want to pass that string directly into `base58.b58decode` because you want to decode the WIF (which is base58).

> **Comments:**
> **username_7**: Something like this gives you the byte array to manipulate (compare results in http://gobittest.appspot.com/PrivateKey): private_key_WIF = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" first_encode = base58.b58decode(private_key_WIF) print ([hex(ord(c)) for c in first_encode])  

---

### Answer by username_1 on 2017-11-04. Score: 9

converts WIF private key back to basic private key format
import hashlib
import base58
import binascii

private_key_WIF = input("WIF: ")
first_encode = base58.b58decode(private_key_WIF)
private_key_full = binascii.hexlify(first_encode)
private_key = private_key_full[2:-8]
print(private_key)

> **Comments:**
> **username_2**: Welcome to Bitcoin.SE! While the code might be what is needed, it would be great if you could explain the issue and how it's fixed in your answer too :)  
> **username_3**: Why did you do private_key_full[2:-8]?  
> **username_3**: Useful resource for others: http://learnmeabitcoin.com/glossary/wif  
> **username_1**: https://en.bitcoin.it/wiki/Private_key#An_example_private_key  
> **username_1**: this code above is for private keys associated with uncompressed public keys, they are 51 characters and always start with the number 5 on mainnet (9 on testnet).  
> **username_1**: [2:-8] gets rid of the version byte prefix, the Compression Byte suffix and the Checksum....returns only a private key  

---

### Answer by username_4 on 2022-12-09. Score: 0

simple way:
import bitcoin

private_key = "EF235AACF90D9F4AADD8C92E4B2562E1D9EB97F0DF9BA3B508258739CB013DB2"
print(bitcoin.decode_privkey(private_key, 'hex'))


---

### Answer by username_5 on 2022-12-15. Score: 0

simplest way
from bit import Key

# Import wif format:
privKey = Key.('L1VotKmtZRLZSnSPhLhQxfts2aqBMru2APTs4Yuc8TYJ4jNhQoGB')



# Export hex format:

privKey = Key.to_hex()

# Export int format:

privKey = Key.to_int()


# Export to bytes
privKey = Key.to_bytes()


---

