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;
    }
  • 相关阅读:
    Python13_安装、解释器
    Python12_关于文件概念的讨论与序列化
    Python11_文件的读写
    which | whereis |locate |find
    tail命令 | head命令
    cat 命令|more命令|less命令
    数据库模型设计,第一范式、第二范式、第三范式简单例子理解
    json
    正则表达式
    SFTP相关命令
  • 原文地址:https://www.cnblogs.com/guoke-jsp/p/8968746.html
Copyright © 2011-2022 走看看