[code]import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.;
import javax.crypto.spec.;
import java.security.;
import java.security.spec.;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
public final class Cripto2 extends JFrame{
private static SecretKey skey;
private static KeySpec ks;
private static PBEParameterSpec ps;
private static final String algorithm = “PBEWithMD5AndDES”;
private static BASE64Encoder enc = new BASE64Encoder();
private static BASE64Decoder dec = new BASE64Decoder();
JLabel nome1,nome2;
JButton descri,cripto,botaovoltar;
static {
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
ps = new PBEParameterSpec (new byte[]{3,1,4,1,5,9,2,6}, 20);
ks = new PBEKeySpec ("EAlGeEen3/m8/YkO".toCharArray());
skey = skf.generateSecret (ks);
} catch (java.security.NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (java.security.spec.InvalidKeySpecException ex) {
ex.printStackTrace();
}
}
public static final String encrypt(final String text)
throws
BadPaddingException,
NoSuchPaddingException,
IllegalBlockSizeException,
InvalidKeyException,
NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
final Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, skey, ps);
return enc.encode (cipher.doFinal(text.getBytes()));
}
public static final String decrypt(final String text)
throws
BadPaddingException,
NoSuchPaddingException,
IllegalBlockSizeException,
InvalidKeyException,
NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
final Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, skey, ps);
String ret = null;
try {
ret = new String(cipher.doFinal(dec.decodeBuffer (text)));
} catch (Exception ex) {
}
return ret;
}
// a classe construtor
public Cripto2(){
super("Algoritmo-2");
Container tela = getContentPane();
tela.setLayout(null);
// nome
nome1 = new JLabel("ALGORITMO 2!!! ");
nome1.setBounds(125,20,200,20);
//butão
descri = new JButton ("Descriptografar");
descri.setBounds(165,100,150,20);
cripto = new JButton ("Criptografar/Descriptografar");
cripto.setBounds(40,100,250,20);
botaovoltar = new JButton("Voltar");
botaovoltar.setBounds(100,150,120,20);
// exibe no frame
tela.add(nome1);
//tela.add(descri);
tela.add(cripto);
tela.add(botaovoltar);
botaovoltar.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
int opc = JOptionPane.showConfirmDialog(null, "Você desejar voltar realmente?",
"Atenção",2,
JOptionPane.YES_NO_OPTION);
if (opc == JOptionPane.YES_OPTION) {
dispose();
this.mouseExited(e);
}
}
});
cripto.addActionListener (
new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
aviso();
} catch (Exception ex) {
Logger.getLogger(Cripto2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
);
}
public static void main(String[] args) {
Cripto2 p = new Cripto2();
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setSize(340,280);
p.setVisible(true);
}
public static void aviso()throws Exception {
String password =JOptionPane.showInputDialog(null,"Digite o texto para criptografar");
String encoded = Cripto2.encrypt (password);
JOptionPane.showMessageDialog(null,(encoded),"O Texto Criptografado",
JOptionPane.INFORMATION_MESSAGE);
Cripto2.decrypt(password);
JOptionPane.showMessageDialog(null, (password),"O texto Descriptografado",
JOptionPane.INFORMATION_MESSAGE);
}
}[/code]
tenho esse aqui ver se vc aproveita