diff --git a/pom.xml b/pom.xml index b4516bd..b0c302f 100644 --- a/pom.xml +++ b/pom.xml @@ -126,6 +126,13 @@ + + + commons-io + commons-io + 2.5 + + diff --git a/src/main/java/deployment/DespliegueTest.java b/src/main/java/deployment/DespliegueTest.java index b5c1571..d45ac7c 100644 --- a/src/main/java/deployment/DespliegueTest.java +++ b/src/main/java/deployment/DespliegueTest.java @@ -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; @@ -14,7 +18,7 @@ import main.java.Authority; import main.java.AuthorityImpl; -import java.awt.Button; +import main.java.CryptoRSA; public class DespliegueTest { @@ -29,7 +33,6 @@ public class DespliegueTest { private Integer token; private String votationId; - private JFrame frame1; private JFrame frame2; private JFrame frame3; @@ -37,8 +40,16 @@ public class DespliegueTest { 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 ------------------------------------> @@ -222,6 +233,143 @@ public void actionPerformed(ActionEvent ae){ } }); - - } - } \ No newline at end of file + + + + //<-----------------------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); + + + } + + }); + + + } + + }); + + + } + + } + \ No newline at end of file diff --git a/src/main/java/main/java/CryptoRSA.java b/src/main/java/main/java/CryptoRSA.java index 34983de..95a983a 100644 --- a/src/main/java/main/java/CryptoRSA.java +++ b/src/main/java/main/java/CryptoRSA.java @@ -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 @@ -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 @@ -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 @@ -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); @@ -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 @@ -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) { @@ -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; diff --git a/src/main/java/main/java/LocalDataBaseManager.java b/src/main/java/main/java/LocalDataBaseManager.java index ca99538..6eb7524 100644 --- a/src/main/java/main/java/LocalDataBaseManager.java +++ b/src/main/java/main/java/LocalDataBaseManager.java @@ -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