zoukankan      html  css  js  c++  java
  • 数字证书中读取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;
    }
  • 相关阅读:
    Scala for the Impatients---(1)Basics
    2.2 Markov Chain
    2.1 Monte Carlo Integration
    1.2 Sampling From Non-standard Distribution
    1.1 Built-in Distributions In Matlab
    Design Pattern -- Builder
    Java Dynamic proxy
    The Difference Between Keypoints and Descriptors
    gcc -l option vs. -L option: The difference
    Stationarity and Independence of Data
  • 原文地址:https://www.cnblogs.com/guoke-jsp/p/8968746.html
Copyright © 2011-2022 走看看