数字证书中读取PublicKey


1. 读取https签发证书中的key

 1) 在下面的代码中,是实现读取证书字符串来读取key的,CERTIFICATE 就是一个证书的字符串, 而方法cf.generateCertificate() 接受的是一个InputStream 流,当然这个地方也可以读取一个文件 new FileInputSream("file path")即可!

public String getCertificateKey() {
        CertificateFactory cf = null;
        PublicKey publicKey = null;
        try {
            cf = CertificateFactory.getInstance("X.509");
            //DataInputStream  di = new DataInputStream("");
            X509Certificate cert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(CERTIFICATE.getBytes()));
            publicKey = cert.getPublicKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
        byte[] publicKeyString = Base64.encode(publicKey.getEncoded(), Base64.DEFAULT);
        String publickey = new String(publicKeyString);
        System.out.println("-----------------公钥--------------------");
        System.out.println(publickey);
        System.out.println("-----------------公钥--------------------");
        return publickey;
    }

 2) 这里的 X509Certificate 文件是 import java.security.cert.X509Certificate; 包路径下的, 

 

 

 

public String getCertificateKey() {
CertificateFactory cf = null;
PublicKey publicKey = null;
try {
cf = CertificateFactory.getInstance("X.509");
//DataInputStream di = new DataInputStream("");
X509Certificate cert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(CERTIFICATE.getBytes()));
publicKey = cert.getPublicKey();
} catch (Exception e) {
e.printStackTrace();
}
byte[] publicKeyString = Base64.encode(publicKey.getEncoded(), Base64.DEFAULT);
String publickey = new String(publicKeyString);
System.out.println("-----------------公钥--------------------");
System.out.println(publickey);
System.out.println("-----------------公钥--------------------");
return publickey;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM