/
extract-multibit-privkey.py
executable file
·70 lines (57 loc) · 2.78 KB
/
extract-multibit-privkey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# extract-multibit-privkey.py -- MultiBit private key extractor
# Copyright (C) 2014, 2015 Christopher Gurnee
#
# This file is part of btcrecover.
#
# btcrecover is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# btcrecover is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
# If you find this program helpful, please consider a small
# donation to the developer at the following Bitcoin address:
#
# 3Au8ZodNHPei7MQiSVAWb7NB2yqsb48GW4
#
# Thank You!
from __future__ import print_function
import sys, os.path, base64, zlib, struct
prog = os.path.basename(sys.argv[0])
if len(sys.argv) != 2 or sys.argv[1].startswith("-"):
print("usage:", prog, "MULTIBIT_PRIVATE_KEY_FILE", file=sys.stderr)
sys.exit(2)
privkey_filename = sys.argv[1]
with open(privkey_filename, "rb") as privkey_file:
# Multibit privkey files contain base64 text split into multiple lines;
# we need the first 32 bytes after decoding, which translates to 44 before.
base64_encoded = "".join(privkey_file.read(50).split()) # join multiple lines into one
if len(base64_encoded) < 44:
print(prog+": error: file is not a MultiBit private key file (too short)", file=sys.stderr)
sys.exit(1)
try: salt_privkey = base64.b64decode(base64_encoded[:44])
except:
print(prog+": error: file is not a MultiBit private key file (not base64 encoded)", file=sys.stderr)
sys.exit(1)
if not salt_privkey.startswith(b"Salted__"):
print(prog+": error: file is not a MultiBit private key file", file=sys.stderr)
sys.exit(1)
if len(salt_privkey) < 32:
print(prog+": error: file is not a MultiBit private key file (too short)", file=sys.stderr)
sys.exit(1)
print("\nWARNING: please read the important warning in the Usage for MultiBit\n"
" Classic section of Extract_Scripts.md before proceeding.\n")
print("MultiBit partial first encrypted private key, salt, and crc in base64:", file=sys.stderr)
# salt_privkey[8:32] now consists of:
# 8 bytes of salt, followed by
# 1 16-byte encrypted aes block containing the first 16 base58 chars of a 52-char encoded private key
bytes = b"mb:" + salt_privkey[8:32]
crc_bytes = struct.pack("<I", zlib.crc32(bytes) & 0xffffffff)
print(base64.b64encode(bytes + crc_bytes))