<blockquote>
<p>Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.</p>
</blockquote>
<ol>
<li>Check if the string has valid Base-58 characters (<code>123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz</code>)</li>
<li>Decode the string to get the byte array using Base-58 encoding while removing and validating its checksum (last 4 bytes). Refer to the <a href="https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string" rel="nofollow noreferrer">wiki link</a> you posted for details.</li>
<li>Take the first byte and check if it is using the expected (version) value (128 for MainNet, 239 for TestNet).</li>
<li>Check the remaining data length (without first byte):<br />
4.1. If it is 32 bytes, that is the key<br />
4.2. If it is 33 bytes, remove last byte and check if it is equal to <code>1</code>, the rest is the key<br />
4.3. If the length is anything else, it is invalid</li>
<li>Convert the 32 bytes you have from step 4 to a 256-bit integer using big-endian definition</li>
<li>The value of step 5 has to be &gt;= <code>1</code> and &lt;= <code>0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140</code></li>
</ol>

CC BY-SA 4.0
2020-10-18T08:42:32.490Z
2020-10-18T08:42:32.490Z
Coding Enthusiast
<p>Using <a href="https://en.bitcoin.it/wiki/Base58Check_encoding" rel="nofollow noreferrer">this link</a> I can understand something about a private key BASE58 (P2PKH):</p>
<p>&quot;such encodings will always yield a 51-character string that starts with '5', or more specifically, either '5H', '5J', or '5K'.&quot;</p>
<p>So let's say I have this model of private key:</p>
<p>5J1111111111111111111111111111111111110111111222222</p>
<p>I know that is not a valid private key because I can use a website such as bitaddress to tell me about this.</p>
<p>Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.</p>
<p>Keep in mind I don't have any other information, just a string like that.</p>

CC BY-SA 4.0
2020-10-18T03:02:39.567Z
I think the search term you are missing is "wallet import format": https://en.bitcoin.it/wiki/Wallet_import_format
Murch
CC BY-SA 4.0
2020-10-18T03:33:44.610Z
I think I should say WIF instead private key, but anyway, those steps are a little bit confused to me because I don't have a compressed public key to verify the WIF, I only have a string.
Luca Blight
<private-key><base58><wallet-import-format>
What is the math behind a private key BASE58 (P2PKH)
Luca Blight
# What is the math behind a private key BASE58 (P2PKH)

(Asked by: username_0 on 2020-10-18)

Using [this link](https://en.bitcoin.it/wiki/Base58Check_encoding) I can understand something about a private key BASE58 (P2PKH):

"such encodings will always yield a 51-character string that starts with '5', or more specifically, either '5H', '5J', or '5K'."

So let's say I have this model of private key:

5J1111111111111111111111111111111111110111111222222

I know that is not a valid private key because I can use a website such as bitaddress to tell me about this.

Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.

Keep in mind I don't have any other information, just a string like that.

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

> **Comments:**
> **username_1**: I think the search term you are missing is "wallet import format": https://en.bitcoin.it/wiki/Wallet_import_format  
> **username_0**: I think I should say WIF instead private key, but anyway, those steps are a little bit confused to me because I don't have a compressed public key to verify the WIF, I only have a string.  

---

## 1 answers

### Answer by username_2 on 2020-10-18. Score: 1 | Accepted answer

> Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.

1. Check if the string has valid Base-58 characters (`123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`)
2. Decode the string to get the byte array using Base-58 encoding while removing and validating its checksum (last 4 bytes). Refer to the [wiki link](https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string) you posted for details.
3. Take the first byte and check if it is using the expected (version) value (128 for MainNet, 239 for TestNet).
4. Check the remaining data length (without first byte):  
   4.1. If it is 32 bytes, that is the key  
   4.2. If it is 33 bytes, remove last byte and check if it is equal to `1`, the rest is the key  
   4.3. If the length is anything else, it is invalid
5. Convert the 32 bytes you have from step 4 to a 256-bit integer using big-endian definition
6. The value of step 5 has to be >= `1` and <= `0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140`


---

