| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.chuanghai.smartschool.tuitionpayment.utils;
- import com.chuanghai.smartschool.tuitionpayment.config.WeixiaoConfig;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * @Author: codingliang
- * @Description: 微校接口数据加解密
- * @Date: 2021-06-21 10:26
- * @Version: V1.0
- **/
- @Component
- public class MyAES {
- @Autowired
- private WeixiaoConfig weixiaoConfig;
- /**
- * 加密
- *
- * @param sSrc 需要加密的字串
- * @return
- * @throws Exception
- */
- public String encrypt(String sSrc) throws Exception {
- String sKey = weixiaoConfig.getAppKey();
- String sIv = weixiaoConfig.getAppSecret();
- sIv = sIv.substring(0, 16); // app_secret 取前16位
- Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
- int blockSize = cipher.getBlockSize();
- byte[] dataBytes = sSrc.getBytes();
- int plaintextLength = dataBytes.length;
- if (plaintextLength % blockSize != 0) {
- plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
- }
- byte[] plaintext = new byte[plaintextLength];
- System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
- SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
- IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
- cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
- byte[] encrypted = cipher.doFinal(plaintext);
- return byte2hex(encrypted).toLowerCase();
- }
- /**
- * 解密
- *
- * @param sSrc 需要解密的字串
- * @return
- * @throws Exception
- */
- public String decrypt(String sSrc) throws Exception {
- String sKey = weixiaoConfig.getAppKey();
- String sIv = weixiaoConfig.getAppSecret();
- sIv = sIv.substring(0, 16); // app_secret 取前16位
- byte[] encrypted1 = hex2byte(sSrc);
- Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
- SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
- IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());
- cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
- byte[] original = cipher.doFinal(encrypted1);
- String originalString = new String(original);
- return originalString;
- }
- private byte[] hex2byte(String strhex) {
- if (strhex == null) {
- return null;
- }
- int l = strhex.length();
- if (l % 2 == 1) {
- return null;
- }
- byte[] b = new byte[l / 2];
- for (int i = 0; i != l / 2; i++) {
- b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
- 16);
- }
- return b;
- }
- private String byte2hex(byte[] b) {
- String hs = "";
- String stmp = "";
- for (int n = 0; n < b.length; n++) {
- stmp = (Integer.toHexString(b[n] & 0XFF));
- if (stmp.length() == 1) {
- hs = hs + "0" + stmp;
- } else {
- hs = hs + stmp;
- }
- }
- return hs.toUpperCase();
- }
- }
|