-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.java
More file actions
146 lines (120 loc) · 2.98 KB
/
Cipher.java
File metadata and controls
146 lines (120 loc) · 2.98 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
/**
* @author roberson
*
*/
import java.math.BigInteger;
public class Cipher {
private String cipheredWord = "";
private String word;
private Integer publicKey;
private Integer privateKey;
private static String dic(String a) {
String b = "";
for (int i = 0; i < a.length(); i++) {
char c = a.charAt(i);
b = b + (int) c + " ";
}
//System.out.println(b);
return b;
}
/**************************
* Retornar o número N O número N é o tamanho do conjunto
* */
private static Integer numeroN(Integer primo1, Integer primo2) {
Integer conjunto;
conjunto = primo1 * primo2;
return conjunto;
}
/*************************
* Função Totiene de Euler Calcular o Coprimo anterior esta função diz a
* quantidade de co-primos de um numero que são menores que ele mesmo.
* */
private static Integer totieneDeEuler(Integer primo1, Integer primo2) {
// fi(x) = (p-1) * (q-1)
Integer fi;
fi = (primo1 - 1) * (primo2 - 1);
return fi;
}
/***********************************
* Calcular a chave pública Devemos escolher um número e em que 1 < e <
* φ(n), de forma que e seja co-primo de φ(n). Em outras palavras, queremos
* um e onde o MDC(φ(n), e) = 1, sendo e > 1.
*
* @param e
* recebe valor maior que 1
* */
private static Integer calcularPK(Integer num) {
int count = 0;
for (int a = 1; a < num; a++) { // definition of totient: the amount of
// numbers less than num coprime to it
if (GDC(num, a) == 1) { // coprime
count++;
}
}
//return 13;
return count;
}
/*******************
* Calcular o GDC
* */
private static Integer GDC(int a, int b) {
int temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
if (a % b == 0)
return b;
return GDC(a % b, b);
}
@SuppressWarnings("unused")
private static Integer MDC(int a, int b) {
int resto;
while (b != 0) {
resto = a % b;
a = b;
b = resto;
}
return a;
}
public void DoCipher() {
String msg = dic(word);
String arr[] = msg.split(" ");
Integer fi = totieneDeEuler(publicKey, privateKey);
Long tamConjunto = Long.valueOf(numeroN(publicKey, privateKey));
for (String a : arr) {
Integer chavePublica = calcularPK(fi);
BigInteger exp = null;
BigInteger m = new BigInteger(a);
exp = m.pow(chavePublica);
BigInteger resultado = exp.mod(BigInteger.valueOf(tamConjunto));
cipheredWord = cipheredWord + resultado + " ";
}
//System.out.println("\n" + cipheredWord);
}
public String getCipheredWord() {
return cipheredWord;
}
public void setCipheredWord(String cipheredWord) {
this.cipheredWord = cipheredWord;
}
public Integer getPublicKey() {
return publicKey;
}
public void setPublicKey(Integer publicKey) {
this.publicKey = publicKey;
}
public Integer getPrivateKey() {
return privateKey;
}
public void setPrivateKey(Integer privateKey) {
this.privateKey = privateKey;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}