4
4
import java .security .KeyPair ;
5
5
import java .security .KeyPairGenerator ;
6
6
import java .security .NoSuchAlgorithmException ;
7
+ import java .util .Base64 ;
7
8
8
9
import javax .crypto .Cipher ;
10
+ import javax .crypto .SecretKey ;
11
+ import javax .crypto .spec .SecretKeySpec ;
12
+
13
+
9
14
10
15
public class CryptoRSA {
11
16
12
17
private static Cipher rsa ;
18
+ private static KeyPair keyPair ;
13
19
14
20
/**
15
21
* Dado un voto, generamos su clave pública y privada
@@ -19,13 +25,14 @@ public class CryptoRSA {
19
25
* @return Par de claves pública y privada
20
26
* @throws NoSuchAlgorithmException
21
27
*/
22
- public static KeyPair generateKeyPair () throws NoSuchAlgorithmException {
28
+ public static KeyPair generateKeyPair () throws NoSuchAlgorithmException {
23
29
KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance ("RSA" );
24
30
KeyPair keyPair = keyPairGenerator .generateKeyPair ();
25
31
26
32
return keyPair ;
27
33
}
28
34
35
+
29
36
30
37
/**
31
38
* Método que dado un voto y una clave pública, encripta usando RSA
@@ -36,9 +43,11 @@ public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
36
43
* : Clave publica
37
44
* @return Voto encriptado
38
45
*/
39
- public static byte [] encrypt (String votationId ,byte [] textToEncypt , Key publicKey , Integer token ) {
46
+
47
+
48
+ public static byte [] encrypt (String votationId ,byte [] textToEncypt , Key publicKey , Integer token ) {
40
49
byte [] cipherText = new byte [0 ];
41
-
50
+
42
51
try {
43
52
if (Token .checkTokenDb (new Integer (votationId ), token ))
44
53
// Obtener el tipo de encriptacion que vamos a realizar
@@ -64,13 +73,14 @@ public static byte[] encrypt(String votationId,byte[] textToEncypt, Key publicKe
64
73
* : Clave privada
65
74
* @return Voto desencriptado
66
75
*/
67
- public static byte [] decrypt (String votationId ,byte [] cipherText , Key privateKey ,Integer token ) {
76
+ public static byte [] decrypt (String votationId ,byte [] cipherText ,Key privateKey ,Integer token ) {
68
77
byte [] text = new byte [0 ];
69
-
78
+
70
79
try {
71
80
if (Token .checkTokenDb (new Integer (votationId ), token ))
72
81
// Obtener el tipo de encriptacion que vamos a realizar
73
82
rsa = Cipher .getInstance ("RSA" );
83
+
74
84
75
85
// Desencripta
76
86
rsa .init (Cipher .DECRYPT_MODE , privateKey );
@@ -83,6 +93,9 @@ public static byte[] decrypt (String votationId,byte[] cipherText, Key privateKe
83
93
return text ;
84
94
}
85
95
96
+ // --------------------------------------------------------------------------------------------
97
+
98
+
86
99
87
100
/**
88
101
* Método que dado un voto y una clave pública, encripta usando RSA
@@ -91,18 +104,31 @@ public static byte[] decrypt (String votationId,byte[] cipherText, Key privateKe
91
104
* : Voto sin encriptar
92
105
* @param publicKey
93
106
* : Clave publica
107
+ *
94
108
* @return Voto encriptado
109
+ *
110
+ * @throws NoSuchAlgorithmException
95
111
*/
96
- public static byte [] encryptLocal (String votationId ,byte [] textToEncypt , Key publicKey , Integer token ) {
112
+ public static byte [] encryptLocal (String votationId ,byte [] textToEncypt ,Key publicKey , Integer token ) throws NoSuchAlgorithmException {
97
113
byte [] cipherText = new byte [0 ];
98
114
115
+ LocalDataBaseManager dbLocal = new LocalDataBaseManager ();
116
+
117
+ keyPair = generateKeyPair ();
118
+
119
+ String publicKey2 = Base64 .getEncoder ().encodeToString (keyPair .getPublic ().getEncoded ());
120
+ String privateKey = Base64 .getEncoder ().encodeToString (keyPair .getPrivate ().getEncoded ());
121
+
122
+ dbLocal .postKeys (votationId , publicKey2 , privateKey );
123
+ LocalDataBaseManager .sendGeneratedToken (new Integer (votationId ), token );
124
+
99
125
try {
100
126
if (TokenLocal .checkTokenDb (new Integer (votationId ), token ))
101
127
// Obtener el tipo de encriptacion que vamos a realizar
102
128
rsa = Cipher .getInstance ("RSA" );
103
129
104
130
// Encripta
105
- rsa .init (Cipher .ENCRYPT_MODE , publicKey );
131
+ rsa .init (Cipher .ENCRYPT_MODE , keyPair . getPublic () );
106
132
cipherText = rsa .doFinal (textToEncypt );
107
133
108
134
} catch (Exception e ) {
@@ -121,20 +147,26 @@ public static byte[] encryptLocal (String votationId,byte[] textToEncypt, Key pu
121
147
* : Clave privada
122
148
* @return Voto desencriptado
123
149
*/
124
- public static byte [] decryptLocal (String votationId ,byte [] cipherText , Key privateKey ,Integer token ) {
150
+ public static byte [] decryptLocal (String votationId ,byte [] cipherText ,Key privateKey ,Integer token ) {
125
151
byte [] text = new byte [0 ];
126
152
153
+ LocalDataBaseManager dbLocal = new LocalDataBaseManager ();
154
+
127
155
try {
128
156
if (TokenLocal .checkTokenDb (new Integer (votationId ), token ))
129
157
// Obtener el tipo de encriptacion que vamos a realizar
130
158
rsa = Cipher .getInstance ("RSA" );
131
159
160
+ byte [] encodedKey = Base64 .getDecoder ().decode (dbLocal .getSecretKey (votationId ));
161
+ SecretKey privateKey2 = new SecretKeySpec (encodedKey , 0 , encodedKey .length , "RSA" );
162
+
163
+
132
164
// Desencripta
133
165
rsa .init (Cipher .DECRYPT_MODE , privateKey );
134
166
text = rsa .doFinal (cipherText );
135
167
136
168
} catch (Exception e ) {
137
- System .out .println ("decrypt exception : " + e .getMessage ());
169
+ System .out .println ("decrypt done : " + e .getMessage ());
138
170
}
139
171
140
172
return text ;
0 commit comments