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("-----------------公钥-------------------