-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
215 lines (182 loc) · 11 KB
/
Main.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import javax.crypto.AEADBadTagException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.spec.KeySpec;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static final int ITERATIONS = 4096;
private static final int AES_KEY_SIZE = 128;
private static final int GCM_TAG_LENGTH = 16;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the path to the wallet to attempt recovery on (ex: /path/to/wallet.dat): ");
String walletPath = scan.nextLine();
boolean walletExists = false;
while (!walletExists) {
File test = new File(walletPath);
if (test.exists()) {
walletExists = true;
} else {
System.out.println("The file " + test.getAbsolutePath() + " does not exist! Please enter the full path to the wallet to attempt recovery on: ");
walletPath = scan.nextLine();
}
}
File walletFile = new File(walletPath);
System.out.println("Wallet file " + walletFile.getAbsolutePath() + " exists!");
System.out.println("Please enter the passphrase the wallet is encrypted with: ");
String passphrase = scan.nextLine();
scan.close();
System.out.println("Starting wallet recovery process on file " + walletFile.getAbsolutePath() + "...");
phase1(walletPath, passphrase.toCharArray());
}
public static void phase1(String path, char[] passphrase) {
System.out.println("Phase 1: Attempting to import wallet using standard model...");
int totalAddressCount = 0;
StoredWallet importedWallet = null;
StoredWallet functionalWallet = new StoredWallet();
StoredWallet paddingExceptionWallet = new StoredWallet();
StoredWallet genericExceptionWallet = new StoredWallet();
try (FileInputStream stream = new FileInputStream(path)) {
WalletV2Serializer serializer = new WalletV2Serializer();
importedWallet = serializer.read(stream);
functionalWallet.version = importedWallet.version;
functionalWallet.keyType = importedWallet.keyType;
functionalWallet.locked = importedWallet.locked;
functionalWallet.defaultAddress = importedWallet.defaultAddress;
functionalWallet.addresses = new ArrayList<>();
paddingExceptionWallet.version = importedWallet.version;
paddingExceptionWallet.keyType = importedWallet.keyType;
paddingExceptionWallet.locked = importedWallet.locked;
paddingExceptionWallet.defaultAddress = importedWallet.defaultAddress;
paddingExceptionWallet.addresses = new ArrayList<>();
genericExceptionWallet.version = importedWallet.version;
genericExceptionWallet.keyType = importedWallet.keyType;
genericExceptionWallet.locked = importedWallet.locked;
genericExceptionWallet.defaultAddress = importedWallet.defaultAddress;
genericExceptionWallet.addresses = new ArrayList<>();
for (StoredAddress a : importedWallet.addresses) {
totalAddressCount++;
EncryptedInfo unlocked = new EncryptedInfo();
boolean success = false;
try {
unlocked.cipherText = decrypt(a.cipher, passphrase);
a.cipher = unlocked;
success = true;
functionalWallet.addresses.add(a);
} catch (AEADBadTagException e) {
System.out.println("Encountered an AEADBadTagException parsing address " + a.address + ":");
e.printStackTrace();
paddingExceptionWallet.addresses.add(a);
} catch (Exception e) {
System.out.println("Encountered an unexpected exception parsing address " + a.address + ":");
e.printStackTrace();
genericExceptionWallet.addresses.add(a);
}
}
System.out.println("Addresses in original encrypted wallet: " + totalAddressCount);
System.out.println("Recovered encrypted wallet addresses: " + functionalWallet.addresses.size());
System.out.println("Encrypted addresses with a padding exception during decryption: " + paddingExceptionWallet.addresses.size());
System.out.println("Encrypted addresses with a generic exception during decryption: " + genericExceptionWallet.addresses.size());
for (int i = 0; i < paddingExceptionWallet.addresses.size(); i++) {
StoredAddress a = paddingExceptionWallet.addresses.get(i);
System.out.println("Padding Exception Address (" + i + "/" + paddingExceptionWallet.addresses.size() + "):");
System.out.println("\tAddress: " + a.address);
System.out.println("\tEncrypted Public Key: " + Utility.bytesToBase64(a.publicKey));
System.out.println("\tCipher: ");
System.out.println("\t\tSalt: " + Utility.bytesToBase64(a.cipher.salt));
System.out.println("\t\tIV: " + Utility.bytesToBase64(a.cipher.iv));
System.out.println("\t\tAdditional Data: " + Utility.bytesToBase64(a.cipher.additionalData));
System.out.println("\t\tCipher Text: " + Utility.bytesToBase64(a.cipher.cipherText));
System.out.println("");
}
for (int i = 0; i < genericExceptionWallet.addresses.size(); i++) {
StoredAddress a = genericExceptionWallet.addresses.get(i);
System.out.println("Generic Exception Address (" + i + "/" + genericExceptionWallet.addresses.size() + "):");
System.out.println("\tAddress: " + a.address);
System.out.println("\tEncrypted Public Key: " + Utility.bytesToBase64(a.publicKey));
System.out.println("\tCipher: ");
System.out.println("\t\tSalt: " + Utility.bytesToBase64(a.cipher.salt));
System.out.println("\t\tIV: " + Utility.bytesToBase64(a.cipher.iv));
System.out.println("\t\tAdditional Data: " + Utility.bytesToBase64(a.cipher.additionalData));
System.out.println("\t\tCipher Text: " + Utility.bytesToBase64(a.cipher.cipherText));
System.out.println("");
}
if (functionalWallet.addresses.size() > 0) {
String phase1FunctionalWalletName = "recovered.dat";
File phase1FunctionalWalletFile = new File(phase1FunctionalWalletName);
System.out.println(
"Writing " + functionalWallet.addresses.size() + " keys with no exception to: " + phase1FunctionalWalletFile.getAbsolutePath());
saveWalletToFile(functionalWallet, phase1FunctionalWalletFile);
System.out.println("Done writing wallet file " + phase1FunctionalWalletFile.getAbsolutePath() + "!");
} else {
System.out.println("No keys were recovered that did not have a decryption exception. Skipping writing standard recovery wallet.");
System.out.println("!!! PLEASE MAKE SURE THAT THE DECRYPTION KEY YOU ENTERED IS CORRECT !!!");
}
if (paddingExceptionWallet.addresses.size() > 0) {
String phase1PaddingExceptionWalletName = "paddingexception.dat";
File phase1PaddingExceptionWalletFile = new File(phase1PaddingExceptionWalletName);
System.out.println("Writing " + paddingExceptionWallet.addresses.size() + " keys with padding exeptions to: "
+ phase1PaddingExceptionWalletFile.getAbsolutePath());
saveWalletToFile(paddingExceptionWallet, phase1PaddingExceptionWalletFile);
System.out.println("Done writing wallet file " + phase1PaddingExceptionWalletFile.getAbsolutePath() + "!");
} else {
System.out.println("No keys were found with any padding decryption exceptions! Not writing padding exception wallet.");
}
if (genericExceptionWallet.addresses.size() > 0) {
String phase1GenericExceptionWalletName = "genericexception.dat";
File phase1GenericExceptionWalletFile = new File(phase1GenericExceptionWalletName);
System.out.println("Writing " + genericExceptionWallet.addresses.size() + " keys with generic exceptions to: "
+ phase1GenericExceptionWalletFile.getAbsolutePath());
saveWalletToFile(genericExceptionWallet, phase1GenericExceptionWalletFile);
System.out.println("Done writing wallet file " + phase1GenericExceptionWalletFile.getAbsolutePath() + "!");
} else {
System.out.println("No keys were found with any generic decryption exceptions! Not writing generic exception wallet.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean saveWalletToFile(StoredWallet wallet, File walletDatFile) {
if (walletDatFile.exists()) {
System.out.println("Cannot save wallet to file " + walletDatFile.getAbsolutePath() + ", the file already exists! Exiting...");
System.exit(0);
}
try (FileOutputStream stream = new FileOutputStream(walletDatFile, false)) {
WalletSerializer serializer = new WalletV2Serializer();
serializer.write(wallet, stream);
return true;
} catch (IOException e) {
String errorContext = "An error occurred while attempting to save the wallet to the file " + walletDatFile + "!";
e.printStackTrace();
return false;
}
}
public static byte[] decrypt(EncryptedInfo encrypted, char[] passphrase) throws AEADBadTagException, Exception {
if (encrypted.salt == null || encrypted.iv == null || encrypted.additionalData == null) {
return encrypted.cipherText;
}
try {
KeySpec keySpec = new PBEKeySpec(passphrase, encrypted.salt, ITERATIONS, AES_KEY_SIZE);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey secretKey = factory.generateSecret(keySpec);
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, encrypted.iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey.getEncoded(), 0, 16, "AES"), spec);
cipher.updateAAD(encrypted.additionalData);
return cipher.doFinal(encrypted.cipherText);
} catch (AEADBadTagException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
}