首页 > 代码库 > 加密测试类

加密测试类

  1 /**
  2  * 3des加密:
  3  */
  4 package com.test;
  5 
  6 import java.security.Key;
  7 import java.security.SecureRandom;
  8 import javax.crypto.Cipher;
  9 import javax.crypto.KeyGenerator;
 10 //import sun.misc.BASE64Decoder;
 11 //import sun.misc.BASE64Encoder;
 12 
 13 import sun.misc.BASE64Decoder;
 14 import sun.misc.BASE64Encoder;
 15 
 16 public class DesEncrypt {
 17     /**
 18      * 
 19      * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
 20      * 
 21      * 方法: void getKey(String strKey)从strKey的字条生成一个Key
 22      * 
 23      * String getEncString(String strMing)对strMing进行加密,返回String密文 String
 24      * getDesString(String strMi)对strMin进行解密,返回String明文
 25      * 
 26      * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
 27      * byteD)byte[]型的解密
 28      */
 29 
 30     Key key;
 31 
 32     /**
 33      * 根据参数生成KEY
 34      * 
 35      * @param strKey
 36      */
 37     public void getKey(String strKey) {
 38         try {
 39             KeyGenerator _generator = KeyGenerator.getInstance("DES");
 40             _generator.init(new SecureRandom(strKey.getBytes()));
 41             this.key = _generator.generateKey();
 42             _generator = null;
 43         } catch (Exception e) {
 44             e.printStackTrace();
 45         }
 46     }
 47 
 48     /**
 49      * 加密String明文输入,String密文输出
 50      * 
 51      * @param strMing
 52      * @return
 53      */
 54     public String getEncString(String strMing) {
 55 
 56         byte[] byteMi = null;
 57         byte[] byteMing = null;
 58         String strMi = "";
 59         BASE64Encoder base64en = new BASE64Encoder();
 60         try {
 61             byteMing = strMing.getBytes("UTF8");
 62             byteMi = this.getEncCode(byteMing);
 63             strMi = base64en.encode(byteMi);
 64         } catch (Exception e) {
 65             e.printStackTrace();
 66         } finally {
 67             base64en = null;
 68             byteMing = null;
 69             byteMi = null;
 70         }
 71         return strMi;
 72     }
 73 
 74     /**
 75      * 解密 以String密文输入,String明文输出
 76      * 
 77      * @param strMi
 78      * @return
 79      */
 80     public String getDesString(String strMi) {
 81         BASE64Decoder base64De = new BASE64Decoder();
 82         byte[] byteMing = null;
 83         byte[] byteMi = null;
 84         String strMing = "";
 85         try {
 86             byteMi = base64De.decodeBuffer(strMi);
 87             byteMing = this.getDesCode(byteMi);
 88             strMing = new String(byteMing, "UTF8");
 89         } catch (Exception e) {
 90             e.printStackTrace();
 91         } finally {
 92             base64De = null;
 93             byteMing = null;
 94             byteMi = null;
 95         }
 96         return strMing;
 97     }
 98 
 99     /**
100      * 加密以byte[]明文输入,byte[]密文输出
101      * 
102      * @param byteS
103      * @return
104      */
105     private byte[] getEncCode(byte[] byteS) {
106         byte[] byteFina = null;
107         Cipher cipher;
108         try {
109             cipher = Cipher.getInstance("DES");
110             cipher.init(Cipher.ENCRYPT_MODE, key);
111             byteFina = cipher.doFinal(byteS);
112         } catch (Exception e) {
113             e.printStackTrace();
114         } finally {
115             cipher = null;
116         }
117         return byteFina;
118     }
119 
120     /**
121      * 解密以byte[]密文输入,以byte[]明文输出
122      * 
123      * @param byteD
124      * @return
125      */
126     private byte[] getDesCode(byte[] byteD) {
127         Cipher cipher;
128         byte[] byteFina = null;
129         try {
130             cipher = Cipher.getInstance("DES");
131             cipher.init(Cipher.DECRYPT_MODE, key);
132             byteFina = cipher.doFinal(byteD);
133         } catch (Exception e) {
134             e.printStackTrace();
135         } finally {
136             cipher = null;
137         }
138         return byteFina;
139     }
140 
141     public static void main(String[] args) {
142         System.out.println("des demo");
143         DesEncrypt des = new DesEncrypt();// 实例化一个对像
144         des.getKey("MYKEY");// 生成密匙
145         System.out.println("key=MYKEY");
146         String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文
147         System.out.println("密文=" + strEnc);
148         String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
149         System.out.println("明文=" + strDes);
150     }
151 }