新手指南
liangj2025/04/27
1. 对接指南
1.1 调用流程
TIP
- 1、调用方需提供
企业税号
及名称
申请获取私钥
及密码
相关信息; - 2、提供方开通对应
接口套餐
; - 3、调用方根据提供方提供信息,使用
密钥
进行加密,并对数据进行加签
,发送给提供方,获取返回内容后通过密钥
进行解密,获取结果。
2. 相关工具
2.1 相关工具类(java)
import java.nio.charset.Charset;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class SecurityUtil {
private static final String ALGORITHM_3DES = "DESede";
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public SecurityUtil() {
}
public static byte[] encrypt3DES(String encryptPassword, byte[] encryptByte) {
try {
Cipher cipher = init3DES(encryptPassword, 1);
byte[] doFinal = cipher.doFinal(encryptByte);
return doFinal;
} catch (Exception var4) {
return null;
}
}
public static String encrypt3DES(String encryptPassword, String encryptStr) {
try {
Cipher cipher = init3DES(encryptPassword, 1);
byte[] enBytes = cipher.doFinal(encryptStr.getBytes(DEFAULT_CHARSET));
return Base64Util.encode2String(enBytes);
} catch (Exception var4) {
return null;
}
}
public static byte[] decrypt3DES(String decryptPassword, byte[] decryptByte) {
try {
Cipher cipher = init3DES(decryptPassword, 2);
byte[] doFinal = cipher.doFinal(decryptByte);
return doFinal;
} catch (Exception var4) {
return null;
}
}
public static String decrypt3DES(String decryptPassword, String decryptString) {
try {
Cipher cipher = init3DES(decryptPassword, 2);
byte[] deBytes = cipher.doFinal(Base64Util.decode2Byte(decryptString));
return new String(deBytes, DEFAULT_CHARSET);
} catch (Exception var4) {
return null;
}
}
private static Cipher init3DES(String decryptPassword, int cipherMode) throws Exception {
SecretKey deskey = new SecretKeySpec(decryptPassword.getBytes(), "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(cipherMode, deskey);
return cipher;
}
}
import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.codec.binary.Base64;
import javax.crypto.Cipher;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RSAUtil {
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
public static String sign(String content, String privateKey) {
try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey.getBytes(DEFAULT_CHARSET)));
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
Signature signature = Signature.getInstance("SHA1WithRSA");
signature.initSign(priKey);
signature.update(content.getBytes(DEFAULT_CHARSET));
byte[] signed = signature.sign();
return Base64.encodeBase64String(signed);
} catch (Exception var7) {
return null;
}
}
public static boolean verify(String content, String sign, String publicKey) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decodeBase64(publicKey.getBytes(DEFAULT_CHARSET));
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
Signature signature = Signature.getInstance("SHA1WithRSA");
signature.initVerify(pubKey);
signature.update(content.getBytes(DEFAULT_CHARSET));
return signature.verify(Base64.decodeBase64(sign.getBytes(DEFAULT_CHARSET)));
} catch (Exception var8) {
return false;
}
}
public static String encrypt(String content, String publicKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(1, getPublicKey(publicKeyStr));
byte[] enBytes = cipher.doFinal(content.getBytes(DEFAULT_CHARSET));
return Base64.encodeBase64String(enBytes);
}
public static String decrypt(String content, String privateKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, getPrivateKey(privateKeyStr));
byte[] deBytes = cipher.doFinal(Base64.decodeBase64(content.getBytes(DEFAULT_CHARSET)));
return new String(deBytes, DEFAULT_CHARSET);
}
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes = Base64.decodeBase64(key.getBytes(DEFAULT_CHARSET));
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes = Base64.decodeBase64(key.getBytes(DEFAULT_CHARSET));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
public static String getKeyString(Key key) {
byte[] keyBytes = key.getEncoded();
return Base64.encodeBase64String(keyBytes);
}
public static <T> String getSignatureContent(JSONObject params) {
if (params == null) {
return null;
} else {
StringBuilder content = new StringBuilder();
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
for (int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
String value = params.getString(key);
if (value == null) continue;
content.append(i == 0 ? "" : "&").append(key).append("=").append(value);
}
return content.toString();
}
}
public static <T> String getListSignatureContent(List<JSONObject> mapList) {
if (mapList == null) {
return null;
} else {
List<String> listStr = new ArrayList<>();
for (JSONObject item : mapList) {
listStr.add(getSignatureContent(item));
}
Collections.sort(listStr);
return listStr.toString();
}
}
public static void genKeyPair() {
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
try {
Base64 base64 = new Base64();
String publicKeyString = new String(base64.encode(publicKey.getEncoded()));
String privateKeyString = new String(base64.encode(privateKey.getEncoded()));
System.out.println("publicKeyString: " + publicKeyString);
System.out.println("privateKeyString: " + privateKeyString);
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String PUBLIC_KEY = "xxxxxxxx";
public static final String PRIVATE_KEY = "xxxxxx";
}
调用示例
public class Demo {
//私钥
static String tc_privateKey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
//密码
static String tc_pwd = "xxxxxxxxxxxxxxxxxx";
public static void main(String[] args) {
JSONObject param = new JSONObject();
JSONObject content = new JSONObject();//请求参数的集合,除公共参数外所有请求参数都必须放在这个参数中传递
param.put("signType", "RSA");//加密类型
param.put("timestamp", DateFormatUtils.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));//请求时间
param.put("content", SecurityUtil.encrypt3DES(tc_pwd, content.toString()));//业务报文
param.put("servername", "servername");//请求端简称标识
String sign = RSAUtil.sign(RSAUtil.getSignatureContent(param), tc_privateKey);
param.put("sign", sign);
}
public static void main(String[] args) {
String content = "xxxxxxx";
//解密返回报文
SecurityUtil.decrypt3DES(tc_pwd, content);
}
}