不想存储Cert证书内容,只想存储证书密钥,可通过以下2種方式实现
一、通過java读取证书的密钥出来:
1 package com.zat.ucop.service.util; 2 3 import sun.misc.BASE64Encoder; 4 5 import java.io.FileInputStream; 6 import java.security.PublicKey; 7 import java.security.cert.CertificateFactory; 8 import java.security.cert.X509Certificate; 9 10 /** 11 * 读取cert格式证书的密钥. 12 * 13 * @author weixiong.cao 14 * @date 2019/4/3 15 */ 16 public class ReadCerDemo { 17 public static void main(String[] args) throws Exception { 18 String keyStorePath = "E:/mpay公钥/client_02.cer"; 19 20 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 21 X509Certificate cert = (X509Certificate) cf.generateCertificate(new FileInputStream(keyStorePath)); 22 PublicKey publicKey = cert.getPublicKey(); 23 BASE64Encoder base64Encoder = new BASE64Encoder(); 24 String publicKeyString = base64Encoder.encode(publicKey.getEncoded()); 25 System.out.println("-----------------公钥--------------------"); 26 System.out.println(publicKeyString); 27 System.out.println("-----------------公钥--------------------"); 28 } 29 }
二、通過openssl命令讀取
進入linux控制臺,輸入以下命令:
openssl x509 -in ./client_02.cer -pubkey -noout > ./mpayPubKey.pubkey
以上命令會將證書內容生成到一個 mpayPubKey.pubkey 文件,文件內容如下: