intj=i<<1;
Strings=content.substring(j,j+2);
result[i]=Integer.valueOf(s,16).byteValue();
}
returnresult;
}
/**
*将字节数组转换为base64编码字符串
*@parambuffer
*@return
*/
publicstaticStringbytesToBase64(byte[]buffer){
//BASE64Encoderen=newBASE64Encoder();
returnBase64.encode(buffer);
// returnencoder.encode(buffer);
}
/**
*将base64编码的字符串解码为字节数组
*@paramvalue
*@return
*@throwsIOException
*/
publicstaticbyte[]base64ToBytes(Stringvalue)throwsIOException{
//returnBase64.decodeToByteArray(value);
// System.out.println(decoder.decodeBuffer(value));
// returndecoder.decodeBuffer(value);
returnBase64.decode(value);
}
/**
*加密给定的字符串
*@paramvalue
*@return加密后的base64字符串
*/
publicstaticStringencryptString(Stringvalue){
returnencryptString(value,DEFAULT_KEY);
}
/**
*根据给定的密钥加密字符串
*@paramvalue待加密的字符串
*@paramkey以BASE64形式存在的密钥
*@return加密后的base64字符串
*@throwsIOException
*/
publicstaticStringencryptString(Stringvalue,Stringkey)throwsIOException{
returnencryptString(value,base64ToBytes(key));
}
/**
*根据给定的密钥加密字符串
*@paramvalue待加密的字符串
*@paramkey字节数组形式的密钥
*@return加密后的base64字符串
*/
publicstaticStringencryptString(Stringvalue,byte[]key){
try{
byte[]data=value.getBytes(VALUE_ENCODING);
data=CryptUtils.encryptData(data,key);
returnbytesToBase64(data);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnnull;
}
}
/**
*解密字符串
*@paramvaluebase64形式存在的密文
*@return明文
*/
publicstaticStringdecryptString(Stringvalue){
returndecryptString(value,DEFAULT_KEY);
}
/**
*解密字符串
*@paramvaluebase64形式存在的密文
*@paramkeybase64形式存在的密钥
*@return明文
*@throwsIOException
*/
publicstaticStringdecryptString(Stringvalue,Stringkey)throwsIOException{
Strings=decryptString(value,base64ToBytes(key));
returns;
}
/**
*解密字符串
*@paramvaluebase64形式存在的密文
*@paramkey字节数据形式存在的密钥
*@return明文
*/
publicstaticStringdecryptString(Stringvalue,byte[]key){
try{
byte[]data=base64ToBytes(value);
data=CryptUtils.decryptData(data,key);
returnnewString(data,VALUE_ENCODING);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
}
packagecom.gdie.lab.crypt;
importjava.io.IOException;
importjavax.crypto.Cipher;
importjavax.crypto.KeyGenerator;
importjavax.crypto.SecretKey;
importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64;
publicclassCryptUtils{
privatestaticStringAlgorithm="DES";
privatestaticbyte[]DEFAULT_KEY=newbyte[]{-53,122,-42,-88,-110,-123,-60,-74};
privatestaticStringVALUE_ENCODING="UTF-8";
/**
*生成密钥
*
*@returnbyte[]返回生成的密钥
*@throwsexception
* 扔出异常.
*/
publicstaticbyte[]getSecretKey()throwsException{
KeyGeneratorkeygen=KeyGenerator.getInstance(Algorithm);
SecretKeydeskey=keygen.generateKey();
//if(debug)System.out.println("生成密钥:
"+byte2hex(deskey.getEncoded
//()));
returndeskey.getEncoded();
}
/**
*将指定的数据根据提供的密钥进行加密
*
*@paraminput
* 需要加密的数据
*@paramkey
* 密钥
*@returnbyte[]加密后的数据
*@throwsException
*/
publicstaticbyte[]encryptData(byte[]input,byte[]key)throwsException{
SecretKeydeskey=newjavax.crypto.spec.SecretKeySpec(key,Algorithm);
//if(debug)
//{
//System.out.println("加密前的二进串:
"+byte2hex(input));
//System.out.println("加密前的字符串:
"+newString(input));
//
//}
Cipherc1=Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[]cipherByte=c1.doFinal(input);
//if(debug)System.out.println("加密后的二进串:
"+byte2hex(cipherByte));
returncipherByte;
}
publicstaticbyte[]encryptData(byte[]input)throwsException{
returnencryptData(input,DEFAULT_KEY);
}
/**
*将给定的已加密的数据通过指定的密钥进行解密
*
*@paraminput
* 待解密的数据
*@paramkey
* 密钥
*@returnbyte[]解密后的数据
*@throwsException
*/
publicstaticbyte[]decryptData(byte[]input,byte[]key)throwsException{
SecretKeydeskey=newjavax.crypto.spec.SecretKeySpec(key,Algorithm);
//if(debug)System.out.println("解密前的信息:
"+byte2hex(input));
Cipherc1=Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[]clearByte=c1.doFinal(input);
//if(debug)
//{
//System.out.println("解密后的二进串:
"+byte2hex(clearByte));
//System.out.println("解密后的字符串:
"+(newString(clearByte)));
//
//}
returnclearByte;
}
publicstaticbyte[]decryptData(byte[]input)throwsException{
returndecryptData(input,DEFAULT_KEY);
}
/**
*字节码转换成16进制字符串
*
*@parambyte[]b输入要转换的字节码
*@returnString返回转换后的16进制字符串
*/
publicstaticStringbyte2hex(byte[]bytes){
StringBuilderhs=newStringBuilder();
for(byteb:
bytes)
hs.append(String.format("%1$02X",b));
returnhs.toString();
}
publicstaticbyte[]hex2byte(Stringcontent){
intl=content.length()>>1;
byte[]result=newbyte[l];
for(inti=0;i intj=i<<1;
Strings=content.substring(j,j+2);
result[i]=Integer.valueOf(s,16).byteValue();
}
returnresult;
}
/**
*将字节数组转换为base64编码字符串
*@parambuffer
*@return
*/
publicstaticStringbytesToBase64(byte[]buffer){
//BASE64Encoderen=newBASE64Encoder();
returnBase64.encode(buffer);
// returnencoder.encode(buffer);
}
/**
*将base64编码的字符串解码为字节数组
*@paramvalue
*@return
*@throwsIOException
*/
publicstaticbyte[]base64ToByte