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
}
/**
* Form1 overrides dispose so
it can clean up the
* component
list.
*/
public void
dispose()
{
super.dispose();
components.dispose();
}
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);
}
}
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;
}
}
}
}