Created
June 21, 2019 05:37
-
-
Save TheWaWaR/0b62251f8b6b6309aea0183d1343d1ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sha3 | |
import hashlib | |
import scrypt | |
from binascii import unhexlify, hexlify | |
from Crypto.Cipher import AES | |
from Crypto.Util import Counter | |
data = { | |
"address": "178ca94d1dece58a1c16bcb6e0eab8f440e60a01", | |
"crypto": { | |
"cipher": "aes-128-ctr", | |
"ciphertext": "383cca818805e1134e93d200db94b9babc798e6537373604543ae870c1d221e2", | |
"cipherparams": { | |
"iv": "25e68fc8d166ee203c645868d5e0f94a" | |
}, | |
"kdf": "scrypt", | |
"kdfparams": { | |
"dklen": 32, | |
"n": 262144, | |
"p": 1, | |
"r": 8, | |
"salt": "d27657804be7ef5b9c4d0ac513aa210eeb1ec64ad8f23d639aa579aebfc76832" | |
}, | |
"mac": "75ec9139e21805983da2ca8f2523aec5bd78cf5983b93f1306b913e80fabdb7d" | |
}, | |
"id": "5f20d562-5ab8-4c15-8df6-4ba8fbddacba", | |
"version": 3 | |
} | |
password = 'hello123' | |
def get_kdf_key(kdfparams, password): | |
r = kdfparams['r'] | |
p = kdfparams['p'] | |
n = kdfparams['n'] | |
buflen = kdfparams['dklen'] | |
salt = unhexlify(kdfparams['salt']) | |
return scrypt.hash(password, salt, N=n, r=r, p=p, buflen=buflen) | |
def calc_mac(data, password): | |
crypto = data['crypto'] | |
ciphertext = unhexlify(crypto['ciphertext']) | |
kdfparams = crypto['kdfparams'] | |
decryption_key = get_kdf_key(kdfparams, password) | |
print(hexlify(decryption_key[16:32] + ciphertext)) | |
calculated_mac = sha3.keccak_256(decryption_key[16:32] + ciphertext) | |
return calculated_mac.digest() | |
mac_hex = hexlify(calc_mac(data, password)) | |
print('mac = {}'.format(mac_hex)) | |
print(mac_hex == data['crypto']['mac']) | |
def decrypt_key(data, password): | |
crypto = data['crypto'] | |
ciphertext = unhexlify(crypto['ciphertext']) | |
iv_hex = crypto['cipherparams']['iv'] | |
iv = unhexlify(iv_hex) | |
decryption_key = get_kdf_key(crypto['kdfparams'], password) | |
ctr = Counter.new(128, initial_value=long(iv_hex, 16)) | |
aes = AES.new(decryption_key[:16], AES.MODE_CTR, counter=ctr) | |
return aes.decrypt(ciphertext) | |
print('privkey = {}'.format(hexlify(decrypt_key(data, password)))) | |
def encrypt_key(privkey_hex, iv_hex, kdfparams, password): | |
privkey_bytes = unhexlify(privkey_hex) | |
iv = unhexlify(iv_hex) | |
decryption_key = get_kdf_key(kdfparams, password) | |
ctr = Counter.new(128, initial_value=long(iv_hex, 16)) | |
aes = AES.new(decryption_key[:16], AES.MODE_CTR, counter=ctr) | |
return aes.encrypt(privkey_bytes) | |
privkey_hex = '2d319aff207c5b566af776a3e59dfda994e538baa239c81ffa4f780759d3fbe8' | |
iv_hex = '25e68fc8d166ee203c645868d5e0f94a' | |
kdfparams = data['crypto']['kdfparams'] | |
output_ciphertext = hexlify(encrypt_key(privkey_hex, iv_hex, kdfparams, password)) | |
print('ciphertext = {}'.format(output_ciphertext)) | |
print(output_ciphertext == '383cca818805e1134e93d200db94b9babc798e6537373604543ae870c1d221e2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment