开发语言与私钥证书的关系如下:
开发语言 |
私钥格式 |
证书格式 |
JAVA |
.key.p8 |
.crt |
PHP |
.key.pem |
.cert.pem |
.NET |
.key.der |
.crt |
其它 |
.key.pem |
.cert.pem |
我们是java语言,工程property配置:
#商户私钥 PKCS#8标准的私钥
90000002.mer.prikey.path=cert/90000002.key.p8
#平台公钥 X.509证书
plat.cert.path=cert/umpay.cert.crt
RSA加密/RSA解密
获取公钥和私钥
import com.umpay.core.util.ProFileUtil; import java.io.ByteArrayInputStream;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.PublicKey;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;public static PublicKey getPublicKey() throws Exception { byte[] b = ProFileUtil.getFileByte("plat.cert.path"); ByteArrayInputStream bais = new ByteArrayInputStream(b); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate x509Certificate = (X509Certificate) cf.generateCertificate(bais); byte[] keyBytes = x509Certificate.getPublicKey().getEncoded(); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(x509KeySpec);} public static PrivateKey getPrivateKey(String merId) throws Exception { byte[] key = ProFileUtil.getFileByte(merId + ".mer.prikey.path"); PKCS8EncodedKeySpec e = new PKCS8EncodedKeySpec(key); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(e);}
RSA签名(加签/验签)
原文地址:https://www.cnblogs.com/buguge/p/11769740.html
时间: 2024-11-07 16:46:41