import
java.security.SecureRandom;
import
javax.crypto.Cipher;
import
javax.crypto.SecretKey;
import
javax.crypto.SecretKeyFactory;
import
javax.crypto.spec.DESKeySpec;
import
org.apache.commons.codec.binary.Base64;
import
org.apache.commons.codec.binary.StringUtils;
/**
*
客户端与服务器通讯时对消息体加密和解密的工具类
*
@author LIUTAO
*
*/
public
class
DESBase64Util {
/**
*
密码长度必须是8的倍数
*/
public
final
static
String KEY =
"RS35836780805258"
;
/**
*
加密方式
*/
public
final
static
String DES =
"DES"
;
/**
*
默认编码
*/
public
final
static
String ENCODING =
"UTF-8"
;
/**
*
DES加密
*
*
@param src
*
@param key
*
@return
*
@throws Exception
*/
public
static
byte
[]
encrypt(
byte
[]
src,
byte
[]
key)
throws
Exception {
SecureRandom
sr =
new
SecureRandom();
DESKeySpec
dks =
new
DESKeySpec(key);
SecretKeyFactory
keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey
securekey = keyFactory.generateSecret(dks);
Cipher
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE,
securekey, sr);
return
cipher.doFinal(src);
}
/**
*
DES解密
*
*
@param src
*
@param key
*
@return
*
@throws Exception
*/
public
static
byte
[]
decrypt(
byte
[]
src,
byte
[]
key)
throws
Exception {
SecureRandom
sr =
new
SecureRandom();
DESKeySpec
dks =
new
DESKeySpec(key);
SecretKeyFactory
keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey
securekey = keyFactory.generateSecret(dks);
Cipher
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE,
securekey, sr);
return
cipher.doFinal(src);
}
/**
*
BASE64编码
*
*
@param info
*
@return
*/
public
static
String base64Encode(
byte
[]
info) {
return
Base64.encodeBase64String(info);
}
/**
*
BASE64解码
*
*
*
@param info
*
@return
*/
public
static
byte
[]
base64Decode(String info) {
return
Base64.decodeBase64(info);
}
/**
*
先对消息体进行DES编码再进行BASE64编码
*
@param info
*
@return
*/
public
static
String encodeInfo(String info) {
try
{
byte
[]
temp = encrypt(info.getBytes(ENCODING),
KEY.getBytes(ENCODING));
return
base64Encode(temp);
}
catch
(Exception e) {
e.printStackTrace();
}
return
""
;
}
/**
*
先对消息体进行BASE64解码再进行DES解码
*
@param info
*
@return
*/
public
static
String decodeInfo(String info) {
byte
[]
temp = base64Decode(info);
try
{
byte
[]
buf = decrypt(temp,
KEY.getBytes(ENCODING));
return
StringUtils.newStringUtf8(buf);
}
catch
(Exception e) {
e.printStackTrace();
}
return
""
;
}
public
static
void
main(String[] args) {
String
info =
"我是中国人,我爱祖国这是测试数据无所设置无所设置"
;
String
encodeInfo = encodeInfo(info);
System.out.println(encodeInfo);
String
decodeInfo = decodeInfo(encodeInfo);
System.out.println(decodeInfo);
}
}