-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
68 lines (45 loc) · 1.81 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
public class RSA {
public static void main(String[] args){
KulcsPar jSzereplo = new KulcsPar();
String tisztaSzoveg = "Hello,Vilag!";
byte[] buffer = tisztaSzoveg.getBytes();
java.math.BigInteger[] titkos = new java.math.BigInteger[buffer.length];
for (int i = 0; i < titkos.length; ++i){
titkos[i] = new java.math.BigInteger(new byte[]{buffer[i]});
titkos[i] = titkos[i].modPow(jSzereplo.e, jSzereplo.m);
}
for (int i = 0; i < titkos.length; ++i){
titkos[i] = titkos[i].modPow(jSzereplo.d, jSzereplo.m);
buffer[i] = titkos[i].byteValue();
}
System.out.println(new String(buffer));
}
}
class KulcsPar {
java.math.BigInteger d, e, m;
public KulcsPar(){
int meretBitekben = 700 * (int) (java.lang.Math.log ((double) 10)/ java.lang.Math.log((double)2));
System.out.println("Méret bitekben: ");
System.out.println(meretBitekben);
java.math.BigInteger p = new java.math.BigInteger(meretBitekben, 100, new java.util.Random());
System.out.println("p");
System.out.println(p);
java.math.BigInteger q = new java.math.BigInteger(meretBitekben, 100, new java.util.Random());
System.out.println("q");
System.out.println(q);
m = p.multiply(q);
System.out.println("m");
System.out.println(m);
java.math.BigInteger z = p.subtract(java.math.BigInteger.ONE).multiply(q.subtract(java.math.BigInteger.ONE));
System.out.println("z");
System.out.println(z);
do{
do{
d = new java.math.BigInteger(meretBitekben, new java.util.Random());
}while(d.equals(java.math.BigInteger.ONE));
}while(!z.gcd(d).equals(java.math.BigInteger.ONE));
e = d.modInverse(z);
System.out.println("e");
System.out.println(e);
}
}