AES 对称加密

  1 package com.skynet.rimp.common.utils.string;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.security.InvalidKeyException;
  5 import java.security.NoSuchAlgorithmException;
  6 import java.security.SecureRandom;
  7 
  8 import javax.crypto.BadPaddingException;
  9 import javax.crypto.Cipher;
 10 import javax.crypto.IllegalBlockSizeException;
 11 import javax.crypto.KeyGenerator;
 12 import javax.crypto.NoSuchPaddingException;
 13 import javax.crypto.SecretKey;
 14 import javax.crypto.spec.SecretKeySpec;
 15 
 16 public class AESUtil {
 17 
 18     private static final String key = "yunyou17lw";
 19 
 20     /**
 21      * 加密
 22      * @param content 需要加密的内容
 23      * @param password  加密密码
 24      * @return
 25      */
 26     private static byte[] encryptByte(String content, String password) {
 27         try {
 28             KeyGenerator kgen = KeyGenerator.getInstance("AES");
 29             kgen.init(128, new SecureRandom(password.getBytes()));
 30             SecretKey secretKey = kgen.generateKey();
 31             byte[] enCodeFormat = secretKey.getEncoded();
 32             SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
 33             Cipher cipher = Cipher.getInstance("AES");// 创建密码器
 34             byte[] byteContent = content.getBytes("utf-8");
 35             cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
 36             byte[] result = cipher.doFinal(byteContent);
 37             return result; // 加密
 38         } catch (NoSuchAlgorithmException e) {
 39             e.printStackTrace();
 40         } catch (NoSuchPaddingException e) {
 41             e.printStackTrace();
 42         } catch (InvalidKeyException e) {
 43             e.printStackTrace();
 44         } catch (UnsupportedEncodingException e) {
 45             e.printStackTrace();
 46         } catch (IllegalBlockSizeException e) {
 47             e.printStackTrace();
 48         } catch (BadPaddingException e) {
 49             e.printStackTrace();
 50         }
 51         return null;
 52     }
 53 
 54     /**
 55      * 解密
 56      * @param content  待解密内容
 57      * @param password 解密密钥
 58      * @return
 59      */
 60     private static byte[] decryptByte(byte[] content, String password) {
 61         try {
 62             KeyGenerator kgen = KeyGenerator.getInstance("AES");
 63             kgen.init(128, new SecureRandom(password.getBytes()));
 64             SecretKey secretKey = kgen.generateKey();
 65             byte[] enCodeFormat = secretKey.getEncoded();
 66             SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
 67             Cipher cipher = Cipher.getInstance("AES");// 创建密码器
 68             cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
 69             byte[] result = cipher.doFinal(content);
 70             return result; // 加密
 71         } catch (NoSuchAlgorithmException e) {
 72             e.printStackTrace();
 73         } catch (NoSuchPaddingException e) {
 74             e.printStackTrace();
 75         } catch (InvalidKeyException e) {
 76             e.printStackTrace();
 77         } catch (IllegalBlockSizeException e) {
 78             e.printStackTrace();
 79         } catch (BadPaddingException e) {
 80             e.printStackTrace();
 81         }
 82         return null;
 83     }
 84 
 85     /**
 86      * AES加密
 87      * @param str
 88      * @return
 89      */
 90     public static String encrypt(String str) {
 91         return BinaryHex.binary2Hex(encryptByte(str, key));
 92     }
 93 
 94     /**
 95      * AES解密
 96      * @param str
 97      * @return
 98      */
 99     public static String decrypt(String str) throws UnsupportedEncodingException {
100         // 16进制转2进制
101         byte[] decryptFrom = BinaryHex.hex2Byte(str);
102         // 根据byte进行解码
103         byte[] decryptResult = decryptByte(decryptFrom, key);
104         return new String(decryptResult,"UTF-8");
105     }
106 
107     public static void main(String[] args) throws UnsupportedEncodingException {
108         String str = "发多少房间打扫看来减肥鲁大师 <br>///fdsfds房贷首付斯蒂芬是";
109         // 加密
110         System.out.println("加密前:" + str);
111         String s = AESUtil.encrypt(str);
112         System.out.println("加密后:" + s);
113 
114         System.out.println("解密后:" + AESUtil.decrypt(s));
115     }
116 }
 1 package com.skynet.rimp.common.utils.string;
 2 
 3 /**
 4  * 2进制/16进制互转
 5  *
 6  * @author Jieven
 7  * @date 2014-5-12
 8  */
 9 public class BinaryHex {
10 
11     /**
12      * 2进制转16进制
13      * @param bString 2进制字符串
14      * @return
15      */
16     public static String binary2Hex(String bString) {
17         if (bString == null || bString.equals("") || bString.length() % 8 != 0)
18             return null;
19         StringBuffer tmp = new StringBuffer();
20         int iTmp = 0;
21         for (int i = 0; i < bString.length(); i += 4) {
22             iTmp = 0;
23             for (int j = 0; j < 4; j++) {
24                 iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
25             }
26             tmp.append(Integer.toHexString(iTmp));
27         }
28         return tmp.toString();
29     }
30 
31     /**
32      * 16进制转2进制
33      * @param hexString
34      * @return
35      */
36     public static String hex2Binary(String hexString) {
37         if (hexString == null || hexString.length() % 2 != 0)
38             return null;
39         String bString = "", tmp;
40         for (int i = 0; i < hexString.length(); i++) {
41             tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
42             bString += tmp.substring(tmp.length() - 4);
43         }
44         return bString;
45     }
46 
47     /**
48      * 将二进制转换成16进制
49      * @param buf
50      * @return
51      */
52     public static String binary2Hex(byte buf[]) {
53         StringBuffer sb = new StringBuffer();
54         for (int i = 0; i < buf.length; i++) {
55             String hex = Integer.toHexString(buf[i] & 0xFF);
56             if (hex.length() == 1) {
57                 hex = '0' + hex;
58             }
59             sb.append(hex.toUpperCase());
60         }
61         return sb.toString();
62     }
63 
64     /**
65      * 将16进制转换为二进制
66      * @param hexStr
67      * @return
68      */
69     public static byte[] hex2Byte(String hexStr) {
70         if (hexStr.length() < 1)
71             return null;
72         byte[] result = new byte[hexStr.length() / 2];
73         for (int i = 0; i < hexStr.length() / 2; i++) {
74             int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
75             int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
76             result[i] = (byte) (high * 16 + low);
77         }
78         return result;
79     }
80 
81     public static void main(String[] args) {
82         String bString = "1010101111001101";
83         System.out.println(binary2Hex(bString));
84 
85         String hexString = "ABCD";
86         System.out.println(hex2Binary(hexString));
87     }
88 }

一共两个类,可制作成工具类

posted @ 2019-11-29 16:33  _情书  阅读(217)  评论(0编辑  收藏  举报