AESUtil.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.nokia.financeapi.utils;
  2. import cn.hutool.core.codec.Base64;
  3. import com.google.gson.Gson;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. /**
  10. * aes加密解密
  11. */
  12. public class AESUtil {
  13. static final String KEY = "2na$$PdV9AW8b#CS";
  14. // 固定替换
  15. private static final String ADD = "/add/";
  16. // 加密
  17. public static String encrypt(String str, String key) throws Exception {
  18. // 加密
  19. if (str == null || key == null) {
  20. // 加密
  21. return null;
  22. }
  23. // 加密
  24. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  25. // 加密
  26. cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
  27. // 加密
  28. byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
  29. // 加密
  30. String result = Base64.encode(bytes);
  31. // String result = new BASE64Encoder().encode(bytes);
  32. // 加密
  33. result = result.replaceAll("\r\n", "");
  34. // 加密
  35. result = result.replaceAll("\\+", ADD);
  36. // 加密
  37. return result;
  38. }
  39. // 解密
  40. public static String decrypt(String str, String key) throws Exception {
  41. // 解密
  42. if (str == null || key == null) {
  43. // 解密
  44. return null;
  45. }
  46. // 解密
  47. str = str.replaceAll("\\\\n", "");
  48. // 解密
  49. str = str.replaceAll(ADD, "+");
  50. // 解密
  51. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  52. // 解密
  53. cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
  54. // 解密
  55. byte[] bytes = Base64.decode(str);
  56. // byte[] bytes = new BASE64Decoder().decodeBuffer(str);
  57. // 解密
  58. bytes = cipher.doFinal(bytes);
  59. // 解密
  60. return new String(bytes);
  61. }
  62. public static String decrypt(String str) throws Exception {
  63. return decrypt(str, KEY);
  64. }
  65. public static void main(String[] args) throws Exception {
  66. // 参数
  67. HashMap<String, String> map = new HashMap<>();
  68. // 财务系统id, 固定值
  69. map.put("APP_ID", "FINANCE");
  70. // 时间戳
  71. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  72. map.put("TIME_STAMP", sdf.format(new Date()));
  73. // 当前登录账号
  74. map.put("LOGIN_ID", "loginId");
  75. //请求的地址
  76. map.put("REQUEST_URL","https://ip:port/aaa");
  77. // 加密串
  78. String encrypt = AESUtil.encrypt(new Gson().toJson(map), "2na$$PdV9AW8b#CS");
  79. // 返回
  80. System.out.println(encrypt);
  81. //解密
  82. System.out.println(AESUtil.decrypt(encrypt,"2na$$PdV9AW8b#CS"));
  83. System.out.println(AESUtil.decrypt("zoQtYlmhk/add/mBUBZD5mFJB1IXEwaLRS97Uf9z9Hlqdh8eF/add/c2gDUmD3pwGPfA8FQKBNTCB8LPlCPF/vHcU/2tL7Ps/add/6rn6w5rDS98R4GJueGgU01n12YZVS/FNx6pSI/add/BuEy1XyrSg8QBfx7igSozPxmdZ6a235/add/fqZPPSjoQReI","2na$$PdV9AW8b#CS"));
  84. }
  85. }