省厅系统对接
This commit is contained in:
parent
e85374d104
commit
1bb8f555b8
|
|
@ -10,7 +10,7 @@
|
||||||
<name>inventory-dao</name>
|
<name>inventory-dao</name>
|
||||||
<description>inventory-dao project for Spring Boot</description>
|
<description>inventory-dao project for Spring Boot</description>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>11</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>11</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>11</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
4
pom.xml
4
pom.xml
|
|
@ -12,8 +12,8 @@
|
||||||
<description>inventory project for Spring Boot</description>
|
<description>inventory project for Spring Boot</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@
|
||||||
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
@ -326,6 +326,12 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--分页 end-->
|
<!--分页 end-->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.11</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
||||||
146
utils/src/main/java/com/common/utils/encryption/ENDEUtils.java
Normal file
146
utils/src/main/java/com/common/utils/encryption/ENDEUtils.java
Normal file
|
|
@ -0,0 +1,146 @@
|
||||||
|
package com.common.utils.encryption;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.crypto.asymmetric.KeyType;
|
||||||
|
import cn.hutool.crypto.asymmetric.RSA;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
|
import java.util.Base64.Decoder;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 省清单数据对接请求响应加解密工具
|
||||||
|
* Created by why on 2024/5/11.
|
||||||
|
*/
|
||||||
|
public class ENDEUtils {
|
||||||
|
|
||||||
|
private static final String AES_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
|
||||||
|
|
||||||
|
private static final String AES = "AES";
|
||||||
|
|
||||||
|
private static final Decoder BASE64DECODER = Base64.getMimeDecoder();
|
||||||
|
|
||||||
|
private static final Integer NANOID_SIZE = 16;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求加密公钥
|
||||||
|
*/
|
||||||
|
private static final String RSA_PUBLIC = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfjUOMOQ3EYNv3f6ARlVSHHdPVJaNoUPA5rNH9TCaPDWg/Mm7dk15ic1kNp4lKCYjSf6HszdN5NQl/Z/CGXGheKQB6Qm8je5MWa2ZVdpUbaO3ihhKzs3KFNHGryOm4P4yjiE6wmj8fun0ILsmgXLISIfYazijkD4InL+I6Dbv7awIDAQAB";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应解密私钥
|
||||||
|
*/
|
||||||
|
private static final String RSA_PRIVATE = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALUqZHsHpD8iV+zbKqPMHLpJUsoLcSIDEJ31sW7XI7cRdYxGb8YFf8Rorb7mzdhzczNushb7H+IAf115/cdOPjbpUzRQ2uPvn87acPoW5gi82KZB1IPqe4sB+jKTmBeYCvkM2QZSUDQZYyElgbZ+DA6DpOVd7nsHzBycYdHckZ9nAgMBAAECgYARi+sSfXF7ieyBLNMK7UyFAHAL0KLi+/AClJbcWOsFIqRDc1kFEWhIEilt2XXrdnccbqbCEg2K+qhbWeLqCFp/Fk+YupPRYzXNAbVVjtaX/RD9xYDZ4UUmywlEgroQqKM5oOqBDYBT5W15eRTPghA3gcx0/Njm9qi6CQtrJriN2QJBANhKZOdJRbj20GMfZrokQProvhTxKH4uWVrooStG44PLZbsr0O0iTG4uf0IucF2VTuJRJXsUvdRqo0GZKWufoX8CQQDWbSGuJkdmdvmbGTkDqPayOqWCOSVA/CWGjzysMOIEkHfoltispn+QiC6E9bkTJUcFKuctH80mKiww13OU/iYZAkB8o3I+LeU1z1UKqd/CyH2BmXlgDBBvljn7vy6RB8q3+g/nqjo9TJSKsdS3yJcL9qx8kpJsuOkjbUgi1yYdVOH1AkBolLmEkq7oTsbW2qpwGoyXyPrIVM7SryguRNY2tXDHAR86lbgAHQTCnWjy+FXX/Kpy3sX5wDTylEcRW0PIO1yhAkA7GH64sKTxinoL6WIbnqkJnBZT4b88WhiD7AIjSHxFAQYwFT1bhrmT7QNuNzt6uXLwSTP0MAiqCe0YZbE1UPh9";
|
||||||
|
|
||||||
|
private static final RSA RSA = new RSA(RSA_PRIVATE, RSA_PUBLIC);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AES解密
|
||||||
|
*
|
||||||
|
* @param content 需要解密的字符串
|
||||||
|
* @param key 密钥
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String AESDecode(String content, String key) {
|
||||||
|
try {
|
||||||
|
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES);
|
||||||
|
Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|
||||||
|
// 先用base64解密
|
||||||
|
byte[] encrypted = BASE64DECODER.decode(content);
|
||||||
|
return new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8.displayName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AES加密
|
||||||
|
*
|
||||||
|
* @param content 需要加密的字符串
|
||||||
|
* @param key 密钥
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String AESEncode(String content, String key) {
|
||||||
|
try {
|
||||||
|
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES);
|
||||||
|
Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
|
||||||
|
byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8.displayName()));
|
||||||
|
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
|
||||||
|
return cn.hutool.core.codec.Base64.encodeStr(encrypted, false, false);
|
||||||
|
// return cn.hutool.core.codec.Base64.encode(encrypted);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密请求参数Request
|
||||||
|
* FIXME
|
||||||
|
*
|
||||||
|
* @param request 请求参数JSON字符格式
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static JSONObject RSAEncryptRequest(String request) {
|
||||||
|
String randomKey = IdUtil.nanoId(NANOID_SIZE);
|
||||||
|
String encryptData = AESEncode(request, randomKey);
|
||||||
|
byte[] encrypt = RSA.encrypt(StrUtil.bytes(randomKey, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||||
|
String encryptRandomKey = Base64.getEncoder().encodeToString(encrypt);
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put("k", encryptRandomKey);
|
||||||
|
json.put("v", encryptData);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密响应参数Response
|
||||||
|
* FIXME
|
||||||
|
*
|
||||||
|
* @param k,v 响应参数 k,v
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static JSONObject RSADecryptResponse(String k, String v) {
|
||||||
|
byte[] decrypt = RSA.decrypt(Base64.getDecoder().decode(k), KeyType.PrivateKey);
|
||||||
|
String decryptData = AESDecode(v, new String(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||||
|
return JSONObject.parseObject(decryptData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//加密请求测试JSON格式字符参数
|
||||||
|
String bodyStr = "{\"sysUniqueCode\":\"273556746985934848\"}";
|
||||||
|
//加密请求参数
|
||||||
|
JSONObject req = RSAEncryptRequest(bodyStr);
|
||||||
|
System.out.println("k----> "+req.getString("k"));
|
||||||
|
System.out.println("v----> "+req.getString("v"));
|
||||||
|
//数据市州CODE对接登录测试
|
||||||
|
HttpResponse execute = HttpUtil.createPost("https://qdz.scasst.net/prod-api/auth/sync/getToken")
|
||||||
|
//临时取消加密
|
||||||
|
// .header("bg-debug", "2")
|
||||||
|
.body(req.toJSONString()).execute();
|
||||||
|
JSONObject res = JSONObject.parseObject(execute.body());
|
||||||
|
//解密响应参数
|
||||||
|
JSONObject response = RSADecryptResponse(res.getString("k"), res.getString("v"));
|
||||||
|
System.out.println("response ---> "+response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user