-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
229 lines (185 loc) · 5.91 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
package RSA;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.zip.InflaterInputStream;
public class RSA {
static ArrayList<String[]> inputNumbers = new ArrayList<>();
static ArrayList<Long> encryptetet = new ArrayList<>();
public static void main(String[] args) {
// verschlüsseln();
// Auskommentierter Teil beinhaltetet code zum eingeben, von Chiffrat als Zahlen in die Liste
// "Unkown Chiffrat". Benötigten key auskommentieren und mit passenden Werten füllen.
//
// ArrayList<Long> unknownChiffrat = new ArrayList<Long>();
// ArrayList<Long> message = new ArrayList<>();
// unknownChiffrat.add((long) 31);
// unknownChiffrat.add((long) 33);
// unknownChiffrat.add((long) 01);
// unknownChiffrat.add((long) 72);
// unknownChiffrat.add((long) 76);
// unknownChiffrat.add((long) 1737);
// unknownChiffrat.add((long) 2158);
// Beide Methoden brauchen unkownChiffrat
// key priavateKey = new key(10,3);
// key publicKey = new key(10,3);
//
// message=decrypteWithPublicKey(publicKey, unknownChiffrat);
//
//
// message=decrypteWithPrivateKey(priavateKey, unknownChiffrat);
//
// System.out.println("massage decrypteet:" + message);
}
private static ArrayList<Long> decrypteWithPublicKey(key publicKey, ArrayList<Long> unknownChiffrat) {
// TODO Auto-generated method stub
return decrypte(getPrivateKey(publicKey), unknownChiffrat);
}
private static ArrayList<Long> decrypteWithPrivateKey(key priavateKey, ArrayList<Long> unknownChiffrat) {
// TODO Auto-generated method stub
return decrypte(priavateKey, unknownChiffrat);
}
private static void verschlüsseln() {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Blockgröße der Buchstaben angeben");
int blockGroese= scanner.nextInt();
String abbruch="";
do {
inputNumbers.add(new String [blockGroese]);
for (int i = 0; i < blockGroese; i++) {
System.out.println("Bitte Buchstabe als Nummer eingeben");
inputNumbers.get(inputNumbers.size()-1)[i]= scanner.next();
}
System.out.println("weitere Eingabe? wenn nein 'ende' eingeben");
abbruch=scanner.next();
} while(!abbruch.contains("ende"));
key publicKey;
System.out.println("Parameter für den Public key eingeben:");
System.out.println("N und e gegeben ? ja eingeben");
if(scanner.next().contains("ja") ) {
System.out.println("e eingeben");
int e = scanner.nextInt();
System.out.println("N eingeben");
int N = scanner.nextInt();
publicKey = new key(e,N);
encryptetet = encrypte(publicKey);
} else {
System.out.println("q: ");
int q = scanner.nextInt();
System.out.println("p: ");
int p = scanner.nextInt();
System.out.println("e: ");
int e = scanner.nextInt();
publicKey = new key(e,(p*q));
encryptetet = encrypte(publicKey);
}
System.out.println("encryptet:"+encryptetet);
System.out.println(getPrivateKey(publicKey));
System.out.println("decryptet:"+decrypte(getPrivateKey(publicKey), encryptetet));
}
private static key getPrivateKey(key publicKey) {
// System.out.printf("\nPublic Key = (%d, %d)\n", e, rsaModul);
int q = 0;
int p = 0;
int d = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if(i*j == publicKey.N && i!=1 && j!=0){
q = i;
p = j;
break;
}
}
}
//int phi = (q - 1) * (p - 1);
for (int i = 0; i < 100000; i++) {
if(((i*publicKey.e)%phi(publicKey.N)) == 1){
d = i;
break;
}
}
System.out.println(d);
return new key(d, publicKey.N);
}
private static int phi(int n)
{
int result = n; // Initialize result as n
// Consider all prime factors of n and subtract their
// multiples from result
for (int p = 2; p * p <= n; ++p) {
// Check if p is a prime factor.
if (n % p == 0) {
// If yes, then update n and result
while (n % p == 0)
n /= p;
result -= result / p;
}
}
// If n has a prime factor greater than sqrt(n)
// (There can be at-most one such prime factor)
if (n > 1)
result -= result / n;
return result;
}
private static ArrayList<Long> encrypte(key encrypteKey) {
ArrayList<Long> value = new ArrayList<>();
ArrayList<Long> chiffrat = new ArrayList<>();
for(String [] x : inputNumbers) {
String temp="";
for (int i = 0; i < x.length; i++) {
temp+= x[i];
}
value.add(Long.valueOf(temp));
}
for(Long x: value) {
chiffrat.add(powMod(x, encrypteKey.e,(long) encrypteKey.N));
}
return chiffrat;
}
private static ArrayList<Long> decrypte(key decrypteKey, ArrayList<Long> chiffrat) {
ArrayList<Long> klarText = new ArrayList<>();
for(Long x : chiffrat) {
klarText.add(powMod(x, decrypteKey.e, decrypteKey.N));
}
return klarText;
}
public static long powMod(long base, int exp , long modulus) {
base %= modulus;
long result = 1;
while (exp > 0) {
if ((exp & 1) == 1 ) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exp /= 2;
}
return result;
}
}
class key {
int e;
int N;
public key(int e, int N) {
// TODO Auto-generated constructor stub
this.e=e;
this.N=N;
}
public int getE() {
return e;
}
public int getN() {
return N;
}
public void setE(int e) {
this.e = e;
}
public void setN(int n) {
N = n;
}
@Override
public String toString() {
return "(" +String.valueOf(this.e) + "," +String.valueOf(this.N)+")";
}
}