数据加密之AES

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

1、什么是AES

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。特性:对称加密。

2、加密解密方法

  1 package com.mao;
  2 
  3 import java.security.SecureRandom;
  4 
  5 import javax.crypto.Cipher;
  6 import javax.crypto.KeyGenerator;
  7 import javax.crypto.SecretKey;
  8 import javax.crypto.spec.SecretKeySpec;
  9 
 10 import org.apache.commons.lang.StringUtils;
 11 
 12 /**
 13  * 
 14  * 项目名称:---
 15  * 模块名称:常用工具
 16  * 功能描述:安全工具类
 17  * 创建人: mao2080@sina.com
 18  * 创建时间:2017年4月27日 下午5:33:24
 19  * 修改人: mao2080@sina.com
 20  * 修改时间:2017年4月27日 下午5:33:24
 21  */
 22 public class SecurityUtil {
 23     
 24     /**AES算法key size*/
 25     private static final int AES_KEY_SIZE = 128;
 26     
 27     /**AES算法*/
 28     private static final String ALGORITHM = "AES";
 29     
 30     /**AES-charset*/
 31     private static final String AES_CHARSET = "UTF-8";
 32     
 33     /**
 34      * 
 35      * 描述:将字符串通过AES算法加密
 36      * @author mao2080@sina.com
 37      * @created 2017年4月7日 上午11:00:51
 38      * @since 
 39      * @param content 需要加密的内容
 40      * @param strkey 密钥
 41      * @return 加密后字符串
 42      * @throws Exception
 43      */
 44     public static String EncryptByAES(String content, String strkey) throws Exception {
 45         if (StringUtils.isBlank(strkey)) {
 46             throw new Exception();
 47         }
 48         try {
 49             KeyGenerator kgen = KeyGenerator.getInstance(SecurityUtil.ALGORITHM);
 50             kgen.init(SecurityUtil.AES_KEY_SIZE, new SecureRandom(strkey.getBytes(SecurityUtil.AES_CHARSET)));
 51             SecretKey secretKey = kgen.generateKey();
 52             byte[] enCodeFormat = secretKey.getEncoded();
 53             SecretKeySpec key = new SecretKeySpec(enCodeFormat, SecurityUtil.ALGORITHM);
 54             Cipher cipher = Cipher.getInstance(SecurityUtil.ALGORITHM);// 创建密码器
 55             byte[] byteContent = content.getBytes(SecurityUtil.AES_CHARSET);
 56             cipher.init(Cipher.ENCRYPT_MODE, key);//初始化
 57             byte[] result = cipher.doFinal(byteContent);//加密
 58             return SecurityUtil.parseByte2HexStr(result);
 59         } catch (Exception e) {
 60             throw new Exception();
 61         } 
 62     }
 63  
 64     /**
 65      * 
 66      * 描述:将字符串通过AES算法解密
 67      * @author mao2080@sina.com
 68      * @created 2017年4月7日 上午11:18:51
 69      * @since 
 70      * @param content 需要解密的内容
 71      * @param key 密钥
 72      * @return
 73      * @throws Exception
 74      */
 75     public static String DecryptAES(String content, String strkey) throws Exception {
 76         if (StringUtils.isBlank(strkey)) {
 77             throw new Exception();
 78         }
 79         try {
 80             byte[] decryptFrom = SecurityUtil.parseHexStr2Byte(content);
 81             KeyGenerator kgen = KeyGenerator.getInstance(SecurityUtil.ALGORITHM);
 82             kgen.init(SecurityUtil.AES_KEY_SIZE, new SecureRandom(strkey.getBytes(SecurityUtil.AES_CHARSET)));
 83             SecretKey secretKey = kgen.generateKey();
 84             byte[] enCodeFormat = secretKey.getEncoded();
 85             SecretKeySpec key = new SecretKeySpec(enCodeFormat, SecurityUtil.ALGORITHM);
 86             Cipher cipher = Cipher.getInstance(SecurityUtil.ALGORITHM);//创建密码器
 87             cipher.init(Cipher.DECRYPT_MODE, key);//初始化
 88             return new String(cipher.doFinal(decryptFrom));
 89         } catch (Exception e) {
 90             throw new Exception();
 91         }
 92     }
 93     
 94     /**
 95       *   
 96       * 描述:将二进制转换成16进制字符串
 97       * @author mao2080@sina.com
 98       * @created 2017年4月7日 上午10:55:48
 99       * @since 
100       * @param buf 二进制数组
101       * @return
102       * @throws Exception
103       */
104    public static String parseByte2HexStr(byte buf[]) throws Exception {
105        if(buf == null){
106            throw new Exception();
107        }
108        StringBuffer sb = new StringBuffer();
109        for (int i = 0; i < buf.length; i++) {
110            String hex = Integer.toHexString(buf[i] & 0xFF);
111            if (hex.length() == 1) {
112                sb.append("0");
113            }
114            sb.append(hex);
115        }
116        return sb.toString();
117    }
118    
119    /**
120     * 
121     * 描述:将16进制转换为二进制
122     * @author mao2080@sina.com
123     * @created 2017年4月7日 下午2:16:42
124     * @since 
125     * @param hexStr 10进制字符串
126     * @return
127     * @throws Exception 
128     */
129     public static byte[] parseHexStr2Byte(String hexStr) throws Exception {
130         if (hexStr.length() < 1){
131             throw new Exception();
132         }
133         byte[] result = new byte[hexStr.length() / 2];
134         for (int i = 0; i < hexStr.length() / 2; i++) {
135             int hig = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
136             int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
137             result[i] = (byte) (hig * 16 + low);
138         }
139         return result;
140     }
141     
142 
143     public static void main(String[] args) throws Exception{
144         String aes = EncryptByAES("hello mao", "12345678");
145         System.out.println("AES加密:"+aes);
146         System.out.println("AES解密:"+DecryptAES(aes, "12345678"));
147     }
148     
149 }

3、运行结果

AES加密:775edd66a29a6849a98812199241f230
AES解密:hello mao

4、参考博客

http://www.cnblogs.com/vmax-tam/p/4624032.html

原文地址:https://www.cnblogs.com/mao2080/p/6775347.html