-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.java
More file actions
179 lines (145 loc) · 4.46 KB
/
RSA.java
File metadata and controls
179 lines (145 loc) · 4.46 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
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.Random;
/*
* Public-key encryption implementation
*/
public class RSA {
/*
* Driver to test RSA encryption
*/
public static void main(java.lang.String[] args) {
Person Alice = new Person();
Person Bob = new Person();
String msg = new String("Bob, let's have lunch."); // message to be sent to Bob
long[] cipher;
cipher = Alice.encryptTo(msg, Bob); // encrypted, with Bob's public key
System.out.println("Message is: " + msg);
System.out.println("Alice sends:");
show(cipher);
System.out.println("Bob decodes and reads: " + Bob.decrypt(cipher)); // decrypted,
// with Bob's private key.
System.out.println();
msg = new String("No thanks, I'm busy");
cipher = Bob.encryptTo(msg, Alice);
System.out.println("Message is: " + msg);
System.out.println("Bob sends:");
show(cipher);
System.out.println("Alice decodes and reads: " + Alice.decrypt(cipher));
}
/*
* Find the multiplicative inverse of a long int
* @author Geoff Cohen
* @return The inverse of e, mod m. Uses the extended Eulidean Algorithm
*/
public static long inverse(long e, long m)
{
long makePositive = m;
long u = 1, v = 0;
if (m == 1)
return 0;
//EEA
while (e > 1 && m != 0)
{
long q = e / m; // divide repeatedly
long temp = m; // for holding original m
m = e % m; // r
e = temp; // move m down
temp = v; // for holding v
v = u - q * v; // calculate new v
u = temp; // calculate new u
}
if (u < 0) //in case negative result
u += makePositive;
return u;
}
/*
* Display an array of longs on stdout
* @author Dylan Chow
*/
public static void show(long[] cipher)
{
for(int i = 0; i < cipher.length; i++)
{
System.out.print(cipher[i]);
}
System.out.println();
}
/*
* Raise a number, b, to a power, p, modulo m
* @author Geoff Cohen
* @return bp mod m
*/
public static long modPower(long b, long p, long m)
{
b = b % m; // mod number
long result = 1;
while (p > 0) { // Reduce power through shift operations; 0/1 is base case
if((p & 1)==1) // When power is reduced to 1, stop and calculate result
result = (result * b) % m;
b = (b * b) % m; // Increment b by one power
p = p >> 1; // Decrement p by one power
}
return result;
}
/*
* Find a random prime number
* @author Dave Smits, Dylan Chow
* @return A random prime in the range m..n, using rand to generate the number
*/
public static long randPrime(int m, int n, Random rand) {
long num = (rand.nextInt(n) % ((n - m) + 1)) + n;
return num;
}
/*
* Find a random number relatively prime to a given long int
* @author Dave Smits, Dylan Chow
* @return a random number relatively prime to n
*/
public static long relPrime(long n, Random rand) {
long t = rand.nextInt((int) n); //get Random t Long
for(int i = 2; i < t; i++)
{
if(t % i == 0)
return relPrime(n, rand); // t was not prime
}
return t;
}
/*
* Convert two numeric chars to long int
*
* @author Dave Smits, Geoff Cohen
*
* @return the two digit number beginning at position p of msg as a long int.
*/
public static long toLong(java.lang.String msg, int p) {
long lr = 0;
//byte l = (byte) msg.substring(p, p + 2).charAt(0);
//byte r = (byte) msg.substring(p, p + 2).charAt(1);
if(p + 2 < msg.l ength())
{
l = (byte) msg.substring(p, p + 2).charAt(0);
r = (byte) msg.substring(p, p + 2).charAt(1);
}
else
{
l = (byte) msg.substring(p, p+1).charAt(0);
r = 0;
}
lr = l;
lr = lr << 8;
lr += r;
return lr;
}
/*
* Convert a long to 2 chars
*
* @author Dave Smits, Geoff Cohen
*
* @return The string made up two numeric digits representing x
*/
public static String longTo2Chars(long x) {
byte r = (byte) x;
long t = x >> 8;
byte l = (byte) t;
return (char) l + (char) r + "";
}
}