Splahs Screen

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SplasScreen

package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import Utilities.CryptingUtilities;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class SplashScreen extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private final String CODER = "coder";
private final String DECODER = "decoder";
private CryptingUtilities cr;
private JTextField txtTam;
private JTextField txtP;
private JTextField txtQ;
private JTextField txtPublic;
private JTextField txtPrivate;
private int keyLenght;
private void clearAll( )
{
txtP.setText( "" );
txtQ.setText( "" );
txtPublic.setText( "" );
txtPrivate.setText( "" );
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SplashScreen frame = new SplashScreen();
JOptionPane.showMessageDialog(null, "Se han generado las claves por defecto");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SplashScreen() {
keyLenght = 50;
cr = new CryptingUtilities( keyLenght );
setResizable(false);
setTitle("Codificador y decodificador RSA");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 389);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 444, 361);
contentPane.add(panel);
panel.setLayout(null);
JLabel label = new JLabel("Codificador y decodificador RSA");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 16));
label.setBounds(10, 29, 424, 24);
panel.add(label);

JButton btnCode = new JButton("Codificar");


btnCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Coder frame = new Coder( cr, CODER );
frame.setVisible( true );
}
});
btnCode.setBounds(87, 131, 89, 23);
panel.add(btnCode);
JButton btnDecode = new JButton("Decodificar");
btnDecode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Coder frame = new Coder( cr, DECODER );
frame.setVisible( true );
}
});
btnDecode.setBounds(186, 131, 89, 23);
panel.add(btnDecode);
JLabel lblAContinuacinEscoja = new JLabel("A continuaci\u00F3n, escoja la opci\u00F3n que desea
realizar.");

lblAContinuacinEscoja.setFont(new Font("Arial", Font.PLAIN, 14));


lblAContinuacinEscoja.setHorizontalAlignment(SwingConstants.CENTER);
lblAContinuacinEscoja.setBounds(10, 82, 424, 14);
panel.add(lblAContinuacinEscoja);
JButton btnExit = new JButton("Salir");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
btnExit.setBounds(285, 131, 89, 23);
panel.add(btnExit);

clave");

JButton btnVolverAGenerar = new JButton("Volver a generar claves");


btnVolverAGenerar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if( txtTam.getText().equals( "" ))
JOptionPane.showMessageDialog(null, "Debe digitar un tamao de clave");
else
{
try
{
keyLenght = Integer.parseInt( txtTam.getText( ) );
if( keyLenght <= 1 )
{
JOptionPane.showMessageDialog(null, "Tamao invlido de
}
else
{

cr = new CryptingUtilities( keyLenght );


txtTam.setText( "" + keyLenght );
txtP.setText( "" + cr.getP() );
txtQ.setText( "" + cr.getQ() );
txtPublic.setText( "(" + cr.getN() + ", " + cr.getE() + ")" );
txtPrivate.setText( "(" + cr.getN() + ", " + cr.getD() + ")" );
JOptionPane.showMessageDialog(null, "Se han generado

nuevas claves");

numrico");

clearAll();

}
}
catch( NumberFormatException E)
{
JOptionPane.showMessageDialog(null, "El tamao de clave debe ser
}

}
}

});
btnVolverAGenerar.setBounds(87, 165, 287, 23);
panel.add(btnVolverAGenerar);
JLabel lblTamaoDe = new JLabel("Tama\u00F1o de la clave");
lblTamaoDe.setFont(new Font("Arial", Font.PLAIN, 14));
lblTamaoDe.setBounds(87, 216, 130, 14);
panel.add(lblTamaoDe);
JLabel lblP = new JLabel("p");
lblP.setFont(new Font("Arial", Font.PLAIN, 14));
lblP.setBounds(87, 241, 46, 14);
panel.add(lblP);
JLabel lblQ = new JLabel("q");
lblQ.setFont(new Font("Arial", Font.PLAIN, 14));
lblQ.setBounds(87, 266, 46, 14);
panel.add(lblQ);
JLabel lblClavePblican = new JLabel("Clave p\u00FAblica (n, e)");
lblClavePblican.setFont(new Font("Arial", Font.PLAIN, 14));
lblClavePblican.setBounds(87, 291, 130, 14);
panel.add(lblClavePblican);
JLabel lblClavePrivadanD = new JLabel("Clave privada (n, d)");
lblClavePrivadanD.setFont(new Font("Arial", Font.PLAIN, 14));
lblClavePrivadanD.setBounds(87, 316, 130, 14);
panel.add(lblClavePrivadanD);
txtTam = new JTextField();
txtTam.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
clearAll();
}
});
txtTam.setBounds(227, 214, 147, 20);
txtTam.setText( "" + keyLenght );
panel.add(txtTam);
txtTam.setColumns(10);
txtP = new JTextField();
txtP.setColumns(10);
txtP.setBounds(227, 239, 147, 20);
txtP.setText( "" + cr.getP() );
panel.add(txtP);
txtQ = new JTextField();
txtQ.setColumns(10);
txtQ.setBounds(227, 264, 147, 20);
txtQ.setText( "" + cr.getQ() );
panel.add(txtQ);
txtPublic = new JTextField();
txtPublic.setColumns(10);
txtPublic.setBounds(227, 289, 147, 20);
txtPublic.setText( "(" + cr.getN() + ", " + cr.getE() + ")" );
panel.add(txtPublic);

}
}
Coder

