AesUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.template.common.utils;
  2. import javax.crypto.BadPaddingException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.IllegalBlockSizeException;
  5. import javax.crypto.NoSuchPaddingException;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.security.InvalidAlgorithmParameterException;
  9. import java.security.InvalidKeyException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.UUID;
  12. /**
  13. * <p>Title: AesUtil</p>
  14. * <p>Description: AES加密解密</p>
  15. * @author fengyong
  16. * @date 2018年9月7日
  17. */
  18. public class AesUtil {
  19. /**
  20. * 秘钥
  21. */
  22. public static final String PASSWORD_SECRET_KEY = "EasyRailEveryday";
  23. /**
  24. * 初始向量
  25. */
  26. public static final String INITIAL_VECTOR = "EasyRailEasyRail";
  27. /**
  28. * 加密
  29. * @param content 需要加密的内容
  30. * @param password 加密密码
  31. * @param keySize 密钥长度16,24,32(密码长度为24和32时需要将local_policy.jar/US_export_policy.jar两个jar包放到JRE目录%jre%/lib/security下)
  32. * @return
  33. */
  34. public static byte[] encrypt(String content, String password, int keySize){
  35. try {
  36. //密钥长度不够用0补齐。
  37. SecretKeySpec key = new SecretKeySpec(ZeroPadding(password.getBytes(Base64Util.DEFAULT_CHARSET), keySize), "AES");
  38. //定义加密算法AES、算法模式ECB、补码方式PKCS5Padding
  39. //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  40. //定义加密算法AES 算法模式CBC、补码方式PKCS5Padding
  41. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  42. //CBC模式模式下初始向量 不足16位用0补齐
  43. IvParameterSpec iv = new IvParameterSpec(ZeroPadding(INITIAL_VECTOR.getBytes(Base64Util.DEFAULT_CHARSET),16));
  44. byte[] byteContent = content.getBytes();
  45. //初始化加密
  46. //ECB
  47. //cipher.init(Cipher.ENCRYPT_MODE, key);
  48. //CBC
  49. cipher.init(Cipher.ENCRYPT_MODE, key,iv);
  50. byte[] result = cipher.doFinal(byteContent);
  51. return result;
  52. } catch (NoSuchAlgorithmException e) {
  53. e.printStackTrace();
  54. } catch (NoSuchPaddingException e) {
  55. e.printStackTrace();
  56. } catch (InvalidKeyException e) {
  57. e.printStackTrace();
  58. } catch (IllegalBlockSizeException e) {
  59. e.printStackTrace();
  60. } catch (BadPaddingException e) {
  61. e.printStackTrace();
  62. } catch (InvalidAlgorithmParameterException e) {
  63. e.printStackTrace();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. return null;
  68. }
  69. /**解密
  70. * @param content 待解密内容
  71. * @param password 解密密钥
  72. * @param keySize 密钥长度16,24,32(密码长度为24和32时需要将local_policy.jar/US_export_policy.jar两个jar包放到JRE目录%jre%/lib/security下)
  73. * @return
  74. */
  75. public static String decrypt(byte[] content, String password, int keySize) {
  76. try {
  77. //密钥长度不够用0补齐。
  78. SecretKeySpec key = new SecretKeySpec(ZeroPadding(password.getBytes(), keySize), "AES");
  79. //定义加密算法AES、算法模式ECB、补码方式PKCS5Padding
  80. //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  81. //定义加密算法AES 算法模式CBC、补码方式PKCS5Padding
  82. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  83. //CBC模式模式下初始向量 不足16位用0补齐
  84. IvParameterSpec iv = new IvParameterSpec(ZeroPadding(INITIAL_VECTOR.getBytes(Base64Util.DEFAULT_CHARSET),16));
  85. // 初始化解密
  86. //ECB
  87. //cipher.init(Cipher.DECRYPT_MODE, key);
  88. //CBC
  89. cipher.init(Cipher.DECRYPT_MODE, key,iv);
  90. byte[] result = cipher.doFinal(content);
  91. return new String(result,Base64Util.DEFAULT_CHARSET);
  92. } catch (NoSuchAlgorithmException e) {
  93. e.printStackTrace();
  94. } catch (NoSuchPaddingException e) {
  95. e.printStackTrace();
  96. } catch (InvalidKeyException e) {
  97. e.printStackTrace();
  98. } catch (IllegalBlockSizeException e) {
  99. e.printStackTrace();
  100. } catch (BadPaddingException e) {
  101. e.printStackTrace();
  102. } catch (InvalidAlgorithmParameterException e){
  103. e.printStackTrace();
  104. } catch (Exception e){
  105. e.printStackTrace();
  106. }
  107. return null;
  108. }
  109. /**将二进制转换成16进制
  110. * @param buf
  111. * @return
  112. */
  113. public static String parseByte2HexStr(byte buf[]) {
  114. StringBuilder sb = new StringBuilder();
  115. for (int i = 0; i < buf.length; i++) {
  116. String hex = Integer.toHexString(buf[i] & 0xFF);
  117. if (hex.length() == 1) {
  118. hex = '0' + hex;
  119. }
  120. sb.append(hex.toUpperCase());
  121. }
  122. return sb.toString();
  123. }
  124. /**将16进制转换为二进制
  125. * @param hexStr
  126. * @return
  127. */
  128. public static byte[] parseHexStr2Byte(String hexStr) {
  129. if (hexStr.length() < 1){
  130. return null;
  131. }
  132. byte[] result = new byte[hexStr.length()/2];
  133. for (int i = 0;i< hexStr.length()/2; i++) {
  134. int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
  135. int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
  136. result[i] = (byte) (high * 16 + low);
  137. }
  138. return result;
  139. }
  140. /**
  141. * 字符达不到指定长度补0
  142. * @param in 字符数组
  143. * @param blockSize 长度
  144. * @return
  145. */
  146. public static byte[] ZeroPadding(byte[] in,Integer blockSize){
  147. Integer copyLen = in.length;
  148. if (copyLen > blockSize) {
  149. copyLen = blockSize;
  150. }
  151. byte[] out = new byte[blockSize];
  152. System.arraycopy(in, 0, out, 0, copyLen);
  153. return out;
  154. }
  155. }