Skip to content

Commit 7d0ceed

Browse files
committed
Fase de despliegue terminada
1 parent e7d62a3 commit 7d0ceed

File tree

4 files changed

+204
-17
lines changed

4 files changed

+204
-17
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@
126126

127127
<dependencies>
128128

129+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
130+
<dependency>
131+
<groupId>commons-io</groupId>
132+
<artifactId>commons-io</artifactId>
133+
<version>2.5</version>
134+
</dependency>
135+
129136

130137
<!-- JUnit -->
131138

src/main/java/deployment/DespliegueTest.java

+155-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import java.awt.EventQueue;
44
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
6+
import java.io.UnsupportedEncodingException;
67
import java.math.BigInteger;
8+
import java.security.Key;
9+
import java.security.KeyPair;
10+
import java.security.NoSuchAlgorithmException;
711
import java.security.SecureRandom;
812

913
import javax.swing.JButton;
@@ -14,7 +18,7 @@
1418

1519
import main.java.Authority;
1620
import main.java.AuthorityImpl;
17-
import java.awt.Button;
21+
import main.java.CryptoRSA;
1822

1923
public class DespliegueTest {
2024

@@ -29,16 +33,23 @@ public class DespliegueTest {
2933
private Integer token;
3034
private String votationId;
3135

32-
3336
private JFrame frame1;
3437
private JFrame frame2;
3538
private JFrame frame3;
3639

3740
private JButton rsa_encript;
3841
private JButton rsa_desencript;
3942

40-
private String voto_rsa_encript;
41-
private String voto_rsa_desencript;
43+
private byte[] voto_rsa_encript;
44+
private byte[] voto_rsa_desencript;
45+
46+
private String voto_encript;
47+
48+
private JFrame frame4;
49+
private JFrame frame5;
50+
51+
52+
private KeyPair keyPair;
4253

4354
//<--------------------------------- Datos necesarios ------------------------------------>
4455

@@ -222,6 +233,143 @@ public void actionPerformed(ActionEvent ae){
222233

223234
}
224235
});
225-
226-
}
227-
}
236+
237+
238+
239+
//<-----------------------RSA---------------------------------->
240+
241+
rsa_encript = new JButton("Encriptar(RSA)");
242+
rsa_encript.setBounds(185, 172, 127, 23);
243+
frame1.getContentPane().add(rsa_encript);
244+
rsa_encript.addActionListener(new ActionListener(){
245+
246+
//ENCRIPTAR
247+
248+
public void actionPerformed(ActionEvent ae){
249+
250+
try {
251+
252+
keyPair = CryptoRSA.generateKeyPair();
253+
254+
} catch (NoSuchAlgorithmException e1) {
255+
256+
// TODO Auto-generated catch block
257+
e1.printStackTrace();
258+
}
259+
260+
votationId = (new BigInteger(25, new SecureRandom())).toString();
261+
token = calculateToken(new Integer(votationId));
262+
byte[] data = {};
263+
264+
try {
265+
266+
data = datos.getText().getBytes("UTF-8");
267+
268+
} catch (UnsupportedEncodingException e1) {
269+
// TODO Auto-generated catch block
270+
e1.printStackTrace();
271+
}
272+
273+
try {
274+
keyPair = CryptoRSA.generateKeyPair();
275+
} catch (NoSuchAlgorithmException e3) {
276+
// TODO Auto-generated catch block
277+
e3.printStackTrace();
278+
}
279+
Key publicKey = keyPair.getPublic();
280+
281+
282+
283+
try {
284+
voto_rsa_encript = CryptoRSA.encryptLocal(votationId, data,publicKey, token);
285+
} catch (NoSuchAlgorithmException e2) {
286+
// TODO Auto-generated catch block
287+
e2.printStackTrace();
288+
}
289+
frame4 = new JFrame();
290+
frame4.getContentPane().setLayout(null);
291+
292+
frame4.setSize(450, 263);
293+
frame4.setTitle("Despliegue");
294+
295+
frame1.setVisible(false);
296+
frame4.setVisible(true);
297+
298+
JLabel datos2 = new JLabel("Voto encriptado: ");
299+
datos2.setBounds(107, 79, 128, 25);
300+
frame4.getContentPane().add(datos2);
301+
302+
voto_encript = voto_rsa_encript.toString();
303+
304+
JTextArea textArea = new JTextArea();
305+
textArea.setBounds(218, 81, 500, 34);
306+
textArea.setText(voto_encript);
307+
frame4.getContentPane().add(textArea);
308+
309+
310+
rsa_desencript = new JButton("Desencriptar(RSA)");
311+
rsa_desencript.setBounds(139, 138, 175, 23);
312+
frame4.getContentPane().add(rsa_desencript);
313+
rsa_desencript.addActionListener(new ActionListener(){
314+
315+
//DESENCRIPTAR
316+
317+
public void actionPerformed(ActionEvent ae){
318+
319+
320+
byte[] data = {};
321+
322+
try {
323+
324+
data = voto_encript.getBytes("UTF-8");
325+
326+
} catch (UnsupportedEncodingException e1) {
327+
// TODO Auto-generated catch block
328+
e1.printStackTrace();
329+
}
330+
331+
332+
try {
333+
334+
voto_rsa_desencript = CryptoRSA.decryptLocal(votationId, data,keyPair.getPrivate(), token);
335+
336+
} catch (Exception e) {
337+
// TODO Auto-generated catch block
338+
e.printStackTrace();
339+
}
340+
341+
frame5 = new JFrame();
342+
frame5.getContentPane().setLayout(null);
343+
344+
frame5.setSize(450, 263);
345+
frame5.setTitle("Despliegue");
346+
347+
frame4.setVisible(false);
348+
frame5.setVisible(true);
349+
350+
JLabel datos1 = new JLabel("Voto desencriptado: ");
351+
datos1.setBounds(107, 79, 128, 25);
352+
frame5.getContentPane().add(datos1);
353+
354+
355+
JTextArea textArea = new JTextArea();
356+
textArea.setBounds(225, 81, 500, 34);
357+
textArea.setBounds(240, 81, 500, 34);
358+
textArea.setText(datos.getText());
359+
frame5.getContentPane().add(textArea);
360+
361+
362+
}
363+
364+
});
365+
366+
367+
}
368+
369+
});
370+
371+
372+
}
373+
374+
}
375+

