-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRSAbreaker.java
181 lines (156 loc) · 5.93 KB
/
RSAbreaker.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
////////////////
//LICENSE
////////////////
// Java RSA cracking library VERSION 1.0 Please visit the project's website at: https://github.com/cybernova/RSAbreaker
// Copyright (C) 2017 Andrea Dari (andreadari91@gmail.com)
//
// This library 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
// any later version.
//
// This program 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 library. If not, see <http://www.gnu.org/licenses/>.
import java.math.BigInteger;
public final class RSAbreaker {
private RSAbreaker() {}
/**
* Convert hexadecimal pbKey/pbExp to BigInteger pbKey/pbExp
* @param hex pbKey/pbExp in hexadecimal format
* @return pbKey/pbExp in integer format
*/
public static BigInteger hexToBigInteger(String hex) {
String digits = "0123456789ABCDEF";
hex = hex.toUpperCase();
BigInteger val = BigInteger.ZERO;
for (int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
int d = digits.indexOf(c);
val = val.multiply(BigInteger.valueOf(16)).add(BigInteger.valueOf(d));
}
return val;
}
/**
* Convert BigInteger pbKey/pbExp to hexadecimal pbKey/pbExp
* @param key pbKey/pbExp in integer format
* @return pbKey/pbExp in hexadecimal format
*/
public static String bigIntegerToHex(BigInteger key) {
String digits = "0123456789ABCDEF";
if (key.compareTo(BigInteger.ZERO) == 0) return "0";
String hex = "";
while (key.compareTo(BigInteger.ZERO) > 0) {
BigInteger digit = key.mod(BigInteger.valueOf(16));
hex = digits.charAt(digit.intValueExact()) + hex;
key = key.divide(BigInteger.valueOf(16));
}
return hex;
}
/**
* Calculate key length
* @param key Public key to crack
* @return Public key size in bits
*/
public static int keyLength(BigInteger key) {
return key.bitLength();
}
/**
* Calculate key length
* @param hex Public key to crack in hexadecimal format
* @return Public key size in bits
*/
public static int keyLength(String hex) {
return hexToBigInteger(hex).bitLength();
}
/**
* BruteForce cracking algorithm
* @param pbKey Public key to crack
* @param pbExp Public exponent of the key to crack
*/
public static void bruteForce(BigInteger pbKey, BigInteger pbExp) {
BigInteger p = bigIntSqRootFloor((BigInteger)pbKey);
while (pbKey.mod(p).compareTo(BigInteger.ZERO) != 0) {
p = p.subtract(BigInteger.ONE);
}
BigInteger q = pbKey.divide(p);
calcPrivateExp(p, q, pbExp);
}
/**
* Pollard's Rho cracking algorithm
* @param pbKey Public key to crack
* @param pbExp Public exponent of the key to crack
*/
public static void pollardRho(BigInteger pbKey, BigInteger pbExp) {
BigInteger p;
BigInteger x = new BigInteger("2");
BigInteger y = x;
do {
x = x.multiply(x).add(BigInteger.ONE).mod(pbKey);
y = y.multiply(y).add(BigInteger.ONE).mod(pbKey);
y = y.multiply(y).add(BigInteger.ONE).mod(pbKey);
p = x.subtract(y).gcd(pbKey);
} while (p.compareTo(BigInteger.ONE) == 0);
if (p.compareTo(pbKey) == 0)
return;
BigInteger q = pbKey.divide(p);
calcPrivateExp(p, q, pbExp);
}
/**
* Fermat cracking algorithm
* @param pbKey Public key to crack
* @param pbExp Public exponent of the key to crack
*/
public static void fermat(BigInteger pbKey, BigInteger pbExp) {
BigInteger a = bigIntSqRootCeil(pbKey);
BigInteger b2 = a.multiply(a).subtract(pbKey);
while ( bigIntSqRootCeil(b2).compareTo(bigIntSqRootFloor(b2)) != 0 )
{
a = a.add(BigInteger.ONE);
b2 = a.multiply(a).subtract(pbKey);
}
BigInteger p = a.subtract(bigIntSqRootFloor(b2));
BigInteger q = pbKey.divide(p);
calcPrivateExp(p, q, pbExp);
}
private static void calcPrivateExp(BigInteger p, BigInteger q, BigInteger exp) {
BigInteger mod = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger d = exp.modInverse(mod);
System.out.println(p + " " + q + " " + d);
}
private static BigInteger bigIntSqRootFloor(BigInteger x) throws IllegalArgumentException {
if (x.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException("Negative argument.");
}
if (x.compareTo(BigInteger.ZERO) == 0 || x.compareTo(BigInteger.ONE) == 0) {
return x;
}
BigInteger two = BigInteger.valueOf(2);
BigInteger y = x.divide(two);
while (y.compareTo(x.divide(y)) > 0) {
y = x.divide(y).add(y).divide(two);
}
return y;
}
private static BigInteger bigIntSqRootCeil(BigInteger x) throws IllegalArgumentException {
if (x.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException("Negative argument.");
}
if (x == BigInteger.ZERO || x == BigInteger.ONE) {
return x;
}
BigInteger two = BigInteger.valueOf(2);
BigInteger y = x.divide(two);
while (y.compareTo(x.divide(y)) > 0) {
y = x.divide(y).add(y).divide(two);
}
if (x.compareTo(y.multiply(y)) == 0) {
return y;
}
return y.add(BigInteger.ONE);
}
}