Post
Topic
Board Project Development
Topic OP
Try create Private Key Recovery
by
ICYNOTE2023
on 01/08/2023, 14:33:01 UTC
Hi Guys,,,

I want to try to make a program in python to find or restore a lost private key. The program's goal is also to search for missing characters. Any input, suggestions will be greatly appreciated. If there are deficiencies, please correct them so that they become better.

Code:
import random
import hashlib
import base58
from bitcoinlib.keys import Key

def generate_random_wif(wif, target_address):
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    missing_indices = [i for i in range(len(wif)) if wif[i] == '*']
    found = False
    while not found:
        rand_chars = ''.join(random.choice(alphabet) for _ in range(len(missing_indices)))
        test_wif = wif
        for index, char in zip(missing_indices, rand_chars):
            test_wif = test_wif[:index] + char + test_wif[index+1:]
        try:
            key = Key(wif=test_wif)
            address = key.address()
            if address == target_address:
                found = True
                return test_wif, address
            print(f"Scanning WIF: {test_wif}")
        except:
            print(f"Invalid WIF: {test_wif}")

def main():
    input_wif = input("Enter a WIF string with missing characters (mark with *): ")
    target_address = input("Enter the target Bitcoin address: ")

    if '*' not in input_wif:
        print("Incorrect entry: * sign missing.")
        return

    found_wif, found_address = generate_random_wif(input_wif, target_address)
   
    if found_wif:
        print("Correct WIF found:", found_wif)
        print("Corresponding Bitcoin address:", found_address)
        with open("found.txt", "w") as file:
            file.write(found_wif)

        with open("found2.txt", "w") as file:
            file.write(f"WIF: {found_wif}\nAddress: {found_address}")

    else:
        print("The correct WIF was not found. try more.")

if __name__ == "__main__":
    main()


I want to ask the experts here, how can this program run even faster.

Thank's  Wink