txtPrivate = new JTextField();


txtPrivate.setColumns(10);
txtPrivate.setBounds(227, 314, 147, 20);
txtPrivate.setText( "(" + cr.getN() + ", " + cr.getD() + ")" );
panel.add(txtPrivate);

package GUI;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import
import
import
import

java.awt.event.ActionListener;
java.awt.event.ActionEvent;
java.io.*;
java.math.BigInteger;

import Utilities.CryptingUtilities;
public class Coder extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton btnSave;
private JButton btnAccept;
private JButton btnBack;
private final static String DEFAULT = "default";
private final String CODER = "coder";
private final String DECODER = "decoder";
private final String FILE = "RSAOutput.txt";
/**
* Create the frame.
*/
public Coder( CryptingUtilities cr, String mode ) {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 475);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 444, 447);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblCodificadorRsa = new JLabel("Codificador/Decodificador RSA");
lblCodificadorRsa.setBounds(10, 25, 424, 22);
lblCodificadorRsa.setHorizontalAlignment(SwingConstants.CENTER);
lblCodificadorRsa.setFont(new Font("Arial", Font.BOLD, 14));
panel.add(lblCodificadorRsa);
JTextArea txtInfo = new JTextArea();
txtInfo.setBounds(10, 58, 424, 89);
txtInfo.setOpaque( false );
txtInfo.setEditable(false);
txtInfo.setFont(new Font("Arial", Font.PLAIN, 14));
txtInfo.setText("A continuaci\u00F3n, ingrese la cadena o arreglo que desea manipular, y luego del
proceso aparecer\u00E1 la respuesta en la caja de texto inferior, puede copiarla directamente o guardarla en un archivo
de texto que estar\u00E1 guardado por defecto en la carpeta de este proyecto llamado \"RSAOutput.txt\".");
txtInfo.setWrapStyleWord(true);
txtInfo.setLineWrap(true);
txtInfo.setRows(5);
panel.add(txtInfo);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 153, 424, 102);
panel.add(scrollPane);
JTextArea textInput = new JTextArea();
textInput.setWrapStyleWord(true);
textInput.setLineWrap(true);
scrollPane.setViewportView(textInput);
JLabel lblSalida = new JLabel("Salida");

lblSalida.setFont(new Font("Arial", Font.PLAIN, 14));


lblSalida.setBounds(10, 266, 46, 14);
panel.add(lblSalida);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 286, 424, 102);
panel.add(scrollPane_1);
JTextArea textOutput = new JTextArea();
textOutput.setWrapStyleWord(true);
textOutput.setLineWrap(true);
scrollPane_1.setViewportView(textOutput);
btnAccept = new JButton("Codificar");
btnAccept.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if( mode.equals(DEFAULT) || mode.equals(CODER))
{
BigInteger[] coded = cr.crypt( textInput.getText( ) );
String output = "";
for( int i = 0; i < coded.length; i++ )
output += coded[ i ] + " ";
textOutput.setText( output );
}
else if( mode.equals(DECODER))
{
String input = textInput.getText();
String[] inputArray = input.split( " " );
BigInteger[] coded = new BigInteger[ inputArray.length ];
for( int i = 0; i < inputArray.length; i++)
{
coded[ i ] = new BigInteger( inputArray[ i ] );
}
String output = cr.decrypt( coded );
textOutput.setText( output );
}
JOptionPane.showMessageDialog(null, "Operacin completada.");
}
});
btnAccept.setBounds(78, 413, 89, 23);
panel.add(btnAccept);
btnSave = new JButton("Guardar");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
try
{
PrintWriter pw = new PrintWriter( new File ( FILE ) );
pw.append( textOutput.getText() );
JOptionPane.showMessageDialog(null, "Salvado completo
satisfactoriamente.");
pw.flush();
pw.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
});
btnSave.setBounds(177, 413, 89, 23);
panel.add(btnSave);
btnBack = new JButton("Volver");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
dispose( );
}

});
btnBack.setBounds(276, 413, 89, 23);
panel.add(btnBack);
if( mode.equals(DEFAULT) || mode.equals(CODER))
{
setTitle("Codificador");
btnAccept.setText( "Codificar" );
}
else if( mode.equals(DECODER))
{
setTitle( "Decodificador" );
btnAccept.setText( "Decodificar" );
}
}

