-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.java
269 lines (213 loc) · 10.4 KB
/
rsa.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// package com.nocial;
// import java.util.ArrayList;
// import java.util.List;
// public class EncryptionTest {
// // Declare static list
// private static List<Integer> myListOfE = new ArrayList<>();
// public static void main(String[] args) {
// // Call method to print information
// printFirstInformation();
// // Create an object for my class FindPrimes
// FindPrimes primeValue = new FindPrimes();
// // Calls public method to get user input for number of rounds
// int numberOfRounds = primeValue.getRounds();
// // Assigns prime numbers to p and q from the return of public method
// int p = primeValue.getPrimeNumber(numberOfRounds);
// int q = primeValue.getPrimeNumber(numberOfRounds);
// // Assigns n value
// int n = p * q;
// // Call method to find phi(n)
// int phiOfN = findPhi(p,q);
// // Call method to find co-primes and save them all into a dynamic-sized list called myListOfE
// findCoPrimes(phiOfN);
// // Randomly pick one of the co-primes from the list myListOfE as e
// int e = myListOfE.get(primeValue.findRandomInList(myListOfE.size()));
// // Now we need to find d by finding the modular inverse of e with respect to phi(n)
// int d = findD(phiOfN,e);
// // Call method to print out current information
// printFindings(p,q,n,phiOfN,e, d);
// // Print public and private keys
// printKeys(n, e, d);
// EncryptDecrypt newMessage = new EncryptDecrypt();
// newMessage.encryptMessage(e,n);
// } // END OF MAIN METHOD
// // Find the decrypting exponent
// public static int findD(int phiOfN, int randomE) {
// // d = ((phi(n) * i) + 1) / e
// int d = 0;
// for (int i = 1; i <= phiOfN; i++) {
// int possibleD = ((phiOfN * i) + 1) % randomE;
// if (possibleD == 0) {
// d = ((phiOfN * i) + 1) / randomE;
// break;
// }
// }
// printDividingLine();
// return d;
// }
// public static void printFindings(int p, int q, int n, int phiOfN, int randomE, int d) {
// System.out.println(
// "p is " + p + "\t\t q is " + q + "\t\tn is " + n + "\t\t phi(n) is " + phiOfN + "\n"
// + "e is " + randomE + "\t\td is " + d
// );
// }
// public static void printKeys(int n, int e, int d) {
// printDividingLine();
// System.out.println("The pair of numbers (" + n + ", " + e + ") make up the public key." );
// System.out.println("The pair of numbers (" + n + ", " + d + ") make up the private key." );
// printDividingLine();
// }
// public static void findCoPrimes(int phiOfN) {
// // find e that is relatively prime to (p-1) * (q-1) and is >= 1 and <= phiOfN
// // A number is relatively prime to another if those two numbers
// // don't share any other factors except 1
// // e is the encryption exponent. In other words:
// // Select an integer e, such that e is co-prime to ϕ(n) and 1 < e < <ϕ(n).
// // The pair of numbers (n,e)(n,e) makes up the public key.
// // This loop is used to find a list of co-primes to phi(n)
// // I used a list since I couldn't use an array. Arrays are static size in Java and I needed a dynamic size.
// for (int k = 1; k <= phiOfN; k++) {
// if (gcd(phiOfN, k) == 1) {
// // This print lists all co-primes from 1 to phiOfN
// // System.out.println("e could be " + k);
// myListOfE.add(k);
// }
// }
// }
// public static int findPhi(int p, int q) {
// // This totient function works when the numbers are prime
// return (p-1) * (q-1);
// }
// public static int gcd(int a, int b) {
// /* Basic Euclidean Algorithm for GCD
// If we subtract a smaller number from a larger (we reduce a larger number), GCD doesn't change.
// So if we keep subtracting repeatedly the larger of two, we end up with GCD.
// Now instead of subtraction, if we divide the smaller number, the algorithm stops when we find remainder 0. */
// if (a == 0) {
// return b;
// }
// return gcd(b%a, a);
// }
// public static void printFirstInformation() {
// //Self explanatory--prints the initial information
// printDividingLine();
// System.out.println("""
// This program will find the values needed for a basic RSA algorithm\s
// calculation. The encryption exponent e will be found by randomly picking\s
// a number that is a relative prime to phi(n) and is 1 < e < phi(n).\s
// We are finding p and q by generating a list of primes and then choosing \s
// a random position from that list."""
// );
// printDividingLine();
// }
// public static void printDividingLine() {
// System.out.println("===========================================================================");
// }
// }
// RAW Paste Data
// package com.nocial;
// import java.util.ArrayList;
// import java.util.List;
// public class EncryptionTest {
// // Declare static list
// private static List<Integer> myListOfE = new ArrayList<>();
// public static void main(String[] args) {
// // Call method to print information
// printFirstInformation();
// // Create an object for my class FindPrimes
// FindPrimes primeValue = new FindPrimes();
// // Calls public method to get user input for number of rounds
// int numberOfRounds = primeValue.getRounds();
// // Assigns prime numbers to p and q from the return of public method
// int p = primeValue.getPrimeNumber(numberOfRounds);
// int q = primeValue.getPrimeNumber(numberOfRounds);
// // Assigns n value
// int n = p * q;
// // Call method to find phi(n)
// int phiOfN = findPhi(p,q);
// // Call method to find co-primes and save them all into a dynamic-sized list called myListOfE
// findCoPrimes(phiOfN);
// // Randomly pick one of the co-primes from the list myListOfE as e
// int e = myListOfE.get(primeValue.findRandomInList(myListOfE.size()));
// // Now we need to find d by finding the modular inverse of e with respect to phi(n)
// int d = findD(phiOfN,e);
// // Call method to print out current information
// printFindings(p,q,n,phiOfN,e, d);
// // Print public and private keys
// printKeys(n, e, d);
// EncryptDecrypt newMessage = new EncryptDecrypt();
// newMessage.encryptMessage(e,n);
// } // END OF MAIN METHOD
// // Find the decrypting exponent
// public static int findD(int phiOfN, int randomE) {
// // d = ((phi(n) * i) + 1) / e
// int d = 0;
// for (int i = 1; i <= phiOfN; i++) {
// int possibleD = ((phiOfN * i) + 1) % randomE;
// if (possibleD == 0) {
// d = ((phiOfN * i) + 1) / randomE;
// break;
// }
// }
// printDividingLine();
// return d;
// }
// public static void printFindings(int p, int q, int n, int phiOfN, int randomE, int d) {
// System.out.println(
// "p is " + p + "\t\t q is " + q + "\t\tn is " + n + "\t\t phi(n) is " + phiOfN + "\n"
// + "e is " + randomE + "\t\td is " + d
// );
// }
// public static void printKeys(int n, int e, int d) {
// printDividingLine();
// System.out.println("The pair of numbers (" + n + ", " + e + ") make up the public key." );
// System.out.println("The pair of numbers (" + n + ", " + d + ") make up the private key." );
// printDividingLine();
// }
// public static void findCoPrimes(int phiOfN) {
// // find e that is relatively prime to (p-1) * (q-1) and is >= 1 and <= phiOfN
// // A number is relatively prime to another if those two numbers
// // don't share any other factors except 1
// // e is the encryption exponent. In other words:
// // Select an integer e, such that e is co-prime to ϕ(n) and 1 < e < <ϕ(n).
// // The pair of numbers (n,e)(n,e) makes up the public key.
// // This loop is used to find a list of co-primes to phi(n)
// // I used a list since I couldn't use an array. Arrays are static size in Java and I needed a dynamic size.
// for (int k = 1; k <= phiOfN; k++) {
// if (gcd(phiOfN, k) == 1) {
// // This print lists all co-primes from 1 to phiOfN
// // System.out.println("e could be " + k);
// myListOfE.add(k);
// }
// }
// }
// public static int findPhi(int p, int q) {
// // This totient function works when the numbers are prime
// return (p-1) * (q-1);
// }
// public static int gcd(int a, int b) {
// /* Basic Euclidean Algorithm for GCD
// If we subtract a smaller number from a larger (we reduce a larger number), GCD doesn't change.
// So if we keep subtracting repeatedly the larger of two, we end up with GCD.
// Now instead of subtraction, if we divide the smaller number, the algorithm stops when we find remainder 0. */
// if (a == 0) {
// return b;
// }
// return gcd(b%a, a);
// }
// public static void printFirstInformation() {
// //Self explanatory--prints the initial information
// printDividingLine();
// System.out.println("""
// This program will find the values needed for a basic RSA algorithm\s
// calculation. The encryption exponent e will be found by randomly picking\s
// a number that is a relative prime to phi(n) and is 1 < e < phi(n).\s
// We are finding p and q by generating a list of primes and then choosing \s
// a random position from that list."""
// );
// printDividingLine();
// }
// public static void printDividingLine() {
// System.out.println("===========================================================================");
// }
// }