-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAAlgorithm.java
More file actions
166 lines (144 loc) · 7.44 KB
/
RSAAlgorithm.java
File metadata and controls
166 lines (144 loc) · 7.44 KB
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
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Random;
public class RSAAlgorithm {
// the two primes
private BigInteger p;
private BigInteger q;
// the public base
private BigInteger n;
// the totient
private BigInteger phiN;
// the public exponent coprime (gcd is 1) with phiN (used for encryption)
// (publicKey)
private BigInteger a;
// the private exponent (used for decryption) in the linear combination x*a +
// y*phiN = 1 (privateKey)
private BigInteger x;
// the coefficient of the totient in the linear combination x*a + y*phiN = 1
private BigInteger y;
public RSAAlgorithm(BigInteger p, BigInteger q) {
this.p = p;
this.q = q;
this.n = p.multiply(q);
this.phiN = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger gcd;
// choose the exponent by guessing numbers randomly until one is relatively
// prime to the totient
do {
this.a = BigIntegerUtils.chooseCoprime(this.phiN);
// find a linear combination of `a` and `phiN` that equals 1 using the extended
// Euclidean Algorithm
BigInteger[] coefficients = EuclideanAlgorithm.extendedEuclidean(a, phiN);
// parse `x` and `y`
gcd = this.a.gcd(this.phiN);
this.x = coefficients[1];
this.y = coefficients[2];
} while (!gcd.equals(BigInteger.ONE));
}
public int isValidInput(String[] blocks) {
int numOfBlocks = blocks.length;
for (int i = 0; i < numOfBlocks; i++) {
// error:
BigInteger m = new BigInteger(blocks[i]);
int digitsInBlock = blocks[i].length();
// decoding and then encoding again is inconsistent
if (encode(decode(m)) == m)
return 1;
// the number of digits in any block should be less than the number of digits in
// either of the primes chosen
if (digitsInBlock >= p.toString().length() || digitsInBlock >= q.toString().length())
return 2;
}
// input is completely valid
return 0;
}
public BigInteger[] getPublicKey() {
return new BigInteger[] { n, a };
}
public BigInteger encode(BigInteger c) {
return c.modPow(a, n);
}
public BigInteger decode(BigInteger c) {
return c.modPow(x, n);
}
public String getExample(String message, HashMap<Character, String> cipherTable) {
BigInteger min = BigInteger.ONE;
BigInteger max = new BigInteger("100");
BigInteger random = BigIntegerUtils.randomBigInteger(min, max);
BigInteger randomEncoded = encode(random);
BigInteger randomEncodedDecoded = decode(randomEncoded);
// Use a StringBuilder to build the string
StringBuilder example = new StringBuilder(1000);
example.append("\\paragraph{Example} Take $p = ").append(p).append("$ and $q = ").append(q)
.append("$ to be our two primes $p$, $q$. So $n = ").append(n).append("$ and $\\phi(n) = (")
.append(p).append(" - 1)(").append(q).append(" - 1) = ").append(phiN)
.append("$.\n\nWe choose an integer $a$ relatively prime to $\\phi(n) = ").append(phiN)
.append("$: Say $a = ").append(a).append("$. Express $1$ as a linear combination of $")
.append(phiN).append("$ and $").append(a).append("$:\n$$").append(x).append(" \\cdot ")
.append(a).append(y.signum() >= 0 ? " + " : " - ").append(y.abs()).append(" \\cdot ")
.append(phiN).append(" = 1$$\nso `$x$' is $").append(x).append("$. We publish $(n, a) = (")
.append(n).append(", ").append(a)
.append(")$.\n\nTo encode a block $\\beta$, the sender calculates $\\beta^{")
.append(a).append("} \\mod ").append(n)
.append("$, and to decode a received block $m$, we calculate $m^{")
.append(x).append("} \\mod ").append(n)
.append("$.\n\nThus, for example, to encode the message $\\beta = ")
.append(random).append("$, the sender computes\n$$").append(random).append("^{").append(a)
.append("} \\mod ").append(n).append(" = ").append(randomEncoded).append(" \\mod ").append(n)
.append("$$\nand so sends $m = ").append(randomEncoded)
.append("$. On receipt of this message, anyone who knows `$x$' (the inverse of $").append(a)
.append(" \\mod ").append(n).append("$) computes $").append(randomEncoded).append("^{")
.append(x).append("} \\mod ").append(n).append("$ which is equal to the original message $")
.append(randomEncodedDecoded).append("$.\n\nIf now we use the number-to-letter equivalents:\n")
.append(HashMapUtils.hashMapToLatexTable(cipherTable)).append("and the received message is $");
// configure the specific string example using `message` and `cipherTable`
int messageLength = message.length();
int numOfBlocks = messageLength / 2; // encode in blocks of two characters (reduces operations)
String[] blocks = new String[numOfBlocks];
StringBuilder sb = new StringBuilder();
int sizeChar = cipherTable.get(message.charAt(0)).length();
for (int i = 0; i < messageLength; i++) {
sb.append(cipherTable.get(message.charAt(i)));
if (i % 2 == 1) {
blocks[i / 2] = encode(new BigInteger(sb.toString())).toString();
example.append(new BigInteger(blocks[i / 2]))
.append(i == messageLength - 1 ? "$, " : "/");
sb.setLength(0);
}
}
example.append("the original message is decoded by calculating\n");
for (int i = 0; i < numOfBlocks; i++) {
BigInteger valueM = new BigInteger(blocks[i]);
BigInteger valueBeta = decode(valueM);
while (sb.length() + valueBeta.toString().length() < 2 * sizeChar)
sb.append("0");
sb.append(valueBeta.toString());
blocks[i] = sb.toString();
show("Block " + i + ": " + blocks[i]);
sb.setLength(0);
example.append("$$").append(valueM).append("^{").append(x).append("} \\equiv ")
.append(valueBeta).append(" \\mod ").append(n).append("$$\n")
.append(i == numOfBlocks - 1 ? "\n" : "and\n");
}
example.append("Juxtaposing these blocks gives ");
for (int i = 0; i < numOfBlocks; i++) {
example.append(blocks[i]);
}
example.append(", and so the message was the word ");
for (int i = 0; i < numOfBlocks; i++) {
for (int j = 0; j < 2; j++) {
example.append(HashMapUtils.getKeyFromValue(cipherTable,
blocks[i].substring(j * sizeChar, (j + 1) * sizeChar)))
.append(i == numOfBlocks - 1 && j == sizeChar ? ".\n\n" : " ");
}
}
if (isValidInput(blocks) == 2)
example.append(
"(In this example we used small primes for purposes of illustration but, in doing so, violated the requirement that the number of digits in any block should be less than the number of digits in either of the primes chosen.)\n\n");
return example.toString();
}
private void show(String s) {
System.out.println(s);
}
}