大批量字符加解密时报 Cipher not initialized

在使用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="));
}


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值