Android x509Certificate获取公钥长度

2 篇文章 0 订阅
2 篇文章 0 订阅

目录

一、通过x509Certificate来获取CA证书的基本信息

二、 通过公钥获取公钥长度


单独使用x509Certificate只能获取第一步中的基本信息,虽然断点时能看到公钥长度,却不能获取公钥长度,所以这个时候获取到Publickey后就可以使用第二步的方法获取公钥长度。

一、通过x509Certificate来获取CA证书的基本信息

     //创建X509工厂类
     CertificateFactory cf = CertificateFactory.getInstance("X.509");
     //创建证书对象
     X509Certificate oCert = (X509Certificate)cf.generateCertificate(inStream);// inStream证书的传入数据
     inStream.close();
     SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd");
      String info = null;
     //获得证书版本
      info = String.valueOf(oCert.getVersion());
     System.out.println("证书版本:"+info);
     //获得证书序列号
      info = oCert.getSerialNumber().toString(16);
     System.out.println("证书序列号:"+info);
     //获得证书有效期
      Date beforedate = oCert.getNotBefore();
      info = dateformat.format(beforedate);
     System.out.println("证书生效日期:"+info);
      Date afterdate = oCert.getNotAfter();
      info = dateformat.format(afterdate);
     System.out.println("证书失效日期:"+info);
     //获得证书主体信息
      info = oCert.getSubjectDN().getName();
     System.out.println("证书拥有者:"+info);
     //获得证书颁发者信息
      info = oCert.getIssuerDN().getName();
     System.out.println("证书颁发者:"+info);
     //获得证书签名算法名称
      info = oCert.getSigAlgName();
     System.out.println("证书签名算法:"+info);


      byte[] byt = oCert.getExtensionValue("1.2.86.11.7.9");
      String strExt = new String(byt);
     System.out.println("证书扩展域:" + strExt);
      byt = oCert.getExtensionValue("1.2.86.11.7.1.8");
      String strExt2 = new String(byt);
     System.out.println("证书扩展域2:" + strExt2);

二、 通过公钥获取公钥长度

通过X509Certificate.getPublicKey 获取公钥

/**
     * Gets the key length of supported keys
     * @param pk PublicKey used to derive the keysize
     * @return -1 if key is unsupported, otherwise a number >= 0. 0 usually means the length can not be calculated,
     * for example if the key is an EC key and the "implicitlyCA" encoding is used.
     */
    public static int getKeyLength(final PublicKey pk) {
        int len = -1;
        if (pk instanceof RSAPublicKey) {
            final RSAPublicKey rsapub = (RSAPublicKey) pk;
            len = rsapub.getModulus().bitLength();
        } else if (pk instanceof JCEECPublicKey) {
            final JCEECPublicKey ecpriv = (JCEECPublicKey) pk;
            final org.bouncycastle.jce.spec.ECParameterSpec spec = ecpriv.getParameters();
            if (spec != null) {
                len = spec.getN().bitLength();
            } else {
                // We support the key, but we don't know the key length
                len = 0;
            }
        } else if (pk instanceof ECPublicKey) {
            final ECPublicKey ecpriv = (ECPublicKey) pk;
            final java.security.spec.ECParameterSpec spec = ecpriv.getParams();
            if (spec != null) {
                len = spec.getOrder().bitLength(); // does this really return something we expect?
            } else {
                // We support the key, but we don't know the key length
                len = 0;
            }
        } else if (pk instanceof DSAPublicKey) {
            final DSAPublicKey dsapub = (DSAPublicKey) pk;
            if ( dsapub.getParams() != null ) {
                len = dsapub.getParams().getP().bitLength();
            } else {
                len = dsapub.getY().bitLength();
            }
        }
        return len;
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值