I'm trying to decrypt an encrypted private key from an encrypted Multibit Classic .wallet file but the resulting public key / wif doesn't match the original.
Here is my code:
'use strict'
var fs = require('fs')
var crypto = require('crypto')
var scrypt = require('scrypt')
var protobuf = require('protobufjs')
var bitcore = require('bitcore-lib')
var network = bitcore.Networks.livenet
function parseWallet (wallet, cb) {
protobuf.load('bitcoin.proto', function (err, root) {
if (err) cb(err, null)
var buffer = fs.readFileSync(wallet)
var walletMessage = root.lookup('wallet.Wallet')
var message = walletMessage.decode(buffer)
console.log('protobuf:', JSON.stringify(message, 0, 1))
message.key.forEach(function (k) {
var res = {}
var address = new bitcore.PublicKey(k.publicKey.toString('hex'))
res.encryptionType = message.encryptionType
res.pubkey = address.toAddress(network).toString()
res.iv = k.encryptedPrivateKey.initialisationVector
res.salt = message.encryptionParameters.salt
res.encryptedPrivateKey = k.encryptedPrivateKey.encryptedPrivateKey
cb(null, res)
})
// var message = walletMessage.decode(buffer)
})
}
parseWallet('dev.wallet', function (err, res) {
if (err) throw err
console.log('parsed:', res)
var passphrase = new Buffer('dev123456')
var scryptParams = {N: 16384, r: 8, p: 1}
var derivedKeySize = 32
var derivedKey = scrypt.hashSync(passphrase, scryptParams, derivedKeySize, res.salt)
var decipher = crypto.createDecipheriv('aes-256-cbc', derivedKey, res.iv)
var decryptedPrivateKey = decipher.update(res.encryptedPrivateKey)
var privateKey = new bitcore.PrivateKey(decryptedPrivateKey.toString('hex'))
var publicKey = privateKey.toPublicKey()
var address = publicKey.toAddress()
var output = {}
output.decryptedPrivateKey = decryptedPrivateKey.toString('hex')
output.pubKey = address.toString()
console.log('output:', JSON.stringify(output, 0, 0))
})
The public key / private key doesn't match after the decryption. I suspect that I'm missing something about encoding and I would appreciate any help.
protobuf: {
"networkIdentifier": "org.bitcoin.production",
"lastSeenBlockHash": "AAAAAAAAAAAzUIO4ArpXPqbCZNnaltVmjJsWob3cLb4=",
"key": [
{
"encryptedPrivateKey": {
"initialisationVector": "CiKlKV+J1LhmMZWtiPHJKA==",
"encryptedPrivateKey": "0C7HTASrcjsvPHxZE5OLr2MS4oFdfEC4BID81WH+prFhUunkQNb8itg4vimaBln5"
},
"publicKey": "AqrhHqufQRw63h4EDHK7ylZlWktTZxANdPpGun6JKwsO",
"creationTimestamp": "1486547219000"
}
],
"encryptionType": "ENCRYPTED_SCRYPT_AES",
"encryptionParameters": {
"salt": "J7kt6NuBTIo="
},
"majorVersion": 2,
"extension": [
{
"id": "org.multibit.walletProtect.2",
"data": "AA==",
"mandatory": true
}
]
}
parsed: { encryptionType: 2,
pubkey: '1MWq1EiwYh79GHu9PLnVBuPQu66Fs2ph6Z',
iv: <Buffer 0a 22 a5 29 5f 89 d4 b8 66 31 95 ad 88 f1 c9 28>,
salt: <Buffer 27 b9 2d e8 db 81 4c 8a>,
encryptedPrivateKey: <Buffer d0 2e c7 4c 04 ab 72 3b 2f 3c 7c 59 13 93 8b af 63 12 e2 81 5d 7c 40 b8 04 80 fc d5 61 fe a6 b1 61 52 e9 e4 40 d6 fc 8a d8 38 be 29 9a 06 59 f9> }
output: {"decryptedPrivateKey":"ae0ee2bacc68e3f67dce176a99663ced485885a45f685208794c5356c7795a25","pubKey":"1By9rGSNZDMXCv8iSaZF5BpiMnpZKx5k4e"}