-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGuessPrivateKey.java
148 lines (128 loc) · 4.83 KB
/
GuessPrivateKey.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
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashSet;
public class GuessPrivateKey{
public static void main(String[] args){
if(args.length < 2 || args.length > 4){
System.out.println("Wrong arguments. Please see instruction at https://github.com/scorta/GuessPrivateKey");
} else {
int nThread = Integer.parseInt(args[0]);
String fileName = args[1];
GuessKeyThread.CHOICE choice = GuessKeyThread.CHOICE.random;
String start = "0";
if (args.length >= 3) {
choice = GuessKeyThread.CHOICE.valueOf(args[2]);
}
if(args.length == 4){
start = args[3];
}
GuessKeyThread[] guessKeyThreads = new GuessKeyThread[nThread];
System.out.println("Searching keys for Bitcoin address(es) from file " + fileName + " with " + nThread + " thread(s)");
GuessKeyThread.readListAddress(fileName);
GuessKeyThread.step = nThread;
GuessKeyThread.choice = choice;
for (int i = 0; i < nThread; ++i) {
if(choice == GuessKeyThread.CHOICE.random){
guessKeyThreads[i] = new GuessKeyThread();
guessKeyThreads[i].start();
} else {
BigInteger bigInteger = new BigInteger(start);
guessKeyThreads[i] = new GuessKeyThread(bigInteger.add(BigInteger.valueOf(i)));
guessKeyThreads[i].start();
}
}
}
}
}
class GuessKeyThread extends Thread {
public enum CHOICE {random, up, down};
public static int step;
public static CHOICE choice;
private static HashSet<String> bitAddress = new HashSet<>();
private static NetworkParameters netParams = MainNetParams.get();
private BigInteger start;
GuessKeyThread(BigInteger start){
this.start = start;
}
GuessKeyThread(){}
public void run(){
System.out.println("Run in \"" + choice + "\" mode, checking from: " + start);
if(choice == CHOICE.random) {
searchForKey();
} else if(choice == CHOICE.up) {
searchForKeyInRangeUpper();
} else {
searchForKeyInRangeLower();
}
}
public static void readListAddress(String file) {
if(bitAddress.isEmpty()) {
try {
System.out.println("Start importing list Bitcoin address(es)");
BufferedReader br = new BufferedReader(new FileReader(file));
for (String line; (line = br.readLine()) != null; ) {
bitAddress.add(line.split(";")[0]);
}
System.out.println("Imported " + bitAddress.size() + " " + "address(es)");
} catch (Exception e) {
System.out.println("Error at importing list Bitcoin Address. Error: " + e.toString());
}
}
}
private static boolean foundInSet(ECKey ecKey){
if(bitAddress.contains(ecKey.toAddress(netParams).toString())){
return true;
} else {
return false;
}
}
private static void printResult(ECKey ecKey){
System.out.println("Found a key!!!");
String addr = ecKey.toAddress(netParams).toString();
String keyWIF = ecKey.getPrivateKeyAsWiF(netParams);
String keyHex = ecKey.getPrivateKeyAsHex();
try {
PrintWriter out = new PrintWriter(keyWIF + ".txt");
out.println(addr);
out.println(keyWIF);
out.println(keyHex);
out.close();
} catch (Exception e){
System.out.println("---");
System.out.println("Output error: " + e.toString());
System.out.println(addr + " | " + keyWIF + " | " + keyHex);
System.out.println("---");
}
}
private void checkKey(ECKey ecKey){
if(foundInSet(ecKey) || foundInSet(ecKey.decompress())){
printResult(ecKey);
}
}
private void searchForKey(){
ECKey key;
while (true){
key = new ECKey();
checkKey(key);
}
}
private void searchForKeyInRangeUpper(){
ECKey key;
for(BigInteger bigIntKey = start;; bigIntKey = bigIntKey.add(BigInteger.valueOf(step))){
key = ECKey.fromPrivate(bigIntKey);
checkKey(key);
}
}
private void searchForKeyInRangeLower(){
ECKey key;
for(BigInteger bigIntKey = start;; bigIntKey = bigIntKey.subtract(BigInteger.valueOf(step))){
key = ECKey.fromPrivate(bigIntKey);
checkKey(key);
}
}
}