Professional Documents
Culture Documents
Splahs Screen
Splahs Screen
Splahs Screen
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);
clave");
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
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");
});
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 )
{
//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 ];
}
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;