Skip to content

Commit

Permalink
Fase de despliegue terminada
Browse files Browse the repository at this point in the history
  • Loading branch information
josruinav committed Feb 2, 2017
1 parent e7d62a3 commit 7d0ceed
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 17 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@

<dependencies>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>


<!-- JUnit -->

Expand Down
162 changes: 155 additions & 7 deletions src/main/java/deployment/DespliegueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.swing.JButton;
Expand All @@ -14,7 +18,7 @@

import main.java.Authority;
import main.java.AuthorityImpl;
import java.awt.Button;
import main.java.CryptoRSA;

public class DespliegueTest {

Expand All @@ -29,16 +33,23 @@ public class DespliegueTest {
private Integer token;
private String votationId;


private JFrame frame1;
private JFrame frame2;
private JFrame frame3;

private JButton rsa_encript;
private JButton rsa_desencript;

private String voto_rsa_encript;
private String voto_rsa_desencript;
private byte[] voto_rsa_encript;
private byte[] voto_rsa_desencript;

private String voto_encript;

private JFrame frame4;
private JFrame frame5;


private KeyPair keyPair;

//<--------------------------------- Datos necesarios ------------------------------------>

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

}
});

}
}



//<-----------------------RSA---------------------------------->

rsa_encript = new JButton("Encriptar(RSA)");
rsa_encript.setBounds(185, 172, 127, 23);
frame1.getContentPane().add(rsa_encript);
rsa_encript.addActionListener(new ActionListener(){

//ENCRIPTAR

public void actionPerformed(ActionEvent ae){

try {

keyPair = CryptoRSA.generateKeyPair();

} catch (NoSuchAlgorithmException e1) {

// TODO Auto-generated catch block
e1.printStackTrace();
}

votationId = (new BigInteger(25, new SecureRandom())).toString();
token = calculateToken(new Integer(votationId));
byte[] data = {};

try {

data = datos.getText().getBytes("UTF-8");

} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

try {
keyPair = CryptoRSA.generateKeyPair();
} catch (NoSuchAlgorithmException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
Key publicKey = keyPair.getPublic();



try {
voto_rsa_encript = CryptoRSA.encryptLocal(votationId, data,publicKey, token);
} catch (NoSuchAlgorithmException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
frame4 = new JFrame();
frame4.getContentPane().setLayout(null);

frame4.setSize(450, 263);
frame4.setTitle("Despliegue");

frame1.setVisible(false);
frame4.setVisible(true);

JLabel datos2 = new JLabel("Voto encriptado: ");
datos2.setBounds(107, 79, 128, 25);
frame4.getContentPane().add(datos2);

voto_encript = voto_rsa_encript.toString();

JTextArea textArea = new JTextArea();
textArea.setBounds(218, 81, 500, 34);
textArea.setText(voto_encript);
frame4.getContentPane().add(textArea);


rsa_desencript = new JButton("Desencriptar(RSA)");
rsa_desencript.setBounds(139, 138, 175, 23);
frame4.getContentPane().add(rsa_desencript);
rsa_desencript.addActionListener(new ActionListener(){

//DESENCRIPTAR

public void actionPerformed(ActionEvent ae){


byte[] data = {};

try {

data = voto_encript.getBytes("UTF-8");

} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


try {

voto_rsa_desencript = CryptoRSA.decryptLocal(votationId, data,keyPair.getPrivate(), token);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

frame5 = new JFrame();
frame5.getContentPane().setLayout(null);

frame5.setSize(450, 263);
frame5.setTitle("Despliegue");

frame4.setVisible(false);
frame5.setVisible(true);

JLabel datos1 = new JLabel("Voto desencriptado: ");
datos1.setBounds(107, 79, 128, 25);
frame5.getContentPane().add(datos1);


JTextArea textArea = new JTextArea();
textArea.setBounds(225, 81, 500, 34);
textArea.setBounds(240, 81, 500, 34);
textArea.setText(datos.getText());
frame5.getContentPane().add(textArea);


}

});


}

});


}

}

50 changes: 41 additions & 9 deletions src/main/java/main/java/CryptoRSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



public class CryptoRSA {

private static Cipher rsa;
private static KeyPair keyPair;

/**
* Dado un voto, generamos su clave pública y privada
Expand All @@ -19,13 +25,14 @@ public class CryptoRSA {
* @return Par de claves pública y privada
* @throws NoSuchAlgorithmException
*/
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();

return keyPair;
}



/**
* Método que dado un voto y una clave pública, encripta usando RSA
Expand All @@ -36,9 +43,11 @@ public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
* : Clave publica
* @return Voto encriptado
*/
public static byte[] encrypt(String votationId,byte[] textToEncypt, Key publicKey, Integer token) {


public static byte[] encrypt(String votationId,byte[] textToEncypt, Key publicKey , Integer token) {
byte[] cipherText = new byte[0];

try {
if(Token.checkTokenDb(new Integer(votationId), token))
// Obtener el tipo de encriptacion que vamos a realizar
Expand All @@ -64,13 +73,14 @@ public static byte[] encrypt(String votationId,byte[] textToEncypt, Key publicKe
* : Clave privada
* @return Voto desencriptado
*/
public static byte[] decrypt (String votationId,byte[] cipherText, Key privateKey,Integer token) {
public static byte[] decrypt (String votationId,byte[] cipherText,Key privateKey,Integer token) {
byte[] text = new byte[0];

try {
if(Token.checkTokenDb(new Integer(votationId), token))
// Obtener el tipo de encriptacion que vamos a realizar
rsa = Cipher.getInstance("RSA");


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

// --------------------------------------------------------------------------------------------



/**
* Método que dado un voto y una clave pública, encripta usando RSA
Expand All @@ -91,18 +104,31 @@ public static byte[] decrypt (String votationId,byte[] cipherText, Key privateKe
* : Voto sin encriptar
* @param publicKey
* : Clave publica
*
* @return Voto encriptado
*
* @throws NoSuchAlgorithmException
*/
public static byte[] encryptLocal (String votationId,byte[] textToEncypt, Key publicKey, Integer token) {
public static byte[] encryptLocal (String votationId,byte[] textToEncypt,Key publicKey, Integer token) throws NoSuchAlgorithmException {
byte[] cipherText = new byte[0];

LocalDataBaseManager dbLocal = new LocalDataBaseManager();

keyPair = generateKeyPair();

String publicKey2 = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
String privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());

dbLocal.postKeys(votationId, publicKey2, privateKey);
LocalDataBaseManager.sendGeneratedToken(new Integer(votationId), token);

try {
if(TokenLocal.checkTokenDb(new Integer(votationId), token))
// Obtener el tipo de encriptacion que vamos a realizar
rsa = Cipher.getInstance("RSA");

// Encripta
rsa.init(Cipher.ENCRYPT_MODE, publicKey);
rsa.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
cipherText = rsa.doFinal(textToEncypt);

} catch (Exception e) {
Expand All @@ -121,20 +147,26 @@ public static byte[] encryptLocal (String votationId,byte[] textToEncypt, Key pu
* : Clave privada
* @return Voto desencriptado
*/
public static byte[] decryptLocal (String votationId,byte[] cipherText, Key privateKey,Integer token) {
public static byte[] decryptLocal (String votationId,byte[] cipherText,Key privateKey,Integer token) {
byte[] text = new byte[0];

LocalDataBaseManager dbLocal = new LocalDataBaseManager();

try {
if(TokenLocal.checkTokenDb(new Integer(votationId), token))
// Obtener el tipo de encriptacion que vamos a realizar
rsa = Cipher.getInstance("RSA");

byte[] encodedKey = Base64.getDecoder().decode(dbLocal.getSecretKey(votationId));
SecretKey privateKey2 = new SecretKeySpec(encodedKey, 0, encodedKey.length, "RSA");


// Desencripta
rsa.init(Cipher.DECRYPT_MODE, privateKey);
text = rsa.doFinal(cipherText);

} catch (Exception e) {
System.out.println("decrypt exception: " + e.getMessage());
System.out.println("decrypt done: " + e.getMessage());
}

return text;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/main/java/LocalDataBaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static Connection getDatabaseConnection(){
}

/**
* Función que almacena en la base de datos remota un par de claves de cifrado ElGamal
* Función que almacena en la base de datos local un par de claves de cifrado ElGamal
* asociadas a una votación
* @param id La ide de la votación.
* @param publicKey La clave pública de cifrado asociada a la votación
Expand Down

0 comments on commit 7d0ceed

Please sign in to comment.