在使用Cipher类最加密的时候,如果需要大量进行加解密工作,需要避免Cipher类的大量实例化,本文用MAP记录已经实例化的Cipher,如果已经存在则不需要在实例化、避免内存浪费、导致 Cipher not initialized 错误。
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import sun.misc.BASE64Decoder;
/**
* 解密
*/
public class DESTool {
private static String Algorithm = "DESede";// 加密算法的名称
private static Cipher c;// 密码器
//private static byte[] cipherByte;
private static SecretKey deskey;// 密钥
private static String keyString = "XXXXXXXXXXXXXXXXXXXXXXXXX";// 获得密钥的参数
private static HashMap decryptCipherMap=new HashMap();
// Logger log =Logger.getLogger(DESTool.class);
// 对base64编码的string解码成byte数组
public byte[] deBase64(String parm) throws IOException {
BASE64Decoder dec = new BASE64Decoder();
byte[] dnParm = dec.decodeBuffer(parm);
//System.out.println(dnParm.length);
//System.out.println(dnParm);
return dnParm;
}
// 把密钥参数转为byte数组
public byte[] dBase64(String parm) throws IOException {
BASE64Decoder dec = new BASE64Decoder();
byte[] dnParm = dec.decodeBuffer(parm);
return dnParm;
}
/**
* 对 Byte 数组进行解密
*
* @param buff
* 要解密的数据
* @return 返回加密后的 String
*/
public static String createDecryptor(byte[] buff)
throws NoSuchPaddingException, NoSuchAlgorithmException,
UnsupportedEncodingException {
byte[] cipherByte = buff ;
try {
cipherByte = c.doFinal(buff);
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
return null;
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
return null;
}
return (new String(cipherByte, "UTF-8"));
}
public void init(String key) throws IOException, InvalidKeyException,
InvalidKeySpecException {
byte[] dKey = dBase64(key);
try {
if(decryptCipherMap.get("keyString")==null){//判断是否已经存在实例,如果存在不在实例化。
deskey = new javax.crypto.spec.SecretKeySpec(dKey, Algorithm);
c = Cipher.getInstance(Algorithm);
c.init(Cipher.DECRYPT_MODE, deskey);// 初始化密码器,用密钥deskey,进入解密模式
decryptCipherMap.put("keyString", c);
}
} catch (NoSuchPaddingException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
/**
*
* @param strEncryption 加密字符串
* @return 返回解密后的字符串
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws IOException
*/
public static String decryptScore(String strEncryption)throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeySpecException, InvalidKeyException, IOException{
DESTool des = new DESTool();
des.init(keyString);
byte[] dBy = des.deBase64(strEncryption);
return des.createDecryptor(dBy);
}
public static void main(String args[]) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeySpecException, InvalidKeyException, IOException {
DESTool des = new DESTool();
des.init(keyString);
//byte[] dBy = des.deBase64("1ZVasdJJco1qccDnnfQfb8QeaARxhkR6");
byte[] dBy = des.deBase64("JdTkEPNsw8E=");
String dStr = des.createDecryptor(dBy);
System.out.println("解:" + decryptScore("JdTkEPNsw8E="));
}
}