Professional Documents
Culture Documents
Eje 3: Criptografia en Java
Eje 3: Criptografia en Java
Eje 3: Criptografia en Java
Elaborar un programa en java haciendo uso de clases criptográficas para cifrar contraseñas
Programa en Java
Imágenes
Código
Formulario_Login.java
import javax.swing.JOptionPane;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Zaraza Toro
*/
public class Formulario_Login extends javax.swing.JFrame {
/**
* Creates new form Formulario_Login
*/
public Formulario_Login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
TxUser = new javax.swing.JTextField();
TxPass = new javax.swing.JPasswordField();
btIngresar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setText("Usuario");
jLabel3.setText("Contraseña");
TxUser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxUserActionPerformed(evt);
}
});
btIngresar.setText("Ingresar");
btIngresar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btIngresarActionPerformed(evt);
}
});
pack();
}
try {
String encriptador = simetrico.encriptar(Usuario, ("Encriptado"));
String Result = ("\n Su Usuario cifrado es : " +Usuario +" con cifrado simetrico "
+encriptador)+ "\n"+Separador+
"\n" +Mensaje ;
JOptionPane.showMessageDialog(null,Result);
} catch(UnsupportedEncodingException | NoSuchAlgorithmException |
InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException |
BadPaddingException ex) {
Logger.getLogger(aes.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Formulario_Login.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Formulario_Login.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Formulario_Login.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Formulario_Login.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Formulario_Login().setVisible(true);
}
});
}
}
Class rsa.java
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
public static void main(String[] args) {
// TODO code application logic here
}
private byte[] claveCifrada = null;
private byte[] claveDesCifrada = null;
//Clave privada
//Clave pública
PublicKey clavePublica = clavesRSA.getPublic();
//Texto plano
byte[] bufferClaro = msjManager.getBytes();
//Ciframos con clave pública el texto plano utilizando RSA
Cipher cifrador = Cipher.getInstance("RSA");
cifrador.init(Cipher.ENCRYPT_MODE, clavePublica);
// System.out.println("Cifrar con clave pública el Texto:");
mostrarBytes(bufferClaro);
//Realización del cifrado del texto plano
byte[] bufferCifrado = cifrador.doFinal(bufferClaro);
// System.out.println("Texto CIFRADO");
setClaveCifrada(mostrarBytes(bufferCifrado));
//System.out.println("\n_______________________________");
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
Conclusiones
Se aplicó los algoritmos de encriptación RSA y AES uno siendo asimétrico para la
seguridad parte fundamental, desde el inicio del programa siendo el login de acceso
hasta su transacción final, debe brindar los protocolos necesarios para cumplir con la