AESUtil.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.zheng.common.util;
  2. import sun.misc.BASE64Decoder;
  3. import sun.misc.BASE64Encoder;
  4. import javax.crypto.*;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.SecureRandom;
  11. /**
  12. * AES加解密工具类
  13. * Created by shuzheng on 2017/2/5.
  14. */
  15. public class AESUtil {
  16. private static final String ENCODE_RULES = "zheng";
  17. /**
  18. * 加密
  19. * 1.构造密钥生成器
  20. * 2.根据ecnodeRules规则初始化密钥生成器
  21. * 3.产生密钥
  22. * 4.创建和初始化密码器
  23. * 5.内容加密
  24. * 6.返回字符串
  25. */
  26. public static String aesEncode(String content) {
  27. try {
  28. //1.构造密钥生成器,指定为AES算法,不区分大小写
  29. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  30. //2.根据ecnodeRules规则初始化密钥生成器
  31. //生成一个128位的随机源,根据传入的字节数组
  32. SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  33. random.setSeed(ENCODE_RULES.getBytes());
  34. keyGenerator.init(128, random);
  35. //3.产生原始对称密钥
  36. SecretKey originalKey = keyGenerator.generateKey();
  37. //4.获得原始对称密钥的字节数组
  38. byte[] raw = originalKey.getEncoded();
  39. //5.根据字节数组生成AES密钥
  40. SecretKey key = new SecretKeySpec(raw, "AES");
  41. //6.根据指定算法AES自成密码器
  42. Cipher cipher = Cipher.getInstance("AES");
  43. //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
  44. cipher.init(Cipher.ENCRYPT_MODE, key);
  45. //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
  46. byte[] byteEncode = content.getBytes("utf-8");
  47. //9.根据密码器的初始化方式--加密:将数据加密
  48. byte[] byteAES = cipher.doFinal(byteEncode);
  49. //10.将加密后的数据转换为字符串
  50. //这里用Base64Encoder中会找不到包
  51. //解决办法:
  52. //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
  53. String aesEncode = new String(new BASE64Encoder().encode(byteAES));
  54. //11.将字符串返回
  55. return aesEncode;
  56. } catch (NoSuchAlgorithmException e) {
  57. e.printStackTrace();
  58. } catch (NoSuchPaddingException e) {
  59. e.printStackTrace();
  60. } catch (InvalidKeyException e) {
  61. e.printStackTrace();
  62. } catch (IllegalBlockSizeException e) {
  63. e.printStackTrace();
  64. } catch (BadPaddingException e) {
  65. e.printStackTrace();
  66. } catch (UnsupportedEncodingException e) {
  67. e.printStackTrace();
  68. }
  69. //如果有错就返加nulll
  70. return null;
  71. }
  72. /**
  73. * 解密
  74. * 解密过程:
  75. * 1.同加密1-4步
  76. * 2.将加密后的字符串反纺成byte[]数组
  77. * 3.将加密内容解密
  78. */
  79. public static String aesDecode(String content) {
  80. try {
  81. //1.构造密钥生成器,指定为AES算法,不区分大小写
  82. KeyGenerator keygen = KeyGenerator.getInstance("AES");
  83. //2.根据ecnodeRules规则初始化密钥生成器
  84. //生成一个128位的随机源,根据传入的字节数组
  85. SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  86. random.setSeed(ENCODE_RULES.getBytes());
  87. keygen.init(128, random);
  88. //3.产生原始对称密钥
  89. SecretKey originalKey = keygen.generateKey();
  90. //4.获得原始对称密钥的字节数组
  91. byte[] raw = originalKey.getEncoded();
  92. //5.根据字节数组生成AES密钥
  93. SecretKey key = new SecretKeySpec(raw, "AES");
  94. //6.根据指定算法AES自成密码器
  95. Cipher cipher = Cipher.getInstance("AES");
  96. //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
  97. cipher.init(Cipher.DECRYPT_MODE, key);
  98. //8.将加密并编码后的内容解码成字节数组
  99. byte[] byteContent = new BASE64Decoder().decodeBuffer(content);
  100. /*
  101. * 解密
  102. */
  103. byte[] byteDecode = cipher.doFinal(byteContent);
  104. String aesDecode = new String(byteDecode, "utf-8");
  105. return aesDecode;
  106. } catch (NoSuchAlgorithmException e) {
  107. e.printStackTrace();
  108. } catch (NoSuchPaddingException e) {
  109. e.printStackTrace();
  110. } catch (InvalidKeyException e) {
  111. e.printStackTrace();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. } catch (IllegalBlockSizeException e) {
  115. throw new RuntimeException("兄弟,配置文件中的密码需要使用AES加密,请使用com.zheng.common.util.AESUtil工具类修改这些值!");
  116. //e.printStackTrace();
  117. } catch (BadPaddingException e) {
  118. e.printStackTrace();
  119. }
  120. //如果有错就返加nulll
  121. return null;
  122. }
  123. public static void main(String[] args) {
  124. String[] keys = {
  125. "", "123456"
  126. };
  127. System.out.println("key | AESEncode | AESDecode");
  128. for (String key : keys) {
  129. System.out.print(key + " | ");
  130. String encryptString = aesEncode(key);
  131. System.out.print(encryptString + " | ");
  132. String decryptString = aesDecode(encryptString);
  133. System.out.println(decryptString);
  134. }
  135. }
  136. }