3DES加密工具类
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
/**
* 3DES加密解密和BASE64编码解码混合使用
*/
public class ThreeDESUtil {
private static Logger logger = Logger
.getLogger(ThreeDESUtil.class);
private static ThreeDESUtil threeDESUtil = null;
private ThreeDESUtil() {
init();
}
public synchronized static ThreeDESUtil getInstance() {
if (threeDESUtil == null) threeDESUtil = new ThreeDESUtil();
return threeDESUtil;
}
//key必须在16位/24位/32位,下面使用的是24位,不足24位则后面余数补0填充满24位
private static final String key="f4605a21fc8e4077a21397fd";
//定义加密算法,有DES、DESede(即3DES)、Blowfish
private static final String Algorithm = "DESede/ECB/PKCS5Padding";
//如果使用DESede,默认则使用"DESede/ECB/PKCS5Padding" ECB:电子密码本形式加密,
// BCB:密码块链接加密
// private static final String DES = "DESede/ECB/PKCS5Padding";
// private static final String DES = "DESede/CBC/PKCS5Padding";
// private static final String DES = "DESede/ECB/NoPadding";
private static Cipher decryptCipher;
private static Cipher encryptCipher;
private void init(){
try{
SecretKey deskey = new SecretKeySpec(build3DesKey(key), "DESede"); //生成密钥
encryptCipher = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类
encryptCipher.init(Cipher.ENCRYPT_MODE, deskey);
decryptCipher = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类
decryptCipher.init(Cipher.DECRYPT_MODE,deskey);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 3DES加密方法
* @param src 源数据的字节数组
* @return
*/
public byte[] encryptMode(byte[] src,String key) {
try {
return encryptCipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//3DES解密
public byte[] decrypt(byte[] data,String key){
try {
return decryptCipher.doFinal(data);
} catch (Exception ex) {
//解密失败,打日志
ex.printStackTrace();
}
return null;
}
/**
* 3DES加密Base64编码处理方法
* @param bytes 字符串转的数组
* @return 经过3DES+Base64加密后的字符串
*/
public String encode3DesBase64(byte[] bytes){
byte [] base = encryptMode(bytes,key);
return Base64.encodeBase64String(base);
}
public String encode3DesBase64(String src){
try{
return encode3DesBase64(src.getBytes("utf-8"));
}catch (Exception e){
logger.error(e);
}
return null;
}
/**
* 将3DES+Base64加密后的byte数组进行解密
* @param bytes 先3DES+Base64加密后的 byte数组
* @return 未加密前的字符串
*/
public String decode3DesBase64(byte[] bytes){
byte[] b = null;
String result = null;
try {
b = decrypt(Base64.decodeBase64(bytes),key);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String decode3DesBase64(String src){
try {
return decode3DesBase64(src.getBytes("utf-8"));
}catch (Exception e){
logger.error(e);
}
return null;
}
//构建3DES密钥
public byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0
byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组
/*
* 执行数组拷贝
* System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
*/
if(key.length > temp.length){
//如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, temp.length);
}else{
//如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, key.length);
}
return key;
}
}
注意
SecretKey deskey = new SecretKeySpec(build3DesKey(key), "DESede");
生成密钥的代码,第二个参数为DESede即为3DES加密