|
|
@@ -0,0 +1,157 @@
|
|
|
+package com.ydtech.modules.order.entity.api.dajia;
|
|
|
+
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64Decoder;
|
|
|
+import cn.hutool.core.codec.Base64Encoder;
|
|
|
+import com.ydtech.modules.order.entity.api.dajia.constants.CIndemLmtLvl;
|
|
|
+import io.lettuce.core.dynamic.annotation.Value;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * AES 加解密工具类, AES/CFB/PKCS5Padding
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class AESEncrypt {
|
|
|
+ final static Logger log = LoggerFactory.getLogger(AESEncrypt.class);
|
|
|
+
|
|
|
+
|
|
|
+// public static AESEncrypt me() {
|
|
|
+// return SpringContextUtil.getBean("AESEncrypt");
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
|
|
|
+ */
|
|
|
+ //private static String sKey = CommonUtil.getRandomString(16, "mix");// key,可自行修改
|
|
|
+ private static String ivParameter = "8811430976208818";// 偏移量,可自行修改
|
|
|
+ private static String sCipherAES = "AES/CFB/PKCS5Padding";
|
|
|
+ // private static String sCipherAES = "RSA/ECB/NoPadding";
|
|
|
+ private static String keySpec = "AES";
|
|
|
+
|
|
|
+ public String encrypt(String encData, String secretKey, String vector) throws Exception {
|
|
|
+ if (secretKey == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (secretKey.length() != 16) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Cipher cipher = Cipher.getInstance(sCipherAES);
|
|
|
+ byte[] raw = secretKey.getBytes();
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, keySpec);
|
|
|
+ // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
|
|
|
+ IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
|
|
+ byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
|
|
|
+ // 此处使用BASE64做转码。
|
|
|
+ return new Base64Encoder().encode(encrypted);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String decrypt(String sSrc, String key, String ivs) throws Exception {
|
|
|
+ try {
|
|
|
+ byte[] raw = key.getBytes("ASCII");
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, keySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance(sCipherAES);
|
|
|
+ IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
|
|
+ // 先用base64解密
|
|
|
+
|
|
|
+ byte[] encrypted1 = new Base64Decoder().decode(sSrc);
|
|
|
+ byte[] original = cipher.doFinal(encrypted1);
|
|
|
+ String originalString = new String(original, "utf-8");
|
|
|
+ return originalString;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String encodeBytes(byte[] bytes) {
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+
|
|
|
+ for (int i = 0; i < bytes.length; i++) {
|
|
|
+ strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
|
|
|
+ strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
|
|
|
+ }
|
|
|
+
|
|
|
+ return strBuf.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加密
|
|
|
+ public static String encrypt(String sSrc, String encryKey) throws Exception {
|
|
|
+ Cipher cipher = Cipher.getInstance(sCipherAES);
|
|
|
+ byte[] raw = encryKey.getBytes("ASCII");
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, keySpec);
|
|
|
+ // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
|
|
|
+ IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
|
|
+ byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
|
|
|
+ // 此处使用BASE64做转码。
|
|
|
+ return new Base64Encoder().encode(encrypted);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解密
|
|
|
+ public static String decrypt(String sSrc, String encryKey) throws Exception {
|
|
|
+ try {
|
|
|
+ byte[] raw = /*sKey*/encryKey.getBytes("ASCII");
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, keySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance(sCipherAES);
|
|
|
+ IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
|
|
+ byte[] encrypted1 = new Base64Decoder().decode(sSrc);// 先用base64解密
|
|
|
+ byte[] original = cipher.doFinal(encrypted1);
|
|
|
+ String originalString = new String(original, "utf-8");
|
|
|
+ return originalString;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+
|
|
|
+
|
|
|
+ String encryKey="qwertyuiopasdfgh";//秘钥
|
|
|
+ // 加密
|
|
|
+ String jsonss = "{\n" +
|
|
|
+ " \"body\": {\n" +
|
|
|
+ " \"businessInfo\": {\n" +
|
|
|
+ " \"busiOpdpt\": \"19\",\n" +
|
|
|
+ " \"busiOprCde\": \"02000176\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"vhlType\": 365001\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"channelCode\": \"TQBD\",\n" +
|
|
|
+ " \"interCode\": \"ODR1007\",\n" +
|
|
|
+ " \"moduleCode\": \"test\",\n" +
|
|
|
+ " \"serialNo\": \"test_20220331_623a1de14fd3488783ce4adf3e6b19eb\",\n" +
|
|
|
+ " \"transTime\": \"2023-11-15 09:03:25\"\n" +
|
|
|
+ "}";
|
|
|
+// String jsonStr = "{\"body\":{\"authenticationNo\":\"21bb9edc-78e2-4d01-bcba-fd49673646cb\",\"checkCode\":\"123456\",\"orderNo\":\"D2022052604020001768f182663e2094\"},\"channelCode\":\"test\",\"interCode\":\"test\",\"moduleCode\":\"test\",\"serialNo\":\"test_20220331_623a1de14fd3488783ce4adf3e6b19eb\",\"transTime\":\"2022-04-02 16:03:52\"}";
|
|
|
+ long lStart = System.currentTimeMillis();
|
|
|
+ String enString = encrypt(jsonss, encryKey);
|
|
|
+ log.info("秘钥:{}", encryKey);
|
|
|
+ log.info("加密后的字串是:{}", enString);
|
|
|
+ System.out.println(enString);
|
|
|
+ long lUseTime = System.currentTimeMillis() - lStart;
|
|
|
+ log.info("加密耗时:{}毫秒", lUseTime);
|
|
|
+ // 解密
|
|
|
+ log.info("秘钥:{}", encryKey);
|
|
|
+ String deS = "04yFURVn6T1L0ApKAm910k32EiCzoxfxkuXK3zR4GJaWclnJh1OmwOllyzWSEPOykrWs691sbW3WeGuDbsisYIgw/RXiC8JJf9PpEy0uVWm5W+rdlaoPcujWPz6L2IuHg+PQqA0aFLpiRlAQ16fshfiasOlqE3Oov+5wBbuEICHTiT42NdcC+VfI6rPDSXTk6T6UiLQNYHHhDn3ZLrqdf14t9WlH3EI+uq12PuHgFgUYBnt1TMxAPWwDNfNwP94pbJ6HYoallMb4rAB0Q1yjrc7Nz8Mt43hda1rYKDIO3WF6KfgTKbDdHWTQqtTStMAbxdV/IBpKaw3QNXal3YeVgGuSJ/5E4F2C6btJsIzLIo/zQ1WM2Yj8VYrp+TSkXaoB";
|
|
|
+
|
|
|
+ String dec = "06THHBNxr34SyEhEeyY7lQrklGRCl9vKEsShgxcGGLLKQup0WLM1Ek4fm/7dQZTDnPH+82tkeHoO3vas6ZJe+xg+R7F7phedehES2gqj+r5xoO9Eiemp3WTtdawiL18FwZ/vEuEx9sYBrQLR8+wZt6F5zy7BbdPW0FwXk7gur4lBsJfUCxbBVX8crdfj2FiK4bqxdGd3FHARn/SNDATCKPGvuLrUwQv9PAZjsPK/ZvLmIL3Ob8NSXUMLGhYDHXy4l4cT3VboIzrYziJtyrULECICuBwUsQUq/L/lsQG9ghXHgMq4h6AuHz19ZAb7vlz/";
|
|
|
+ String DeString = decrypt(deS, encryKey);
|
|
|
+ log.info("decrypt:{}", DeString);
|
|
|
+
|
|
|
+
|
|
|
+ String DeString1 = decrypt(dec, encryKey);
|
|
|
+ log.info("decrypt11:{}", DeString1);
|
|
|
+ }
|
|
|
+}
|