MyAES.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * @param sSrc 需要加密的字串
  21. * @return
  22. * @throws Exception
  23. */
  24. public String encrypt(String sSrc) throws Exception {
  25. String sKey = weixiaoConfig.getAppKey();
  26. String sIv = weixiaoConfig.getAppSecret();
  27. sIv = sIv.substring(0, 16); // app_secret 取前16位
  28. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  29. int blockSize = cipher.getBlockSize();
  30. byte[] dataBytes = sSrc.getBytes();
  31. int plaintextLength = dataBytes.length;
  32. if (plaintextLength % blockSize != 0) {
  33. plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  34. }
  35. byte[] plaintext = new byte[plaintextLength];
  36. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  37. SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
  38. IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
  39. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  40. byte[] encrypted = cipher.doFinal(plaintext);
  41. return byte2hex(encrypted).toLowerCase();
  42. }
  43. /**
  44. * 解密
  45. * @param sSrc 需要解密的字串
  46. * @return
  47. * @throws Exception
  48. */
  49. public String decrypt(String sSrc) throws Exception {
  50. String sKey = weixiaoConfig.getAppKey();
  51. String sIv = weixiaoConfig.getAppSecret();
  52. sIv = sIv.substring(0, 16); // app_secret 取前16位
  53. byte[] encrypted1 = hex2byte(sSrc);
  54. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  55. SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
  56. IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
  57. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  58. byte[] original = cipher.doFinal(encrypted1);
  59. String originalString = new String(original);
  60. return originalString;
  61. }
  62. private byte[] hex2byte(String strhex) {
  63. if (strhex == null) {
  64. return null;
  65. }
  66. int l = strhex.length();
  67. if (l % 2 == 1) {
  68. return null;
  69. }
  70. byte[] b = new byte[l / 2];
  71. for (int i = 0; i != l / 2; i++) {
  72. b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
  73. 16);
  74. }
  75. return b;
  76. }
  77. private String byte2hex(byte[] b) {
  78. String hs = "";
  79. String stmp = "";
  80. for (int n = 0; n < b.length; n++) {
  81. stmp = (Integer.toHexString(b[n] & 0XFF));
  82. if (stmp.length() == 1) {
  83. hs = hs + "0" + stmp;
  84. } else {
  85. hs = hs + stmp;
  86. }
  87. }
  88. return hs.toUpperCase();
  89. }
  90. }