Skip to content

Instantly share code, notes, and snippets.

@coffnix
Created July 31, 2025 19:29
Show Gist options
  • Save coffnix/af42625617b24acc600c84d6f61546da to your computer and use it in GitHub Desktop.
Save coffnix/af42625617b24acc600c84d6f61546da to your computer and use it in GitHub Desktop.
test-py
import base64
import hashlib
import ecdsa
import base58
from Crypto.Cipher import AES
from Crypto.Hash import MD5
# base64 criptografado
encrypted_b64 = (
"U2FsdGVkX18xMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz"
"NDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM="
)
senha = "1234567890123456"
# derivação OpenSSL EVP_BytesToKey com MD5
def evp_bytes_to_key(password, salt, key_len=32, iv_len=16):
d = b""
last = b""
while len(d) < (key_len + iv_len):
last = MD5.new(last + password + salt).digest()
d += last
return d[:key_len], d[key_len:key_len+iv_len]
# decode e salt
raw = base64.b64decode(encrypted_b64)
assert raw[:8] == b"Salted__"
salt = raw[8:16]
ciphertext = raw[16:]
# derivação e descriptografia
key, iv = evp_bytes_to_key(senha.encode(), salt)
cipher = AES.new(key, AES.MODE_CBC, iv)
if len(ciphertext) % 16 != 0:
ciphertext += b"\x00" * (16 - len(ciphertext) % 16)
decrypted = cipher.decrypt(ciphertext)
# extrai os 32 bytes da chave privada
privkey_bytes = decrypted[:32]
privkey_hex = privkey_bytes.hex()
# gera a chave pública (não comprimida)
sk = ecdsa.SigningKey.from_string(privkey_bytes, curve=ecdsa.SECP256k1)
vk = sk.get_verifying_key()
pubkey = b"\x04" + vk.to_string()
# endereço Bitcoin (P2PKH)
sha256 = hashlib.sha256(pubkey).digest()
ripemd160 = hashlib.new("ripemd160", sha256).digest()
payload = b"\x00" + ripemd160
checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
btc_address = base58.b58encode(payload + checksum).decode()
# formato WIF
wif_payload = b"\x80" + privkey_bytes
wif_checksum = hashlib.sha256(hashlib.sha256(wif_payload).digest()).digest()[:4]
wif = base58.b58encode(wif_payload + wif_checksum).decode()
print("Chave privada (hex):", privkey_hex)
print("Endereço Bitcoin:", btc_address)
print("WIF:", wif)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment