mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 19:45:09 +08:00
新增 宁波点行平台Controller
This commit is contained in:
@@ -28,12 +28,13 @@ import java.util.TreeMap;
|
||||
*/
|
||||
public class Sm2Util {
|
||||
|
||||
static{
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成sm2的公钥私钥对
|
||||
*
|
||||
* @return 公钥私钥对
|
||||
*/
|
||||
public static KeyPair generateSm2KeyPair() {
|
||||
@@ -54,9 +55,8 @@ public class Sm2Util {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data 加密前的json数据
|
||||
* @param platformPublicKeyStr 点行平台的国密二公钥
|
||||
* @param data 加密前的json数据
|
||||
* @param platformPublicKeyStr 点行平台的国密二公钥
|
||||
* @param thirdPartyPrivateKeyStr 第三方平台生成的国密二私钥
|
||||
* @return 用于http发送请求的数据
|
||||
* @throws NoSuchAlgorithmException
|
||||
@@ -67,14 +67,13 @@ public class Sm2Util {
|
||||
public static String generateEncryptedRequestInfo(JSONObject data, String platformPublicKeyStr, String thirdPartyPrivateKeyStr)
|
||||
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, CryptoException {
|
||||
String sign = sign(thirdPartyPrivateKeyStr, data);
|
||||
data.put("sign",sign);
|
||||
data.put("sign", sign);
|
||||
return encrypt(platformPublicKeyStr, data.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param publicKeyStr 以BASE64表示的sm2公钥
|
||||
* @param data 待加密字符串, UTF-8编码
|
||||
* @param data 待加密字符串, UTF-8编码
|
||||
* @return 以BASE64表示的加密结果
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws NoSuchProviderException
|
||||
@@ -106,9 +105,8 @@ public class Sm2Util {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param privateKeyStr 以BASE64表示的sm2私钥
|
||||
* @param cipherData 以BASE64表示的加密结果
|
||||
* @param cipherData 以BASE64表示的加密结果
|
||||
* @return 解密后字符串, UTF-8编码
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws NoSuchProviderException
|
||||
@@ -140,9 +138,8 @@ public class Sm2Util {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param privateKeyStr 以BASE64表示的sm2私钥
|
||||
* @param dataJson 待签名json数据
|
||||
* @param dataJson 待签名json数据
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws NoSuchProviderException
|
||||
* @throws InvalidKeySpecException
|
||||
@@ -175,9 +172,8 @@ public class Sm2Util {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param publicKeyStr 以BASE64表示的sm2公钥
|
||||
* @param dataJson 含有签名的待验签json数据
|
||||
* @param dataJson 含有签名的待验签json数据
|
||||
* @return
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws NoSuchProviderException
|
||||
@@ -202,22 +198,22 @@ public class Sm2Util {
|
||||
return sm2Signer.verifySignature(Base64.decode(sign));
|
||||
}
|
||||
|
||||
private static String getSignContent(JSONObject rawData){
|
||||
private static String getSignContent(JSONObject rawData) {
|
||||
JSONObject data = new JSONObject(new TreeMap<>());
|
||||
rawData.forEach((k,v) -> data.put(k,v));
|
||||
rawData.forEach((k, v) -> data.put(k, v));
|
||||
StringBuffer sb = new StringBuffer();
|
||||
data.forEach((k,v)->{
|
||||
data.forEach((k, v) -> {
|
||||
if (v != null && !"".equals(v)) {
|
||||
sb.append(k + "=" + v + "&");
|
||||
}
|
||||
});
|
||||
String stringData = sb.toString();
|
||||
return stringData == null || stringData.isEmpty() ? "" : stringData.substring(0,stringData.length()-1);
|
||||
return stringData == null || stringData.isEmpty() ? "" : stringData.substring(0, stringData.length() - 1);
|
||||
}
|
||||
|
||||
private static BCECPrivateKey getPrivateKey(byte[] privateBytes) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
|
||||
PKCS8EncodedKeySpec eks2 = new PKCS8EncodedKeySpec(privateBytes);
|
||||
KeyFactory kf= KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
|
||||
KeyFactory kf = KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
|
||||
return (BCECPrivateKey) kf.generatePrivate(eks2);
|
||||
}
|
||||
|
||||
@@ -228,9 +224,9 @@ public class Sm2Util {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, CryptoException {
|
||||
// KeyPair keyPair = generateSm2KeyPair();
|
||||
// System.out.println(Base64.toBase64String(keyPair.getPublic().getEncoded()));
|
||||
// System.out.println(Base64.toBase64String(keyPair.getPrivate().getEncoded()));
|
||||
// KeyPair keyPair = generateSm2KeyPair();
|
||||
// System.out.println(Base64.toBase64String(keyPair.getPublic().getEncoded()));
|
||||
// System.out.println(Base64.toBase64String(keyPair.getPrivate().getEncoded()));
|
||||
String platformPublicKeyStr = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEYPOlKmr/XY+na8KxiNvRui1esFugt4tT2AVk+eRlH4KCYLabDZDordal3kcn4UNM7t6J+dyhcfLstNWXpf4lQA==";
|
||||
String thirdPartyPrivateKeyStr = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgCMref1FGlPZ9RfeJw/cnU5uEvFZNhHt7OvF4sgXnBjWgCgYIKoEcz1UBgi2hRANCAARj6kqkCaeNJSxWExQFsot1OuSCFrQOblhKx0U/y8GhgSND2MOAM08yXzl308waLqLt+jcsLF2UTW6XfrZNS5pk";
|
||||
JSONObject data = new JSONObject();
|
||||
|
||||
Reference in New Issue
Block a user