加密2
karte 2011-04-09 12:42:30 /**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
private 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) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
* @return
*/
private 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) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
//转化为16进制字符串
public static String byte2hex(byte[] b){
String hs="";
String stmp="";
for(int n=0;n<b.length;n++){
stmp=(java.lang.Integer.toHexString(b[n]&0XFF));
if(stmp.length()==1)hs=hs+"0"+stmp;
else hs=hs+stmp;
if(n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String args[]) {
Des des=new Des();//实例化一个对像
des.getKey("ortel");//生成密匙
String strEnc = des.getEncString("43534534");//加密字符串,返回String的密文
System.out.println("加密文:"+strEnc);
String strDes = des.getDesString(strEnc);//把String 类型的密文解密
System.out.println("解密文:"+strDes);
System.out.println((byte)'0');
}
}