13 #include <openssl/aes.h>
14 #include <openssl/sha.h>
31 AES_set_decrypt_key(decryptionKey.
begin(), 256, &
key);
39 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(s), strSalt.size() / 2,
BEGIN(prefactor), 16384, 8, 8, 32);
75 Hash((
void*)address.c_str(), address.size(), addrCheck.
begin());
78 return HexStr(addrCheck).substr(0, 8);
81 std::string
BIP38_Encrypt(std::string strAddress, std::string strPassphrase,
uint256 privKey,
bool fCompressed)
87 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(salt), strAddressHash.size() / 2,
BEGIN(hashed), 16384, 8, 8, 64);
93 uint256 block1 =
uint256((privKey << 128) ^ (derivedHalf1 << 128)) >> 128;
98 AES_set_encrypt_key(derivedHalf2.
begin(), 256, &
key);
103 uint256 dh12 = derivedHalf1 >> 128;
110 std::string strPrefix =
"0142";
111 strPrefix += (fCompressed ?
"E0" :
"C0");
116 encryptedKey = encryptedKey | (encrypted1 << 56);
119 encryptedKey = encryptedKey | (encrypted2 << (56 + 128));
126 encryptedKey = encryptedKey | (b58Checksum << 312);
132 bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey,
uint256& privKey,
bool& fCompressed)
134 std::string strKey =
DecodeBase58(strEncryptedKey.c_str());
137 if (strKey.size() != (78 + 8))
146 std::string strAddressHash = strKey.substr(6, 8);
147 std::string ownersalt = strKey.substr(14, 16);
151 fCompressed = (flag &
uint256(0x20)) != 0;
158 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(salt), strAddressHash.size() / 2,
BEGIN(hashed), 16384, 8, 8, 64);
164 DecryptAES(encryptedPart1, derivedHalf2, decryptedPart1);
167 DecryptAES(encryptedPart2, derivedHalf2, decryptedPart2);
170 uint256 temp1 = decryptedPart2 << 128;
171 temp1 = temp1 | decryptedPart1;
174 privKey = temp1 ^ derivedHalf1;
177 }
else if (type !=
uint256(0x43))
180 bool fLotSequence = (flag & 0x04) != 0;
182 std::string prefactorSalt = ownersalt;
184 prefactorSalt = ownersalt.substr(0, 8);
193 passfactor = prefactor;
208 DecryptAES(encryptedPart2, derivedHalf2, decryptedPart2);
211 uint256 x0 = derivedHalf1 >> 128;
212 uint256 x1 = decryptedPart2 ^ x0;
219 x2 = encryptedPart1 | x2;
223 uint256 x3 = derivedHalf1 &
uint256(
"0xffffffffffffffffffffffffffffffff");
224 uint256 seedbPart1 = decryptedPart1 ^ x3;
225 uint256 seedB = seedbPart1 | (seedbPart2 << 128);
237 k.
Set(privKey.
begin(), privKey.
end(), fCompressed);