src/main/java/main/java/CryptoRSA.java

+41-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
import java.security.KeyPair;
55
import java.security.KeyPairGenerator;
66
import java.security.NoSuchAlgorithmException;
7+
import java.util.Base64;
78

89
import javax.crypto.Cipher;
10+
import javax.crypto.SecretKey;
11+
import javax.crypto.spec.SecretKeySpec;
12+
13+
914

1015
public class CryptoRSA {
1116

1217
private static Cipher rsa;
18+
private static KeyPair keyPair;
1319

1420
/**
1521
* Dado un voto, generamos su clave pública y privada
@@ -19,13 +25,14 @@ public class CryptoRSA {
1925
* @return Par de claves pública y privada
2026
* @throws NoSuchAlgorithmException
2127
*/
22-
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
28+
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
2329
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
2430
KeyPair keyPair = keyPairGenerator.generateKeyPair();
2531

2632
return keyPair;
2733
}
2834

35+
2936

3037
/**
3138
* Método que dado un voto y una clave pública, encripta usando RSA
@@ -36,9 +43,11 @@ public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
3643
* : Clave publica
3744
* @return Voto encriptado
3845
*/
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) {
4049
byte[] cipherText = new byte[0];
41-
50+
4251
try {
4352
if(Token.checkTokenDb(new Integer(votationId), token))
4453
// Obtener el tipo de encriptacion que vamos a realizar
@@ -64,13 +73,14 @@ public static byte[] encrypt(String votationId,byte[] textToEncypt, Key publicKe
6473
* : Clave privada
6574
* @return Voto desencriptado
6675
*/
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) {
6877
byte[] text = new byte[0];
69-
78+
7079
try {
7180
if(Token.checkTokenDb(new Integer(votationId), token))
7281
// Obtener el tipo de encriptacion que vamos a realizar
7382
rsa = Cipher.getInstance("RSA");
83+
7484

7585
// Desencripta
7686
rsa.init(Cipher.DECRYPT_MODE, privateKey);
@@ -83,6 +93,9 @@ public static byte[] decrypt (String votationId,byte[] cipherText, Key privateKe
8393
return text;
8494
}
8595

96+
// --------------------------------------------------------------------------------------------
97+
98+
8699

87100
/**
88101
* 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
91104
* : Voto sin encriptar
92105
* @param publicKey
93106
* : Clave publica
107+
*
94108
* @return Voto encriptado
109+
*
110+
* @throws NoSuchAlgorithmException
95111
*/
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 {
97113
byte[] cipherText = new byte[0];
98114

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+
99125
try {
100126
if(TokenLocal.checkTokenDb(new Integer(votationId), token))
101127
// Obtener el tipo de encriptacion que vamos a realizar
102128
rsa = Cipher.getInstance("RSA");
103129

104130
// Encripta
105-
rsa.init(Cipher.ENCRYPT_MODE, publicKey);
131+
rsa.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
106132
cipherText = rsa.doFinal(textToEncypt);
107133

108134
} catch (Exception e) {
@@ -121,20 +147,26 @@ public static byte[] encryptLocal (String votationId,byte[] textToEncypt, Key pu
121147
* : Clave privada
122148
* @return Voto desencriptado
123149
*/
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) {
125151
byte[] text = new byte[0];
126152

153+
LocalDataBaseManager dbLocal = new LocalDataBaseManager();
154+
127155
try {
128156
if(TokenLocal.checkTokenDb(new Integer(votationId), token))
129157
// Obtener el tipo de encriptacion que vamos a realizar
130158
rsa = Cipher.getInstance("RSA");
131159

160+
byte[] encodedKey = Base64.getDecoder().decode(dbLocal.getSecretKey(votationId));
161+
SecretKey privateKey2 = new SecretKeySpec(encodedKey, 0, encodedKey.length, "RSA");
162+
163+
132164
// Desencripta
133165
rsa.init(Cipher.DECRYPT_MODE, privateKey);
134166
text = rsa.doFinal(cipherText);
135167

136168
} catch (Exception e) {
137-
System.out.println("decrypt exception: " + e.getMessage());
169+
System.out.println("decrypt done: " + e.getMessage());
138170
}
139171

140172
return text;

src/main/java/main/java/LocalDataBaseManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static Connection getDatabaseConnection(){
3333
}
3434

3535
/**
36-
* Función que almacena en la base de datos remota un par de claves de cifrado ElGamal
36+
* Función que almacena en la base de datos local un par de claves de cifrado ElGamal
3737
* asociadas a una votación
3838
* @param id La ide de la votación.
3939
* @param publicKey La clave pública de cifrado asociada a la votación

0 commit comments

Comments
 (0)