DBMNG数据库管理与应用

抓住自己最有兴趣的东西,由浅入深,循序渐进地学……
当前位置:首页 > 经验分享 > Java开发

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

在使用Cipher类最加密的时候,如果需要大量进行加解密工作,需要避免Cipher类的大量实例化,本文用MAP记录已经实例化的Cipher,如果已经存在则不需要在实例化、避免内存浪费、导致 Cipher not initialized    错误。 


Java代码  
  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.security.InvalidKeyException;  
  4. import java.security.NoSuchAlgorithmException;  
  5. import java.security.spec.InvalidKeySpecException;  
  6. import java.util.HashMap;  
  7.   
  8. import javax.crypto.Cipher;  
  9. import javax.crypto.NoSuchPaddingException;  
  10. import javax.crypto.SecretKey;  
  11. import sun.misc.BASE64Decoder;  
  12.   
  13. /** 
  14.  * 解密 
  15.  */  
  16. public class DESTool {  
  17.     private static String Algorithm = "DESede";// 加密算法的名称  
  18.     private static Cipher c;// 密码器  
  19.     //private static byte[] cipherByte;  
  20.     private static SecretKey deskey;// 密钥  
  21.     private static String keyString = "XXXXXXXXXXXXXXXXXXXXXXXXX";// 获得密钥的参数  
  22.     private static HashMap decryptCipherMap=new HashMap();   
  23.    // Logger  log =Logger.getLogger(DESTool.class);  
  24.     // 对base64编码的string解码成byte数组  
  25.     public byte[] deBase64(String parm) throws IOException {  
  26.         BASE64Decoder dec = new BASE64Decoder();  
  27.         byte[] dnParm = dec.decodeBuffer(parm);  
  28.         //System.out.println(dnParm.length);  
  29.         //System.out.println(dnParm);  
  30.         return dnParm;  
  31.     }  
  32.   
  33.     // 把密钥参数转为byte数组  
  34.     public byte[] dBase64(String parm) throws IOException {  
  35.         BASE64Decoder dec = new BASE64Decoder();  
  36.         byte[] dnParm = dec.decodeBuffer(parm);  
  37.         return dnParm;  
  38.     }  
  39.   
  40.     /** 
  41.      * 对 Byte 数组进行解密 
  42.      *  
  43.      * @param buff 
  44.      *            要解密的数据 
  45.      * @return 返回加密后的 String 
  46.      */  
  47.     public static String createDecryptor(byte[] buff)  
  48.             throws NoSuchPaddingException, NoSuchAlgorithmException,  
  49.             UnsupportedEncodingException {  
  50.          byte[] cipherByte = buff ;  
  51.         try {  
  52.           
  53.               cipherByte = c.doFinal(buff);  
  54.         } catch (javax.crypto.BadPaddingException ex) {  
  55.               
  56.             ex.printStackTrace();  
  57.               
  58.             return null;  
  59.         } catch (javax.crypto.IllegalBlockSizeException ex) {  
  60.             ex.printStackTrace();  
  61.             return null;  
  62.         }  
  63.         return (new String(cipherByte, "UTF-8"));  
  64.     }  
  65.   
  66.     public void init(String key) throws IOException, InvalidKeyException,  
  67.             InvalidKeySpecException {  
  68.         byte[] dKey = dBase64(key);  
  69.         try {  
  70.               
  71.             if(decryptCipherMap.get("keyString")==null){//判断是否已经存在实例,如果存在不在实例化。  
  72.                 deskey = new javax.crypto.spec.SecretKeySpec(dKey, Algorithm);  
  73.                 c = Cipher.getInstance(Algorithm);  
  74.                 c.init(Cipher.DECRYPT_MODE, deskey);// 初始化密码器,用密钥deskey,进入解密模式  
  75.                 decryptCipherMap.put("keyString", c);     
  76.               
  77.             }     
  78.         } catch (NoSuchPaddingException ex) {  
  79.         } catch (NoSuchAlgorithmException ex) {  
  80.         }  
  81.     }  
  82.       
  83.       
  84.     /** 
  85.      *  
  86.      * @param strEncryption 加密字符串 
  87.      * @return 返回解密后的字符串 
  88.      * @throws IOException 
  89.      * @throws NoSuchAlgorithmException 
  90.      * @throws NoSuchPaddingException 
  91.      * @throws InvalidKeySpecException 
  92.      * @throws InvalidKeyException 
  93.      * @throws IOException 
  94.      */  
  95.     public static String decryptScore(String strEncryption)throws IOException,  
  96.     NoSuchAlgorithmException, NoSuchPaddingException,  
  97.     InvalidKeySpecException, InvalidKeyException, IOException{  
  98.         DESTool des = new DESTool();  
  99.         des.init(keyString);  
  100.         byte[] dBy = des.deBase64(strEncryption);  
  101.         return des.createDecryptor(dBy);  
  102.     }  
  103.       
  104.   
  105.     public static void main(String args[]) throws IOException,  
  106.             NoSuchAlgorithmException, NoSuchPaddingException,  
  107.             InvalidKeySpecException, InvalidKeyException, IOException {  
  108.         DESTool des = new DESTool();  
  109.         des.init(keyString);  
  110.         //byte[] dBy = des.deBase64("1ZVasdJJco1qccDnnfQfb8QeaARxhkR6");  
  111.         byte[] dBy = des.deBase64("JdTkEPNsw8E=");  
  112.         String dStr = des.createDecryptor(dBy);  
  113.         System.out.println("解:" + decryptScore("JdTkEPNsw8E="));  
  114.     }  
  115.       
  116.   
  117. }  
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号