package com.stylefeng.guns.modular.system.util.zhenglian;
|
|
import com.stylefeng.guns.modular.system.util.SpringContextsUtil;
|
import com.zlpay.assist.sign.sm2.SM2Cert;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang.RandomStringUtils;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.security.cert.X509Certificate;
|
import java.util.Base64;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2025/7/28 14:22
|
*/
|
@Slf4j
|
public class ZhengLianUtil {
|
|
private static ZhengLianConfig zhengLianConfig = SpringContextsUtil.getBean(ZhengLianConfig.class).getZhengLianConfig();
|
|
/**
|
* @Description: 从公钥证书获取公钥串
|
* @return
|
* @return String
|
* @throws
|
* @author: tsl
|
* @date: 2019年4月19日 下午9:07:56
|
*/
|
public static String getPublicKey() {
|
String publicKey = "";
|
FileInputStream inputStream = null;
|
try {
|
inputStream = new FileInputStream(new File(zhengLianConfig.getCer()));
|
X509Certificate publicCert = SM2Cert.getPublicCert(inputStream);
|
publicKey = Base64.getEncoder().encodeToString(publicCert.getEncoded());
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (inputStream != null) {
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return publicKey;
|
}
|
|
/**
|
* @Description: 从私钥证书获取私钥传
|
* @return
|
* @return String
|
* @throws
|
* @author: tsl
|
* @date: 2019年4月19日 下午9:08:00
|
*/
|
public static String getPrivateKey() {
|
String privateKey = "";
|
FileInputStream inputStream = null;
|
try {
|
inputStream = new FileInputStream(new File(zhengLianConfig.getSm2()));
|
byte[] priBytes = SM2Cert.getPrivateCert(inputStream);
|
privateKey = SM2Cert.getPrivateKey(zhengLianConfig.getPassword(), priBytes);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (inputStream != null) {
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return privateKey;
|
}
|
/**
|
* @Description: 生成对称加密秘钥
|
* @return String
|
* @throws
|
* @author: syuf
|
* @date: 2018年11月8日 下午5:50:05
|
*/
|
public static String generateKey(int length) {
|
return RandomStringUtils.randomAlphanumeric(length);
|
}
|
/**
|
* @Description: 签名
|
* @param data
|
* @return String
|
* @throws
|
* @author: syuf
|
* @date: 2018年11月15日 下午2:19:01
|
*/
|
public static String sign(String data) {
|
try {
|
// 获取私钥
|
String privateKey = getPrivateKey();
|
return com.zlpay.assist.sign.sm2.SM2Util.sign(privateKey, zhengLianConfig.getSignNo(), data);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
}
|