programa encriptar

11
PROGRAMA ENCRIPTAR – DESENCRIPTAR EN JAVA PARA WINDOWS En una aplicación para windows introducimos todos los componentes del formulario Y añadimos el código siguiente en el formulario: Form1.java: import com.ms.wfc.app.*; import com.ms.wfc.core.*; import com.ms.wfc.ui.*; import com.ms.wfc.html.*; public class Form1 extends Form { boolean accion=true; String clave, texto; public Form1() { // Required for Visual J++ Form Designer support initForm(); radioButton1.setChecked(true); radioButton2.setChecked(false); // TODO: Add any constructor code after initForm call }

Upload: lena-lawliet

Post on 12-Nov-2014

44 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Programa Encriptar

PROGRAMA ENCRIPTAR – DESENCRIPTAR EN JAVA PARA WINDOWS

 

 En una aplicación para windows introducimos todos los componentes del formularioY añadimos el código siguiente en el formulario: Form1.java: import com.ms.wfc.app.*;import com.ms.wfc.core.*;import com.ms.wfc.ui.*;import com.ms.wfc.html.*; public class Form1 extends Form{                               boolean accion=true;                               String clave,  texto;                                                                                         public Form1()                               {                                                              // Required  for Visual J++ Form Designer support                                                              initForm();                                                                                radioButton1.setChecked(true);                                                             radioButton2.setChecked(false);                                                                                                                             // TODO: Add any constructor code after  initForm call                               }                                 /**                                 * Form1 overrides dispose so it can clean up the                                 * component list.                                 */                               public void dispose()                               {                                                             super.dispose();                                                             components.dispose();                               }

Page 2: Programa Encriptar

                                private void radioButton1_checkedChanged(Object source, Event e)                               {                                                              if  (radioButton1.getChecked())                                                             {                                                                                             accion=true;                                                                                             button1.setText("Encriptar");                                                                                                                                                                                                                                                                                   }                                                             else                                                             {                                                                                             accion=false;                                                                                             button1.setText("Desencriptar");                                                             }                                                                                                                       }                                private void button1_click(Object source, Event e)                               {                                                              if  (edit1.getText().equals(""))                                                             {                                                                                             MessageBox.show("Introduzca una clave de encriptado","Error, falta clave");                                                                                             edit1.focus();                                                                                             return;                                                             }                                                              if  (accion)                                                             {                                                                                              if  (edit2.getText().equals(""))                                                                                             {                                                                                                                             MessageBox.show("Introduzca texto a encriptar","Error, falta texto");                                                                                                                             edit2.focus();                                                                                                                             return;                                                                                             }                                                                                             clave=edit1.getText();                                                                                              texto=edit2.getText();                                                                                             edit3.setText(new EncriptCadena(clave,  texto,accion).retorno);                                                             }                                                             else                                                             {                                                                                              if  (edit3.getText().equals(""))                                                                                             {                                                                                                                             MessageBox.show("Introduzca texto a desencriptar","Error, falta texto");                                                                                                                             edit3.focus();                                                                                                                             return;                                                                                             }                                                                                             clave=edit1.getText();                                                                                              texto=edit3.getText();                                                                                             edit2.setText(new EncriptCadena(clave,  texto,accion).retorno);                                                                }                                                                                                                    

Page 3: Programa Encriptar

                                                                                          }

                       El Resto de código lo añade el editor directamente             Se añade la clase que encripta y desencripta: y la llamamos EncriptCadena.java public class EncriptCadena{                               public String retorno="";                               public EncriptCadena(String c, String t, boolean a)                               {                                                              int  i=0, j=0,  ic=c.length()                              ,it=t.length();                                                              int  temp;                                                              int[] claveascii = new int[ic];                                                              int[]  textoascii = new int[it];                                                                                                                         for (i=0;  i<ic;i++)                       claveascii[i]=c.charAt(i);                                                                                                                                                         for (i=0;  i<it;i++)                          textoascii[i]=t.charAt(i);                                                                                                                         if(a)                                                             {                                                                                              for (i=0;  i<it;i++)                                                                                                                    {                                                                                                                              j++;                                                                                                                              if  (j>=ic)j=0;                                                                                                                                                                                                                                                         temp= textoascii[i]+claveascii[j];                                                                                                                              if  (temp > 255) temp=temp-255;                                                                                                                             retorno=retorno + (char)temp;                                                                                                                                                                                                                        }                                                             }                                                             else                                                             {                                                                                              for (i=0;  i<it;i++)                                                                                                                    {                                                                                                                              j++;                                                                                                                              if  (j>=ic)j=0;                                                                                                                                                                                                                                                       temp= textoascii[i]-claveascii[j];                                                                                                                              if  (temp < 0)  temp=temp+256;                                                                                                                             retorno=retorno + (char)temp;                                                                                                                                                                                                                                                      }                                                                                                                                                        }                                                                                          }}

 

Page 4: Programa Encriptar

Encriptar / Desencriptar Texto en S-DES con Java

Ya habíamos visto anteriormente como encriptar ficheros con Java en

AES-256, el caso de hoy será semejante, a diferencia que no encriptará

ficheros completos, sino texto plano. El ejemplo es sencillo de utilizar,

necesitamos la palabra a encriptar, la llave, que es una palabra secreta

definida por nosotros, cuanto mas compleja ésta, más difícil violar el

encriptado a fuerza bruta, les recomiendo que tenga una extensión de 16 o

32 caracteres, y un booleano que sirve para determinar la operación a

realizar, y encriptar o desencriptar.

Este código se tiene que tener cuidado para utilizarlo, ya que si lo

encriptado se hizo en un sistema de codificación y el desencriptado en

otro, no funcionará, ejemplo: Windows utiliza ASCII para éste efecto

y GNU/Linux Unicode, así que lo que fue encriptado en Windows no se

podrá desencriptar en GNU/Linux. Utilícenlo únicamente si siempre será

en el mismo Sistema Operativo o si cambian los sistemas de codificación

por defecto de los mismos.123456789

101112131415161718192021222324252627282930

package comunes; /** * Algoritmo de Encripatado S-DES */public class EncriptCadena { public String retorno = "";  public String EncriptDecryptCadena(String c, String t, boolean a) { int i = 0, j = 0, ic = c.length(), it = t.length(); int temp; int[] claveascii = new int[ic]; int[] textoascii = new int[it]; for (i = 0; i < ic; i++) claveascii[i] = c.charAt(i);  for (i = 0; i < it; i++) textoascii[i] = t.charAt(i);  if (a) { for (i = 0; i < it; i++) { j++; if (j >= ic) j = 0; temp = textoascii[i] + claveascii[j]; if (temp > 255) temp = temp - 255; retorno = retorno + (char) temp; }//end for

Page 5: Programa Encriptar

3132333435363738394041424344

} else { for (i = 0; i < it; i++) { j++; if (j >= ic) j = 0; temp = textoascii[i] - claveascii[j];  if (temp < 0) temp = temp + 256; retorno = retorno + (char) temp; }//end for }//end if return retorno; }//end EncriptDecryptCadena}//end class

Post to Twitter

Page 6: Programa Encriptar

PROGRAMA ENCRIPTAR – DESENCRIPTAR EN JAVA STANDARD 

 Creamos una clase principal. Class1.java con el codigo siguiente: import  java.awt.*;import  java.awt.event.*;  /** * This class can take a variable number of parameters on the command *  line. Program execution begins with the main() method. The class * constructor  is not invoked unless an object of type 'Class1' * created in the main() method. */public class Class1 extends Frame implements ActionListener,  ItemListener{                               boolean accion=true;                               TextField clave;                               TextArea texto1;                               TextArea texto2;                               Checkbox op1,op2;                               Button ejecutar;                               String  textoboton="";                               Panel panel1,panel2,panel3;                                                            public Class1(String nombre)                               {                                                             super(nombre);                                                             panel1 = new Panel();                                                                                             panel1.add(new Label("Escoje    la acción"));                                                                                             CheckboxGroup acciones = new CheckboxGroup();                                                                                             op1 = new Checkbox("Encriptar",acciones,true);                                                                                             op2 = new Checkbox("Desencriptar",acciones,false);                                                                                             op1.addItemListener(this);                                                                                             op2.addItemListener(this);                                                                                             panel1.add(op1);                                                                                             panel1.add(op2);                                                                                                                                                                                                                                                   panel2 = new Panel();                                                                                             panel2.add(new Label("Texto a encriptar"));

Page 7: Programa Encriptar

                                                                                              texto1 = new TextArea(10,10);                                                                                             panel2.add(texto1);                                                                                             panel2.add(new Label("Texto a desencriptar"));                                                                                              texto2 = new TextArea(10,10);                                                                                             panel2.add(texto2);                                                             panel3 = new Panel();                                                                                             panel3.add(new Label("Introduce la Clave"));                                                                                             clave = new TextField(20);                                                                                             panel3.add(clave);                                                                                              textoboton="Encriptar";                                                                                             ejecutar= new Button(textoboton);                                                                                             ejecutar.addActionListener(this);                                                                                                                                                                                           panel3.add(ejecutar);                                                                        setSize(450,250);                                                             BorderLayout rejilla = new BorderLayout();                                                             setLayout(rejilla);                                                                                             add("North",panel1);                                                                                             add("West",panel2);                                                                                             add("South",panel3);                                                                                             addWindowListener(new Cerrar());                                                                                             setVisible(true);                                                                                          }                                                            public void actionPerformed(ActionEvent ae)                               {                                                              if  (clave.getText().equals(""))                                                             {                                                                                             clave.setText("Introduzca una clave de encriptado");                                                                                             return;                                                             }                                                              if  (accion)                                                             {                                                                                              if  (texto1.getText().equals(""))                                                                                             {                                                                                                                              texto1.setText("Introduzca texto a encriptar");                                                                                                                             return;                                                                                             }                                                                                                                                                                                                                                                                                    texto2.setText(new EncriptCadena(clave.getText(), texto1.getText(),accion).retorno);                                                             }                                                             else                                                             {                                                                                              if  (texto2.getText().equals(""))                                                                                             {                                                                                                                              texto2.setText("Introduzca texto a desencriptar");                                                                                                                             return;                                                                                             }                                                                                                                                                         texto1.setText(new EncriptCadena(clave.getText(), texto2.getText(),accion).retorno);                                                              }                         

Page 8: Programa Encriptar

                                }                               public void  itemStateChanged(ItemEvent  ie)                               {                                                             accion=op1.getState();                                                                                                                                                     repaint();                               }                                class Cerrar extends WindowAdapter                               {                                                             public void windowClosing(WindowEvent   we)                                                             {                                                                                             System.exit(0);                                                             }                               }                                                            public void paint(Graphics g)                               {                                                                                                                       }                               public static void main (String[] args)                               {                                                             Class1 c = new Class1("Encriptar-Desencriptar");                               }} 

y añadimos la clase EncriptCadena.java del programa Encriptar-Desencriptar de Windows. public class EncriptCadena{                public String retorno="";                public EncriptCadena(String c, String t, boolean a)                {                               int i=0, j=0, ic=c.length()    ,it=t.length();                               int temp;                               int[] claveascii = new int[ic];                               int[] textoascii = new int[it];                                                             for (i=0; i<ic;i++)  claveascii[i]=c.charAt(i);                                                                             for (i=0; i<it;i++)  textoascii[i]=t.charAt(i);                                                             if(a)                               {                                               for (i=0; i<it;i++)                                                {                                                               j++;                                                               if (j>=ic)j=0;                                                                                                                             temp= textoascii[i]+claveascii[j];                                                               if (temp > 255) temp=temp-255;

Page 9: Programa Encriptar

                                                               retorno=retorno + (char)temp;                                                                                                             }                               }                               else                               {                                               for (i=0; i<it;i++)                                                {                                                               j++;                                                               if (j>=ic)j=0;                                                                                                                             temp= textoascii[i]-claveascii[j];                                                               if (temp < 0) temp=temp+256;                                                               retorno=retorno + (char)temp;                                                                                                                            }                                                                             }                                              }}