go-ethereum
の実装と互換性のあるEthereum用の秘密鍵の暗号化を構築する必要があります(Rubyで暗号化された鍵はEthereumの実装)。aes-128-ctr + scryptを使用してRubyで秘密鍵を暗号化する
イーサリアムは、例えば(16進数でエンコードされた)、このような32ビットの秘密鍵を使用しています:
1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
私は、このキーにgo-ethereum
実装とパスワードで暗号化する「パスワード」をインポートする場合、それをこの出力を生成します。
{
"address":"1be31a94361a391bbafb2a4ccd704f57dc04d4bb",
"crypto":{
"cipher":"aes-128-ctr",
"ciphertext":"62bbf1a5a93b8ba8c66b70b3381f9f5badf44b35287614d309d760ebeec47139",
"cipherparams":{
"iv":"a4a6638ea73872c07d62fa065f37f790"
},
"kdf":"scrypt",
"kdfparams":{
"dklen":32,
"n":262144,
"p":1,
"r":8,
"salt":"69ccd8c258bb50ac2effd65837e09e45b8bd9a747a1a1f3558b65a16e2f46f1a"
},
"mac":"68ca6bc011d4d656e12a34cefd28005dbf76d9cfac15db2eaa83920eec5b38a9"
},
"id":"9863070b-6c16-4aef-8188-2a34660192bf",
"version":3
}
だから、すべてのKDF(鍵導出関数)パラメータを使用して、それが暗号文
62bbf1a5a93b8ba8c66b70b3381f9f5badf44b35287614d309d760ebeec47139
を生成し、
Rubyを使用して同じ暗号テキストを再現しようとしました.Goの実装も見ています。これは私のコードです:
------------ Encryption input ------------
Clear private key = 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
------------ Scrypt parameters ------------
Salt str = 69ccd8c258bb50ac2effd65837e09e45b8bd9a747a1a1f3558b65a16e2f46f1a
Iv str = a4a6638ea73872c07d62fa065f37f790
n = 262144
r = 8
p = 1
dklen = 32
------------ Scrypt output ------------
Derived key = b6e4410aa658f21213c7e55bacbbd8093e67f7f1738e7235335b58a2b690dcf5
------------ Encryption output ------------
Cipher text = 6fddd3d2199edf65a17d9277d2328f5357e70a5be2e173d17681883ef5a3a27e
------------ Decryption output ------------
Decrypted: 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
Decryption worked: true
しかし、暗号文は同じ入力とパラメータを使用して、go-ethereum
が発生したものとは異なっている:
# hard coded password
password = "password"
# hard coded test private key
plain_private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
puts "------------ Encryption input ------------ "
puts "Clear private key = " + plain_private_key
# Scrypt params, same as in Geth/Ethereum
n = 262144
r = 8
p = 1
dklen = 32
# using same salt as Ethereum used
salt = "69ccd8c258bb50ac2effd65837e09e45b8bd9a747a1a1f3558b65a16e2f46f1a"
# using same iv as Ethereum used
iv = "a4a6638ea73872c07d62fa065f37f790"
puts "------------ Scrypt parameters ------------ "
puts "Salt str = " + salt
puts "Iv str = " + iv
puts "n = " + n.to_s
puts "r = " + r.to_s
puts "p = " + p.to_s
puts "dklen = " + dklen.to_s
# Generate derived key
derived_key = SCrypt::Engine.scrypt(password, salt, n, r, p, dklen)
puts "------------ Scrypt output ------------ "
puts "Derived key from password = " + derived_key.unpack("H*")[0]
# Encrypt with derived key
cipher_name = "aes-128-ctr"
cipher = OpenSSL::Cipher.new cipher_name
cipher.encrypt
cipher.iv = iv
cipher.key = derived_key
encrypted = cipher.update([plain_private_key].pack("H*")) + cipher.final
puts "------------ Encryption output ------------ "
puts "Cipher text = " + encrypted.unpack("H*")[0]
# Decrypt with derived key
decipher = OpenSSL::Cipher.new cipher_name
decipher.decrypt
decipher.iv = iv
decipher.key = derived_key
decrypted = decipher.update(encrypted) + decipher.final
decrypted_str = decrypted.unpack("H*")[0]
puts "------------ Decryption output ------------ "
puts "Decrypted: " + decrypted_str
puts "Decryption worked: " + (plain_private_key == decrypted_str).to_s
これが出力されます。
6fddd3d2199edf65a17d9277d2328f5357e70a5be2e173d17681883ef5a3a27e
誰でも私を助けることができますか?