MyAES.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.chuanghai.smartschool.tuitionpayment.utils;
  2. import com.chuanghai.smartschool.tuitionpayment.config.WeixiaoConfig;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. /**
  9. * @Author: codingliang
  10. * @Description: 微校接口数据加解密
  11. * @Date: 2021-06-21 10:26
  12. * @Version: V1.0
  13. **/
  14. @Component
  15. public class MyAES {
  16. @Autowired
  17. private WeixiaoConfig weixiaoConfig;
  18. /**
  19. * 加密
  20. *
  21. * @param sSrc 需要加密的字串
  22. * @return
  23. * @throws Exception
  24. */
  25. public String encrypt(String sSrc) throws Exception {
  26. String sKey = weixiaoConfig.getAppKey();
  27. String sIv = weixiaoConfig.getAppSecret();
  28. sIv = sIv.substring(0, 16); // app_secret 取前16位
  29. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  30. int blockSize = cipher.getBlockSize();
  31. byte[] dataBytes = sSrc.getBytes();
  32. int plaintextLength = dataBytes.length;
  33. if (plaintextLength % blockSize != 0) {
  34. plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  35. }
  36. byte[] plaintext = new byte[plaintextLength];
  37. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  38. SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
  39. IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
  40. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  41. byte[] encrypted = cipher.doFinal(plaintext);
  42. return byte2hex(encrypted).toLowerCase();
  43. }
  44. /**
  45. * 解密
  46. *
  47. * @param sSrc 需要解密的字串
  48. * @return
  49. * @throws Exception
  50. */
  51. public String decrypt(String sSrc) throws Exception {
  52. String sKey = weixiaoConfig.getAppKey();
  53. String sIv = weixiaoConfig.getAppSecret();
  54. sIv = sIv.substring(0, 16); // app_secret 取前16位
  55. byte[] encrypted1 = hex2byte(sSrc);
  56. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  57. SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
  58. IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
  59. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  60. byte[] original = cipher.doFinal(encrypted1);
  61. String originalString = new String(original);
  62. return originalString;
  63. }
  64. private byte[] hex2byte(String strhex) {
  65. if (strhex == null) {
  66. return null;
  67. }
  68. int l = strhex.length();
  69. if (l % 2 == 1) {
  70. return null;
  71. }
  72. byte[] b = new byte[l / 2];
  73. for (int i = 0; i != l / 2; i++) {
  74. b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
  75. 16);
  76. }
  77. return b;
  78. }
  79. private String byte2hex(byte[] b) {
  80. String hs = "";
  81. String stmp = "";
  82. for (int n = 0; n < b.length; n++) {
  83. stmp = (Integer.toHexString(b[n] & 0XFF));
  84. if (stmp.length() == 1) {
  85. hs = hs + "0" + stmp;
  86. } else {
  87. hs = hs + stmp;
  88. }
  89. }
  90. return hs.toUpperCase();
  91. }
  92. }