//DES程序共2個類
//該代碼是用java的des類庫完成的,如果要用數組做的DES請到
http://blog.csdn.net/hacklew1985/archive/2007/04/14/1564958.aspx
//*************************加密類**************************
import java.security.*;
import javax.crypto.*;
import sun.misc.*;
//使用DES加密與解密,可對byte[],String類型進行加密與解密
public class DesEncrypt {
Key key;
public DesEncrypt(String str){
setKey(str);
}
//生成密匙
public void setKey(String strKey){
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
this.key = generator.generateKey();
generator = null;
}catch (Exception e){
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
}
}
// 加密String明文輸入,String密文輸出
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
// 解密:以String密文輸入,String明文輸出
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
//加密:以byte[]明文輸入,byte[]密文輸出
public byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
cipher = null;
}
return byteFina;
}
// 解密以byte[]密文輸入,以byte[]明文輸出
public byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
cipher = null;
}
return byteFina;
}
}
**************************GUI類 ************** *********
import java.awt.*;
/**
*
*/
private static final long serialVersionUID = 1L;
//聲明變量和對象
private DesEncrypt des;
private String s;
private String s2;
private String s3;
//GUI組件
private Label label1;
private Label label2;
private Label label3;
private TextField textfield1;
private TextField textfield2;
private TextField textfield3;
private Button button1;
private Button button2;
private Panel p;
//初始化組件
public void init(){
p=new Panel();
label1=new Label(" 明文");
label2=new Label(" 密碼");
label3=new Label(" 密文");
textfield1=new TextField(10);
textfield2=new TextField(10);
textfield3=new TextField(10);
button1=new Button("加密");
button2=new Button("解密");
setLayout(new GridLayout(4,3,20,25));
setBackground(Color.gray);
add(label1);
add(textfield1);
add(label2);
add(textfield2);
add(label3);
add(textfield3);
add(button1);
add(button2);
}
if(e.target instanceof Button){
s2=textfield2.getText();
des=new DesEncrypt(s2);
//對按紐1情況進行的處理
if(e.target==button1){
s=textfield1.getText();
s3=des.getEncString(s);
textfield3.setText(s3);
//加密完後清空文本
textfield1.setText(null);
}
//對按紐2情況進行的處理
else if(e.target==button2){
s=des.getDesString(s3);
textfield1.setText(s);
}
}
return true;
}
}