package cifrador; import java.awt.*; import java.awt.event.*; import java.applet.*; public class CifradoSimplePolialfabetico extends Applet { private static final long serialVersionUID = 1L; boolean isStandalone = false; private String blanco = "__________________________________________________"; Panel Panel1 = new Panel(); Label lTitulo = new Label(); Label lAlfa = new Label(); TextField tAlfa = new TextField(); Label lPlano = new Label(); TextField tPlano = new TextField(); Label lClave = new Label(); TextField tClave = new TextField(); Label lCifrado = new Label(); TextField tCifrado = new TextField(); Label lError = new Label(); Button btnCifrar = new Button(); Button btnDesCifrar = new Button(); Button btnBorrar = new Button(); GridBagLayout gbl1 = new GridBagLayout(); GridBagConstraints gbc1=new GridBagConstraints(); BorderLayout borderLayout2 = new BorderLayout(); GridLayout gridLayout1 = new GridLayout(); //Construct the applet public CifradoSimplePolialfabetico() { } //Initialize the applet public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { int ancho = Integer.parseInt(this.getParameter("WIDTH")); int alto = Integer.parseInt(this.getParameter("HEIGHT")); if (ancho < 400) ancho=400; if (alto < 300) alto=300; this.setSize(new Dimension(ancho, alto)); Panel1.setBackground(Color.lightGray); lTitulo.setText("Cifra de Sustitucion Simple Polialfabética"); lError.setForeground(Color.black); lError.setText(blanco); lAlfa.setText("Alfabeto Inicial"); tAlfa.setColumns(30); tAlfa.setEditable(true); tAlfa.setText("abcdefghijklmnñopqrstuvwxyz"); lPlano.setText("Texto Plano"); tPlano.setColumns(36); tPlano.setEditable(true); tPlano.setText("esto es una prueba"); lClave.setText("Clave"); tClave.setColumns(36); tClave.setEditable(true); tClave.setText("clave"); lCifrado.setText("TEXTO CIFRADO"); tCifrado.setColumns(36); tCifrado.setEditable(true); tCifrado.setText(""); btnCifrar.setFont(new Font("Dialog", 1, 14)); btnCifrar.setLabel("Cifrar"); btnCifrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnCifrar_actionPerformed(e); } }); btnDesCifrar.setFont(new Font("Dialog", 1, 14)); btnDesCifrar.setLabel("Descifrar"); btnDesCifrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnDesCifrar_actionPerformed(e); } }); btnBorrar.setLabel("Borrar"); btnBorrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnBorrar_actionPerformed(e); } }); this.setLayout(gridLayout1); this.add(Panel1, null); //panel 1 Panel1.setLayout(gbl1); gbc1.anchor=GridBagConstraints.CENTER; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); gbc1.gridwidth=GridBagConstraints.REMAINDER; Panel1.add(lTitulo, gbc1); gbc1.anchor=GridBagConstraints.WEST; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); Panel1.add(lAlfa, gbc1); gbc1.gridwidth=GridBagConstraints.REMAINDER; Panel1.add(tAlfa, gbc1); gbc1.anchor=GridBagConstraints.WEST; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); Panel1.add(lPlano, gbc1); gbc1.gridwidth=GridBagConstraints.REMAINDER; Panel1.add(tPlano, gbc1); gbc1.anchor=GridBagConstraints.WEST; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); Panel1.add(lClave, gbc1); gbc1.gridwidth=GridBagConstraints.REMAINDER; Panel1.add(tClave, gbc1); gbc1.anchor=GridBagConstraints.WEST; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); Panel1.add(lCifrado, gbc1); gbc1.gridwidth=GridBagConstraints.REMAINDER; Panel1.add(tCifrado, gbc1); gbc1.anchor=GridBagConstraints.CENTER; gbc1.gridwidth=1; gbc1.insets=new Insets(5,0,5,0); gbc1.gridwidth=GridBagConstraints.REMAINDER; lError.setForeground(Color.black); Panel1.add(lError, gbc1); gbc1.anchor=GridBagConstraints.CENTER; gbc1.gridwidth=1; gbc1.insets=new Insets(25,0,5,0); Panel1.add(btnCifrar, gbc1); Panel1.add(btnDesCifrar, gbc1); Panel1.add(btnBorrar, gbc1); gbc1.gridwidth=GridBagConstraints.REMAINDER; } void btnCifrar_actionPerformed(ActionEvent e) { int error = 0; lError.setForeground(Color.black); lError.setText(blanco); //Alfabeto inicial String alfabeto = tAlfa.getText().toLowerCase(); //construir los alfabetos a partir del inicial String[] tAlfabeto = new String[alfabeto.length()]; for(int i=0; i < alfabeto.length(); i++){ tAlfabeto[i] = alfabeto.substring(i, alfabeto.length()) + alfabeto.substring(0, i) ; } String entrada = tPlano.getText().toLowerCase(); String clave = tClave.getText().toLowerCase(); String xCifrado = ""; //Comprobar que ha puesto una clave if (clave.length() == 0){ lError.setForeground(Color.red); lError.setText("Debe poner la clave."); error = 1; }else{ //Comprobar que los caracteres de la clave están en el alfabeto for(int i=0; i < clave.length(); i++) if (alfabeto.lastIndexOf(clave.substring(i, i+1)) == -1){ lError.setForeground(Color.red); lError.setText("Algún caracter de la clave no está en el alfabeto inicial."); error = 1; } } if (error == 0){ //Empezar a cifrar los caracteres for(int i=0; i < entrada.length(); i++){ //Si hay espacio, lo respeto if (entrada.substring(i, i+1).equals(" ")) xCifrado = xCifrado + " "; else{ //posC: La posición del caracter de la clave en el alfabeto inicial, para saber que alfabeto utilizo int posC = alfabeto.lastIndexOf(clave.substring(i % clave.length() , i% clave.length() + 1)) ; //posP: La posición del caracter del texto plano en el alfabeto inicial int posP = alfabeto.lastIndexOf(entrada.substring(i, i+1)) ; //Si no encuentro el caracter, pongo una interrogación if (posP == -1) xCifrado = xCifrado + "?"; else xCifrado = xCifrado + tAlfabeto[posC].charAt(posP); //Coge el caracter posP del alfabeto posC } } tCifrado.setText(xCifrado.toUpperCase()); } } void btnDesCifrar_actionPerformed(ActionEvent e) { int error = 0; lError.setForeground(Color.black); lError.setText(blanco); //Alfabeto inicial String alfabeto = tAlfa.getText().toLowerCase(); //construir los alfabetos a partir del inicial String[] tAlfabeto = new String[alfabeto.length()]; for(int i=0; i < alfabeto.length(); i++){ tAlfabeto[i] = alfabeto.substring(i, alfabeto.length()) + alfabeto.substring(0, i) ; } String xEntrada = "";//tPlano.getText().toLowerCase(); String clave = tClave.getText().toLowerCase(); String xCifrado = tCifrado.getText().toLowerCase(); //Comprobar que ha puesto una clave if (clave.length() == 0){ lError.setForeground(Color.red); lError.setText("Debe poner la clave."); error = 1; }else{ //Comprobar que los caracteres de la clave están en el alfabeto for(int i=0; i < clave.length(); i++) if (alfabeto.lastIndexOf(clave.substring(i, i+1)) == -1){ lError.setForeground(Color.red); lError.setText("Algún caracter de la clave no está en el alfabeto inicial."); error = 1; } } if (error == 0){ //Empezar a descifrar los caracteres for(int i=0; i < xCifrado.length(); i++){ //Si hay espacio, lo respeto if (xCifrado.substring(i, i+1).equals(" ")) { xEntrada = xEntrada + " "; }else{ //posC: posicion del caracter de la clave en el alfabeto inicial int posC = alfabeto.lastIndexOf(clave.substring(i % clave.length() , i% clave.length() + 1)) ; //posP: posicion del caracter del texto cifrado en el alfabeto utilizado para cifrarlo // que corresponderá al caracter de la clave int posP = tAlfabeto[posC].lastIndexOf(xCifrado.substring(i, i+1)) ; //Si no encuentro el caracter, pongo una interrogación if (posP == -1) xEntrada = xEntrada + "?"; else xEntrada = xEntrada + alfabeto.charAt(posP); //Coge el caracter posP del alfabeto inicial } } tPlano.setText(xEntrada.toLowerCase()); } } void btnBorrar_actionPerformed(ActionEvent e) { tPlano.setText(""); tCifrado.setText(""); tClave.setText(""); } }