CryptingUtilities
package Utilities;
import java.math.BigInteger;
import java.util.Random;
public class CryptingUtilities
{
private BigInteger[] data;
private BigInteger p, q;
//Constructor de la clase, genera los nmeros p, q y las claves pblica y privada, cuyos datos son guardados en un
arreglo de BigInteger
public CryptingUtilities( int maxLenght )
{
p = new BigInteger( maxLenght, 10, new Random( ) );
do
q = new BigInteger( maxLenght, 10, new Random( ) );
while( q.compareTo( p ) == 0 );
data = NumericUtilities.generateKeys(maxLenght, p, q );
}
//Encripta una cadena de texto
public BigInteger[] crypt( String crypted )
{
int[] message = TextUtilities.StringToASCII( crypted );
BigInteger[] ans = new BigInteger[ message.length ];
for( int i = 0; i < message.length; i++ )
ans[ i ] = BigInteger.valueOf( message[ i ] ).modPow( data[ 1 ], data[ 0 ] );
return ans;
}
//Desencripta un arreglo de BigInteger donde se supone hay un mensaje encriptado
public String decrypt( BigInteger[] message )
{

String ans = "";


BigInteger[] decrypted = new BigInteger[ message.length ];
for( int i = 0; i < message.length; i++ )
decrypted[ i ] = message[ i ].modPow( data[ 2 ], data[ 0 ] );
for( int i = 0; i < message.length; i++ )
ans += (char) decrypted[ i ].intValue();
return ans;

//devuelve el valor de P
public BigInteger getP( )
{
return p;
}
//devuelve el valor de Q
public BigInteger getQ( )
{

return q;
}
//Devuelve el valor comn en las claves N
public BigInteger getN( )
{
return data[ 0 ];
}
//devuelve el valor de la clave pblica E
public BigInteger getE( )
{
return data[ 1 ];
}

//devuelve el valor de clave privada D


public BigInteger getD( )
{
return data[ 2 ];
}

NumericUtilities
package Utilities;
import java.math.BigInteger;
import java.util.*;
public class NumericUtilities
{
//Devuelve un BigInteger aleatorio
public static BigInteger getRandomBigInt( int maxLenght )
{
return new BigInteger( maxLenght, 10, new Random( ) );
}
//obtiene el valor de la funcin totient, basado en los nmeros q y p
public static BigInteger getTotient( BigInteger p, BigInteger q )
{
return (p.subtract( new BigInteger( "1" ) ) ).multiply(
(q.subtract( new BigInteger( "1" ) ) ) );
}
//funcin que devuelve el mximo comn divisor de dos nmeros a y b
public static BigInteger getGCD( BigInteger a, BigInteger b )
{
if( b.compareTo( new BigInteger( "0" ) ) == 0 )
return a;
else
return getGCD( b, a.mod( b ) );
}
//genera un arreglo de BigInteger donde se almacenan los valores de las claves pblicas y privadas, a saber:
//en la posicin 0 se guarda n, en la 1 e y en la 2 d
public static BigInteger[] generateKeys( int maxLenght, BigInteger p, BigInteger q )
{
BigInteger e = new BigInteger( "0" ), totient = getTotient( p, q );
do
{
e = getRandomBigInt( maxLenght );
}while( e.compareTo( new BigInteger( "1" ) ) > 0 && e.compareTo( totient ) < 0
&& getGCD( e, totient ).compareTo( new BigInteger( "1" ) ) == 1);
BigInteger[] ans = { p.multiply( q ), e, e.modInverse( totient ) };
return ans;
}
}
TextUtilities
package Utilities;

public class TextUtilities


{
//Convierte una cadena en un arreglo con los equivalentes ascii de cada caracter en la cadena
public static int[] StringToASCII ( String str )
{
int[] ans = new int[ str.length() ];
for( int i = 0; i < str.length(); i++ )
ans[ i ] = (int) str.charAt( i );
return ans;
}
//convierte un arreglo de nmeros, correspondientes al cdigo ascii, en una cadena String
public static String ASCIIToString( int[] array )
{
String ans = "";
for( int i = 0; i < array.length; i++ )
ans += (char) array[ i ];
return ans;
}
}

You might also like