1

我正在尝试通过使用 Python 中的 pyscrypt 和 Crypto 库执行加密来复制以太坊密钥库文件。我使用 MyEtherWallet 创建了一个 Ethereum Keystore 文件。我从 MyEtherWallet 中获取了 SALT、IV、私钥、密码的值,并将它们硬编码到我的程序中。

from os import urandom
from Crypto.Cipher import AES
from Crypto.Util import Counter
import pyscrypt

private_key = bytes.fromhex('ec675d66dfda85e4fad4196766ff212d3b56ed9961eae353a74eec001614b785')
print(private_key) # This Gives b'\xecg]f\xdf\xda\x85\xe4\xfa\xd4\x19gf\xff!-;V\xed\x99a\xea\xe3S\xa7N\xec\x00\x16\x14\xb7\x85'

salt_bytes = bytes.fromhex('90adac19fc79271237944861e8b53c20aa5b6816acd4e9a0593cfda1dda8c62a')
print(salt_bytes) # This Gives b"\x90\xad\xac\x19\xfcy'\x127\x94Ha\xe8\xb5< \xaa[h\x16\xac\xd4\xe9\xa0Y<\xfd\xa1\xdd\xa8\xc6*"

iv_int = int("3153ecf2defd7f3a00418b6656b4bd83", 16)
print(iv_int) # This Gives 65567938323613189934818362641343626627

ctr = Counter.new(nbits=64,initial_value = iv_int)
print(ctr) # This Gives {'counter_len': 8, 'prefix': b'', 'suffix': b'', 'initial_value': 65567938323613189934818362641343626627, 'little_endian': False}

def enc_key(password):
    key =  pyscrypt.hash(password = b"test12345", 
                       salt = salt_bytes, 
                       N = 8192, 
                       r = 8, 
                       p = 1, 
                       dkLen = 32)
    print(key) # This Gives b'p\xd0+\xd6X"\xa9\x06\x02\xf3\x1d*\x95\xbd\xdb,p\xecJK\x11\x0c\x12\x83\xd3k\xe84\x86^\xbaO'
    return key

def encrypt_private_key(password,private_key):
    cipher = AES.new(enc_key(password),AES.MODE_CTR, counter = ctr)
    ciphertext = cipher.encrypt(private_key)
    return ciphertext

def decrypt_private_key(password,ciphertext):
    cipher = AES.new(enc_key(password),AES.MODE_CTR, counter = ctr)
    plaintext = cipher.decrypt(ciphertext)
    return plaintext


print(private_key.hex()) # This Gives ec675d66dfda85e4fad4196766ff212d3b56ed9961eae353a74eec001614b785
print(encrypt_private_key('test12345',private_key).hex()) # This Gives b5bbacb90c61b82dd30ebbf7fa107ec6926e8129049a088f37c25f7499a848d3 but it should give f5f3b92da81c85a19c7b85f9218f76943bc9227765443214063d56e02199c385
print(decrypt_private_key('test12345',encrypt_private_key('test12345',private_key)).hex()) # This Gives ec675d66dfda85e4fad4196766ff212d3b56ed9961eae353a74eec001614b785

My CipherText 与 Ethereum Keystore 文件中的密文不同。我想获得与 MyEtherWallet 相同的密文值。只有不同的参数可能是计数器值。

4

0 回答 0