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

1、什么是AES

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

2、加密解密方法

 package com.mao;

 import java.security.SecureRandom;

 import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import org.apache.commons.lang.StringUtils; /**
*
* 项目名称:---
* 模块名称:常用工具
* 功能描述:安全工具类
* 创建人: mao2080@sina.com
* 创建时间:2017年4月27日 下午5:33:24
* 修改人: mao2080@sina.com
* 修改时间:2017年4月27日 下午5:33:24
*/
public class SecurityUtil { /**AES算法key size*/
private static final int AES_KEY_SIZE = 128; /**AES算法*/
private static final String ALGORITHM = "AES"; /**AES-charset*/
private static final String AES_CHARSET = "UTF-8"; /**
*
* 描述:将字符串通过AES算法加密
* @author mao2080@sina.com
* @created 2017年4月7日 上午11:00:51
* @since
* @param content 需要加密的内容
* @param strkey 密钥
* @return 加密后字符串
* @throws Exception
*/
public static String EncryptByAES(String content, String strkey) throws Exception {
if (StringUtils.isBlank(strkey)) {
throw new Exception();
}
try {
KeyGenerator kgen = KeyGenerator.getInstance(SecurityUtil.ALGORITHM);
kgen.init(SecurityUtil.AES_KEY_SIZE, new SecureRandom(strkey.getBytes(SecurityUtil.AES_CHARSET)));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, SecurityUtil.ALGORITHM);
Cipher cipher = Cipher.getInstance(SecurityUtil.ALGORITHM);// 创建密码器
byte[] byteContent = content.getBytes(SecurityUtil.AES_CHARSET);
cipher.init(Cipher.ENCRYPT_MODE, key);//初始化
byte[] result = cipher.doFinal(byteContent);//加密
return SecurityUtil.parseByte2HexStr(result);
} catch (Exception e) {
throw new Exception();
}
} /**
*
* 描述:将字符串通过AES算法解密
* @author mao2080@sina.com
* @created 2017年4月7日 上午11:18:51
* @since
* @param content 需要解密的内容
* @param key 密钥
* @return
* @throws Exception
*/
public static String DecryptAES(String content, String strkey) throws Exception {
if (StringUtils.isBlank(strkey)) {
throw new Exception();
}
try {
byte[] decryptFrom = SecurityUtil.parseHexStr2Byte(content);
KeyGenerator kgen = KeyGenerator.getInstance(SecurityUtil.ALGORITHM);
kgen.init(SecurityUtil.AES_KEY_SIZE, new SecureRandom(strkey.getBytes(SecurityUtil.AES_CHARSET)));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, SecurityUtil.ALGORITHM);
Cipher cipher = Cipher.getInstance(SecurityUtil.ALGORITHM);//创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);//初始化
return new String(cipher.doFinal(decryptFrom));
} catch (Exception e) {
throw new Exception();
}
} /**
*
* 描述:将二进制转换成16进制字符串
* @author mao2080@sina.com
* @created 2017年4月7日 上午10:55:48
* @since
* @param buf 二进制数组
* @return
* @throws Exception
*/
public static String parseByte2HexStr(byte buf[]) throws Exception {
if(buf == null){
throw new Exception();
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
} /**
*
* 描述:将16进制转换为二进制
* @author mao2080@sina.com
* @created 2017年4月7日 下午2:16:42
* @since
* @param hexStr 10进制字符串
* @return
* @throws Exception
*/
public static byte[] parseHexStr2Byte(String hexStr) throws Exception {
if (hexStr.length() < 1){
throw new Exception();
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int hig = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (hig * 16 + low);
}
return result;
} public static void main(String[] args) throws Exception{
String aes = EncryptByAES("hello mao", "12345678");
System.out.println("AES加密:"+aes);
System.out.println("AES解密:"+DecryptAES(aes, "12345678"));
} }

3、运行结果

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

4、参考博客

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

最新文章

  1. iOS 中自定义 cell,点击cell的时候文字不出现的原因
  2. UVa 445 - Marvelous Mazes
  3. Java文件读写
  4. java线程图
  5. json和jsonp的使用格式
  6. mybatis的缓存机制
  7. OBJECT-ORIENTED
  8. Java作用域
  9. UE4从零搭建CF游戏关卡(蓝图篇)
  10. 标准模型和IE模型的区别:
  11. Vue.js搭建路由报错 router.map is not a function,Cannot read property ‘component’ of undefined
  12. python3下获取全局坐标
  13. Vue(四)事件和属性
  14. C# ReaderWriterLockSlim 实现
  15. cocos2dx 3.x(打开网页webView)
  16. generator插件配置方式使用
  17. Docker创建容器
  18. 读书笔记--C陷阱与缺陷(三)
  19. 【RF库测试】DateTime库
  20. 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)

热门文章

  1. O015、OpenStack 架构
  2. java实现spark常用算子之count
  3. vue iview面包屑
  4. MySQL之数据库优化
  5. 利用mybatis-generator自动生成代码(转)
  6. xml_dom解析之二
  7. MixNet学习笔记
  8. oracle表的列合并(group by)和行合并(union all)
  9. UVA - 1649 Binomial coefficients (组合数+二分)
  10. 我说CMMI之五:CMMI 4个等级的区别--转载