AESUtil

 1  package com.jf.utils;
 2 
 3 import javax.crypto.Cipher;
 4 import javax.crypto.spec.SecretKeySpec;
 5 
 6 import org.apache.commons.codec.binary.Base64;
 7 import org.slf4j.Logger;
 8 import org.slf4j.LoggerFactory;
 9 /**
10  * <p>Description: [AES-128-ECB加密模式,key需要为16位]</p>
11  */
12 public class AESUtil {
13     private static final Logger logger = LoggerFactory.getLogger(AESUtil.class);
14     
15     // 加密
16     public static String encrypt(String sSrc, String sKey) throws Exception {
17         if (sKey == null) {
18             logger.info("Key为空null");
19             return null;
20         }    
21         // 判断Key是否为16位
22         if (sKey.length() != 16) {
23             logger.info("Key长度不是16位");
24             return null;
25         }
26         byte[] raw = sKey.getBytes("utf-8");
27         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
28         Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
29         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
30         byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
31         return new String(new Base64().encode(encrypted),"UTF-8");//此处使用BASE64做转码功能,同时能起到2次加密的作用
32     }
33     
34     // 解密
35     public static String decrypt(String sSrc, String sKey) throws Exception {
36         try {
37             // 判断Key是否正确
38             if (sKey == null) {
39                 logger.info("Key为空null");
40                 return null;
41             }
42             // 判断Key是否为16位
43             if (sKey.length() != 16) {
44                 logger.info("Key长度不是16位");
45                 return null;
46             }
47             byte[] raw = sKey.getBytes("utf-8");
48             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
49             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
50             cipher.init(Cipher.DECRYPT_MODE, skeySpec);
51             byte[] encrypted1 = new Base64().decode(sSrc.getBytes("UTF-8"));//先用base64解密
52             try {
53                 byte[] original = cipher.doFinal(encrypted1);
54                 String originalString = new String(original, "utf-8");
55                 return originalString;
56             } catch (Exception e) {
57                 logger.info(e.toString());
58                 return null;
59             }
60         } catch (Exception ex) {
61             logger.info(ex.toString());
62             return null;
63         }
64     }
65     
66     public static void main(String[] args) throws Exception {
67     /*
68     * 此处使用AES-128-ECB加密模式,key需要为16位
69     */
70     String cKey = "XfLxxx7qjxxxx";
71     /*// 需要加密的字串
72     String cSrc = "cp2012c*0.150*215*WD1233768467*1800";
73     logger.info("加密前的字串是:" + cSrc);
74     // 加密
75     String enString = AESUtil.encrypt(cSrc, cKey);
76     logger.info("加密后的字串是:" + enString);
77     // 解密pVAxxxxEp3hxxxx2bO7ExxxhjUN0ODxxxNblJuFz4aNMVxxxK6sojo
78     String DeString = AESUtil.decrypt(enString, cKey);
79     logger.info("解密后的字串是:" + DeString);*/
80     
81     System.out.println(AESUtil.encrypt("cid=421&vcode=xxxx&stime=2017-05-10 08:56:34&etime=2017-05-10 16:17:49", cKey));
82     }
83 }

 

posted @ 2018-09-16 22:35  面向bug编程  阅读(85)  评论(0编辑  收藏  举报