APPLET ENCRIPTAR –DESENCRIPTAR
Creamos applet encriptar –desencriptar:
La pagina web puede ser el codigo siguiente:
<HTML>
<HEAD>
<
</HEAD>
<BODY>
<P> </P>
<!-- Insert HTML here
-->
<APPLET style="LEFT: 0px; WIDTH: 500px; TOP: 0px; HEIGHT: 215px"
height=215 width=443 code=Applet1.class name=Applet1
VIEWASTEXT>
<PARAM NAME="foreground"
VALUE="FFFFFF">
<PARAM NAME="background"
VALUE="008080">
<PARAM NAME="label" VALUE="This string was passed from the HTML
host."></APPLET>
</BODY>
</HTML>
El applet1.java es el
siguiente:
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/**
* This class reads PARAM tags from its
HTML host page and sets
* the color and
label properties of the applet. Program execution
* begins with the init() method.
*/
public class Applet1 extends Applet
implements ActionListener, ItemListener
{
boolean
accion=true;
TextField clave;
TextArea
texto1;
TextArea texto2;
Checkbox op1,op2;
Button ejecutar;
String
textoboton="";
Panel panel1,panel2,panel3;
public void
init()
{
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"));
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);
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);
}
}
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)
{
}
}
La Clase 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;
}
}
}
}