RSA加解密&RSA加验签
Posted buguge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RSA加解密&RSA加验签相关的知识,希望对你有一定的参考价值。
开发语言与私钥证书的关系如下:
开发语言
|
私钥格式
|
证书格式
|
JAVA
|
.key.p8
|
.crt
|
.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签名(加签/验签)
以上是关于RSA加解密&RSA加验签的主要内容,如果未能解决你的问题,请参考以下文章