-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAEncryption.java
More file actions
155 lines (125 loc) · 4.37 KB
/
RSAEncryption.java
File metadata and controls
155 lines (125 loc) · 4.37 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
class RSAEcryptionSimulation {
public static void main(String[] args) throws Exception {
Machine sender = new Machine(11, 13);
Machine receiver = new Machine(17, 23);
receiver.RSAMakePublicKey();
receiver.RSAMakePrivateKey();
int msg = 11;
int sendmsg = sender.RSAEncodeMsg(receiver.publicKey, msg);
System.out.println("orginal message: " + msg);
System.out.println("encrypted msg: " + sendmsg);
int receivemsg = receiver.RSADecodeMsg(sendmsg);
System.out.println("decrypted msg: " + receivemsg);
}
}
class EncryptionHelper {
// ***In Java, % returns remainder, not modulo
public static int mod(int num, int modulus) {
int rem = num % modulus;
if (rem < 0) rem += modulus;
return rem;
}
// ***In Java, % returns remainder, not modulo
public static int mod(double num, int modulus) {
int rem = (int) (num % modulus);
if (rem < 0) rem += modulus;
return rem;
}
// x^y mod N
public static int modexp(int x, int y, int N) {
if (y == 0) return 1;
int z = modexp(x, y >> 1, N);
int pow = (int) Math.pow(z, 2);
if (mod(y, 2) == 0) {
return mod(pow, N);
} else {
return mod(pow * x, N);
}
}
// GCD
// Euclid's rule - if a & b are positive integers, a >= b
// then gcd(a, b) = gcd(a mod b, b)
public static int euclid(int a, int b) {
if (b == 0) return a;
return euclid(b, mod(a, b));
}
// check if num is a prime number
// if prime - return true
public static boolean primality(int num) {
for (int i=1; i<num; i++) {
if (modexp(i, num-1,num) != 1) {
return false;
}
}
return true;
}
// Extended GCD
// Lemma - if d divides both a and b, and d = ax + by for some x and y
// then d = gcd(a, b)
// below method finds x and y
public static int[] extendedEuclid(int a, int b) {
// result[0] - x
// result[1] - y
// result[2] - GCD of a and b
if (b == 0) {
int[] result = {1, 0, a};
return result;
}
int[] result = extendedEuclid(b, mod(a, b));
int temp = result[0];
result[0] = result[1];
result[1] = temp - Math.floorDiv(a, b) * result[0];
return result;
}
}
class PublicKeyPair {
int modulus;
int encode;
}
class Machine {
private int secret;
private int p;
private int q;
public PublicKeyPair publicKey;
public Machine(int setp, int setq) throws Exception {
//first check p and q are both primes
if (!EncryptionHelper.primality(p)) {
throw new Exception("p not a prime number");
}
if (!EncryptionHelper.primality(q)) {
throw new Exception("q not a prime number");
}
p = setp;
q = setq;
}
public void RSAMakePublicKey() {
// proceed to make public key pair
this.publicKey = new PublicKeyPair();
// first index is modulus
// second index is the key
//modulus
this.publicKey.modulus = this.p*this.q;
// key
int keyCandidate = 3;
while (EncryptionHelper.euclid(keyCandidate, (this.p-1)*(this.q-1)) != 1) keyCandidate++;
this.publicKey.encode = keyCandidate;
}
public void RSAMakePrivateKey() {
// result = [x, y, d] where ax + by = d calling extendedEuclid(a. b)
int modulus = (this.p-1)*(this.q-1);
int[] result = EncryptionHelper.extendedEuclid(this.publicKey.encode, modulus);
this.secret = EncryptionHelper.mod(result[0], modulus);
}
public int RSAEncodeMsg(PublicKeyPair publicKey, int msg) throws Exception {
if (publicKey == null) throw new Exception("cannot encode with empty public key");
int encodedMsg =
EncryptionHelper.modexp(msg, publicKey.encode, publicKey.modulus);
return encodedMsg;
}
public int RSADecodeMsg(int msg) throws Exception {
if (this.publicKey == null) throw new Exception("public key haven't been set");
if (this.secret == 0) throw new Exception("private key haven't been set");
int decodedMsg = EncryptionHelper.modexp(msg, this.secret, this.publicKey.modulus);
return decodedMsg;
}
}