Access Basic
Encryption Decryption Signature
If you use the SDK for connection, you can ignore this part. Signature generation, signature verification, encryption, and decryption have already been implemented in the JAR package. You can use the interfaces in the SDK just like using ordinary interfaces.
As mentioned before, the public key and private key were sent to the merchant's email when they registered.
package com.deepxin.payment.thirds.paymentwithhttp;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
public class BaseDemo {
// Do not modify PRIVATE_KEY_BASE64!
protected final static String PRIVATE_KEY_BASE64 = loadPrivateKey();
// Do not modify PUBLIC_KEY_BASE_64!
protected final static String PUBLIC_KEY_BASE_64 = loadPublicKey();
protected final static Sign MD5_WITH_RSA_SIGN = SecureUtil.sign(SignAlgorithm.MD5withRSA, PRIVATE_KEY_BASE64, PUBLIC_KEY_BASE_64);
protected final static int SUCCESS_CODE = 100000;
protected static String loadPrivateKey() {
// in real-world applications, keys should be loaded from a secure storage location.
return "your private key";
}
protected static String loadPublicKey() {
// in real-world applications, keys should be loaded from a secure storage location.
return "your public key";
}
protected static String encryptWithPublicKey(Object data, String publicKey) {
Assert.notNull(data, "Data cannot be null.");
Assert.notBlank(publicKey, "Public key cannot be blank.");
RSA rsa = new RSA( null, publicKey);
return rsa.encryptBase64(JSONUtil.toJsonStr(data), KeyType.PublicKey);
}
protected static String sign(Object data, String appKey) {
Assert.notNull(data, "Data cannot be null.");
Assert.notBlank(appKey, "Key cannot be blank.");
return Base64.encode(MD5_WITH_RSA_SIGN.sign(getCombinationBytes(data, appKey)));
}
protected static byte[] getCombinationBytes(Object param, String appKey) {
return (JSONUtil.toJsonStr(param) + appKey).getBytes();
}
protected static boolean verify(String verifySign, Object param, String appKey) {
if (CharSequenceUtil.isBlank(verifySign)) {
return false;
}
return MD5_WITH_RSA_SIGN.verify(getCombinationBytes(param, appKey), Base64.decode(verifySign));
}
protected static String decryptWithPrivateKey(String encryptStr, String privateKey) {
if (CharSequenceUtil.isBlank(encryptStr)) {
return null;
}
RSA rsa = new RSA(privateKey, null);
return rsa.decryptStr(encryptStr, KeyType.PrivateKey);
}
// Get request map
protected static Map<String, Object> getRequestMap(String encryptStr, String merchantCode, String sign) {
Map<String, Object> requestMap = new HashMap<>();
requestMap.put("data", encryptStr);
requestMap.put("merchantCode", merchantCode);
requestMap.put("sign", sign);
return requestMap;
}
}