package com.cube.limail.util;
import javax.crypto.cipher;
import javax.crypto.keygenerator;
import javax.crypto.secretkey;
public class eryptogram
{
private static string algorithm ="des";
private string key="cb7a92e3d3491964";
//定义 加密算法,可用 des,desede,blowfish
static boolean debug = false ;
public eryptogram ()
{
}
public static byte [] getsecretkey () throws exception
{
keygenerator keygen = keygenerator.getinstance (algorithm );
secretkey deskey = keygen.generatekey ();
system.out.println ("生成密钥:"+bytestohexstring (deskey.getencoded ()));
if (debug ) system.out.println ("生成密钥:"+bytestohexstring (deskey.getencoded ()));
return deskey.getencoded ();
}
public static byte [] encryptdata (byte [] input ,byte [] key ) throws exception
{
secretkey deskey = new javax.crypto.spec.secretkeyspec (key ,algorithm );
if (debug )
{
system.out.println ("加密前的二进串:"+byte2hex (input ));
system.out.println ("加密前的字符串:"+new string (input ));
} cipher c1 = cipher.getinstance (algorithm );
c1.init (cipher.encrypt_mode ,deskey );
byte [] cipherbyte =c1.dofinal (input );
if (debug ) system.out.println ("加密后的二进串:"+byte2hex (cipherbyte ));
return cipherbyte ;
}
public static byte [] decryptdata (byte [] input ,byte [] key ) throws exception
{
secretkey deskey = new javax.crypto.spec.secretkeyspec (key ,algorithm );
if (debug ) system.out.println ("解密前的信息:"+byte2hex (input ));
cipher c1 = cipher.getinstance (algorithm );
c1.init (cipher.decrypt_mode ,deskey );
byte [] clearbyte =c1.dofinal (input );
if (debug )
{
system.out.println ("解密后的二进串:"+byte2hex (clearbyte ));
system.out.println ("解密后的字符串:"+(new string (clearbyte )));
} return clearbyte ;
}
public static string byte2hex (byte [] b )
{
string hs ="";
string stmp ="";
for (int n =0 ;n
{
stmp =(java.lang.integer.tohexstring (b [n ] & 0xff ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n
} return hs.touppercase ();
}
public static byte[] hexstringtobyte(string hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.tochararray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (tobyte(achar[pos]) << 4 | tobyte(achar[pos + 1]));
}
return result;
}
private static byte tobyte(char c) {
byte b = (byte) "0123456789abcdef".indexof(c);
return b;
}
public static final string bytestohexstring(byte[] barray) {
stringbuffer sb = new stringbuffer(barray.length);
string stemp;
for (int i = 0; i < barray.length; i++) {
stemp = integer.tohexstring(0xff & barray[i]);
if (stemp.length() < 2)
sb.append(0);
sb.append(stemp.touppercase());
}
return sb.tostring();
}
public static byte[] getsecretkey(long deptid) throws exception {
byte[] key=null;
string value=null;
//commdao dao=new commdao();
// list list=dao.getrecordlist("from key k where k.deptid="+deptid);
//if(list.size()>0){
//value=((com.csc.sale.bean.key)list.get(0)).getkey();
value = "cb7a92e3d3491964";
key=hexstringtobyte(value);
//}
if (debug)
system.out.println("密钥:" + value);
return key;
}
public string encryptdata2(string data) {
string en = null;
try {
byte[] key=hexstringtobyte(this.key);
en = bytestohexstring(encryptdata(data.getbytes(),key));
} catch (exception e) {
e.printstacktrace();
}
return en;
}
public string decryptdata2(string data) {
string de = null;
try {
byte[] key=hexstringtobyte(this.key);
de = new string(decryptdata(hexstringtobyte(data),key));
} catch (exception e) {
e.printstacktrace();
}
return de;
}
}
加密使用:
byte[] key=eryptogram.getsecretkey(deptid); //获得钥匙(字节数组)
byte[] tmp=eryptogram.encryptdata(password.getbytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=eryptogram.bytestohexstring(tmp); //将字节数组转化为字符串,获得加密后的字符串密码
解密与之差不多