-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.java
More file actions
60 lines (48 loc) · 1.45 KB
/
Copy pathRSA.java
File metadata and controls
60 lines (48 loc) · 1.45 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
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class RSA {
private BigInteger n;
private BigInteger e;
private BigInteger d;
public RSA(int tamanho) {
gerarChaves(tamanho);
}
public RSA(BigInteger n, BigInteger e, BigInteger d) {
this.n = n;
this.e = e;
this.d = d;
}
private void gerarChaves(int tamanho) {
BigInteger p = Comum.gerarPrimo(tamanho);
BigInteger q = Comum.gerarPrimo(tamanho);
n = p.multiply(q);
BigInteger fi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = Comum.gerarPrimo(tamanho);
List<BigInteger> euclidiano = Comum.euclid(e, fi);
if(euclidiano.get(0).compareTo(BigInteger.ONE) != 0) {
e = Comum.gerarPrimo(tamanho);
euclidiano = Comum.euclid(e, fi);
}
d = euclidiano.get(1).mod(fi);
}
public String toString() {
return "Public keys:\n"+
"\tn: " + n + "\n"+
"\te: " + e + "\n"+
"Private keys:\n"+
"\tn: " + n + "\n"+
"\td: " + d + "\n";
}
public List<BigInteger> encripta(List<BigInteger> blocos){
List<BigInteger> encriptado = new ArrayList<BigInteger>();
for(BigInteger bloco : blocos) encriptado.add(Comum.expoModular(bloco, e, n));
return encriptado;
}
public List<BigInteger> decripta(List<BigInteger> blocos){
List<BigInteger> decriptado = new ArrayList<BigInteger>();
for(BigInteger bloco : blocos) decriptado.add(Comum.expoModular(bloco, d, n));
return decriptado;
}
}
//String.format("%064X", d)