Recover ETH private key from kestore file in PHP

Recover pirvate key from keystore
ethereum recover private key from keystore file

Implementation

Read the keystore file

$file = file_get_contents($path);
$json = json_decode($file, true);

Derivate the key

$crypto = $json['crypto'];
$derivation = scrypt(
$this->password,
hex2bin($crypto['kdfparams']['salt']),
$crypto['kdfparams']['n'], //The CPU difficulty. Also called "N" in scrypt documentation. Must be a power of 2.
$crypto['kdfparams']['r'], //The memory difficulty. Also called "r" in scrypt documentation.
$crypto['kdfparams']['p'], //The parallel difficulty. Also called "p" in scrypt documentation.
$crypto['kdfparams']['dklen'], //The key length
);
$key = openssl_decrypt(
hex2bin($crypto['ciphertext']),
strtoupper($crypto['cipher']),
hex2bin($derivation),
OPENSSL_RAW_DATA,
hex2bin($crypto['cipherparams']['iv']),
);

Wrap it up

$file = file_get_contents($path);
$json = json_decode($file, true);
$crypto = $json['crypto'];
$derivation = scrypt(
$this->password,
hex2bin($crypto['kdfparams']['salt']),
$crypto['kdfparams']['n'], //The CPU difficulty. Also called "N" in scrypt documentation. Must be a power of 2.
$crypto['kdfparams']['r'], //The memory difficulty. Also called "r" in scrypt documentation.
$crypto['kdfparams']['p'], //The parallel difficulty. Also called "p" in scrypt documentation.
$crypto['kdfparams']['dklen'], //The key length
);
$key = openssl_decrypt(
hex2bin($crypto['ciphertext']),
strtoupper($crypto['cipher']),
hex2bin($derivation),
OPENSSL_RAW_DATA,
hex2bin($crypto['cipherparams']['iv']),
);
./ethkey inspect --private <keystore-file>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store