方式1:使用javax.security.cert.X509Certificate进行解析
[java] view plain copy
- URL url = Demo.class.getClassLoader().getResource("C000024.crt"); //证书路径
- System.out.println("公钥所在路径:"+url.getFile());
- X509Certificate cert = X509Certificate.getInstance(new FileInputStream(url.getFile()));
- PublicKey publicKey = cert.getPublicKey();
- BASE64Encoder base64Encoder=new BASE64Encoder();
- String publicKeyString = base64Encoder.encode(publicKey.getEncoded());
- System.out.println("-----------------公钥--------------------");
- System.out.println(publicKeyString);
- System.out.println("-----------------公钥--------------------");
方式2:使用java.security.cert.X509Certificate进行解析
[java]