java读取pfx格式的证书-并获取公钥私钥

java读取pfx格式的证书-并获取公钥私钥packagecom.hgh.javase.security;importjava.io.FileInputStream;importjava.security.KeyStore;importjava.security.PrivateKey;importjava.security.PublicKey;importjava.security.cert.Certificate;im

大家好,又见面了,我是你们的朋友全栈君。

package com.hgh.javase.security;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class ReadPFX {  
    public ReadPFX (){  
    }  
    //转换成十六进制字符串 
    public static String Byte2String(byte[] b) {  
        String hs="";  
        String stmp="";  

        for (int n=0;n<b.length;n++) {  
            stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));  
            if (stmp.length()==1) hs=hs+"0"+stmp;  
            else hs=hs+stmp;  
            //if (n<b.length-1) hs=hs+":"; 
        }  
        return hs.toUpperCase();  
    }  

    public static byte[] StringToByte(int number) {   
        int temp = number;   
        byte[] b=new byte[4];   
        for (int i=b.length-1;i>-1;i--){   
            b[i] = new Integer(temp&0xff).byteValue();//将最高位保存在最低位 
            temp = temp >> 8; //向右移8位 
        }   
        return b;   
    }   
     public  PrivateKey GetPvkformPfx(String strPfx, String strPassword){  
        try {  
            KeyStore ks = KeyStore.getInstance("PKCS12");  
            FileInputStream fis = new FileInputStream(strPfx);  
            // If the keystore password is empty(""), then we have to set 
            // to null, otherwise it won't work!!! 
            char[] nPassword = null;  
            if ((strPassword == null) || strPassword.trim().equals("")){  
                nPassword = null;  
            }  
            else  
            {  
                nPassword = strPassword.toCharArray();  
            }  
            ks.load(fis, nPassword);  
            fis.close();  
            System.out.println("keystore type=" + ks.getType());  
            // Now we loop all the aliases, we need the alias to get keys. 
            // It seems that this value is the "Friendly name" field in the 
            // detals tab <-- Certificate window <-- view <-- Certificate 
            // Button <-- Content tab <-- Internet Options <-- Tools menu 
            // In MS IE 6. 
            Enumeration enumas = ks.aliases();  
            String keyAlias = null;  
            if (enumas.hasMoreElements())// we are readin just one certificate. 
            {  
                keyAlias = (String)enumas.nextElement();   
                System.out.println("alias=[" + keyAlias + "]");  
            }  
            // Now once we know the alias, we could get the keys. 
            System.out.println("is key entry=" + ks.isKeyEntry(keyAlias));  
            PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);  
            Certificate cert = ks.getCertificate(keyAlias);  
            PublicKey pubkey = cert.getPublicKey();  
            System.out.println("cert class = " + cert.getClass().getName());  
            System.out.println("cert = " + cert);  
            System.out.println("public key = " + pubkey);  
            System.out.println("private key = " + prikey);  
            return prikey;  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }

     public static void main(String[] args) throws Exception {
         String strPfx = "G:\\workspaceOfJava\\javaSe_test\\testfile\\联想测试pfx证书-密码123.pfx";
         String strPassword ="123";
          KeyStore ks = KeyStore.getInstance("PKCS12");  
          FileInputStream fis = new FileInputStream(strPfx);  
          // If the keystore password is empty(""), then we have to set 
          // to null, otherwise it won't work!!! 
          char[] nPassword = null;  
          if ((strPassword == null) || strPassword.trim().equals("")){  
              nPassword = null;  
          }  
          else  
          {  
              nPassword = strPassword.toCharArray();  
          }  
          ks.load(fis, nPassword);  
          fis.close();  
          System.out.println("keystore type=" + ks.getType());  
          // Now we loop all the aliases, we need the alias to get keys. 
          // It seems that this value is the "Friendly name" field in the 
          // detals tab <-- Certificate window <-- view <-- Certificate 
          // Button <-- Content tab <-- Internet Options <-- Tools menu 
          // In MS IE 6. 
          Enumeration enumas = ks.aliases();  
          String keyAlias = null;  
          if (enumas.hasMoreElements())// we are readin just one certificate. 
          {  
              keyAlias = (String)enumas.nextElement();   
              System.out.println("alias=[" + keyAlias + "]");  
          }  
          // Now once we know the alias, we could get the keys. 
          System.out.println("is key entry=" + ks.isKeyEntry(keyAlias));  
          PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);  
          Certificate cert = ks.getCertificate(keyAlias);  
          PublicKey pubkey = cert.getPublicKey();  
          System.out.println("cert class = " + cert.getClass().getName());  
          System.out.println("cert = " + cert);  
          System.out.println("public key = " + pubkey);  
          System.out.println("private key = " + prikey);  
    }
}  
keystore type=PKCS12
alias=[51d5a154-415e-4b1b-a055-bb37858ca95a]
is key entry=true
cert class = sun.security.x509.X509CertImpl
cert = [
[
  Version: V3
  Subject: O="iTrusChina Co.,Ltd.", OU=Lenovo Project, CN=测试证书20180223, OU=QXCD - 33333, OU=TaxCD - 22222, OU=AgencyID - 11111, EMAILADDRESS=13076684@q123123.com
  Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

  Key:  Sun RSA public key, 2048 bits
  modulus: 22657557413401284100317094122201793054162429174129431065947590647854190906608370581286403391341087763841097975687068701429936152897071539889422949403226598003536587449975692615847706829935556655811895258170702116834985572238495571627042731572846922335830615637175517376649775332008195580962533786663755913965907159282213259680423901869255714119163043711206221551766213650295141599478801968859399127722270154565860981431180272488351937142179175776325777949972384300433866258071941139117596804563616993083186036901020114838205796878318295684796020281687231710152726135818054983898634643653967742508133763124702016417333
  public exponent: 65537
  Validity: [From: Fri Feb 23 16:02:18 CST 2018,
 To: Sat Feb 23 16:02:18 CST 2019]
  Issuer: CN=iTrusChina Class 2 Enterprise Subscriber CA - G3, OU=China Trust Network, O=iTrusChina, C=CN
  SerialNumber: [    3d94cfc7 94b6bec0 90f6a2ac 8c3f270b ee9bddc7]

Certificate Extensions: 3
[1]: ObjectId: 2.5.29.19 Criticality=false BasicConstraints:[ CA:false PathLen: undefined ]  [2]: ObjectId: 2.5.29.31 Criticality=false CRLDistributionPoints [ [DistributionPoint: [URIName: http://topca.itrus.com.cn/public/itruscrl?CA=2C1D97288DE14F85C6640698FB24739AF47DFD1A] ]]  [3]: ObjectId: 2.5.29.15 Criticality=false KeyUsage [ DigitalSignature Key_Encipherment ]  ] Algorithm: [SHA1withRSA] Signature: 0000: 5B EE E7 0C 11 FF 50 0E 04 8F B8 09 7F F7 BF 0E [.....P......... 0010: A5 5C B6 BB F7 78 DE EC AF 32 9A C1 02 9A 7B 5E .\...x...2.....^ 0020: 23 19 9D 8D 8C 47 16 0B AA F0 92 EF 29 51 5B 3E #....G......)Q[> 0030: 03 E5 4F 18 86 DC ED 4C 63 29 3B E6 06 7A 1C 92 ..O....Lc);..z.. 0040: 20 1D 60 2B 42 C5 C3 B6 98 61 A0 8E 20 FF 0F 33 .`+B....a.. ..3 0050: CA F0 32 C8 8B 2C 64 D7 99 4F FC 4F 98 22 4E D3 ..2..,d..O.O."N. 0060: 6A E3 0E F8 A7 2D 04 AD 37 39 23 4C 21 09 C4 8F j....-..79#L!... 0070: 9D 7E 4D 5B 5C C9 25 4F BB EC 6F 80 C8 B8 F0 CE ..M[\.%O..o..... 0080: BD D8 E6 E5 88 88 DB 7B BA 64 E2 57 E3 0F ED 55 .........d.W...U 0090: 74 A6 77 AD 9F CC 78 07 BD 69 7E 6C D3 B5 7E BE t.w...x..i.l.... 00A0: 0D DF 47 40 D0 BE 00 08 F5 3B AE C6 70 0B BB DD ..G@.....;..p... 00B0: 51 50 20 3F CF AD 96 BC 69 0B B3 F3 02 16 D3 DE QP ?....i....... 00C0: 31 7B 78 A2 92 C3 00 E1 24 22 39 41 F6 BD 31 7C 1.x.....$"9A..1. 00D0: 8A 1B 73 0E 59 23 2A 2E 6C 9A 34 8B CB 2F E8 24 ..s.Y#*.l.4../.$ 00E0: 33 07 19 8B 55 D6 5E C9 A5 92 F2 CB 38 1E 37 3F 3...U.^.....8.7? 00F0: B0 20 0C 64 60 03 17 4C C9 CE 7E 67 C5 98 BF F3 . .d`..L...g....  ] public key = Sun RSA public key, 2048 bits modulus: 22657557413401284100317094122201793054162429174129431065947590647854190906608370581286403391341087763841097975687068701429936152897071539889422949403226598003536587449975692615847706829935556655811895258170702116834985572238495571627042731572846922335830615637175517376649775332008195580962533786663755913965907159282213259680423901869255714119163043711206221551766213650295141599478801968859399127722270154565860981431180272488351937142179175776325777949972384300433866258071941139117596804563616993083186036901020114838205796878318295684796020281687231710152726135818054983898634643653967742508133763124702016417333 public exponent: 65537 private key = sun.security.rsa.RSAPrivateCrtKeyImpl@9f22d 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132731.html原文链接:https://javaforall.net

(1)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android开发之做一个竖着的seekbar

    工作繁忙,总想有机会停下来喘口气,整理一下思路,做一些积累,但是这样的机会很少。要做android的界面,基础的要求是熟悉那些内置的组件(prebuilt componments),而有时候这些组件是不够用的,这时候我们就需要自己写一个新的,或者修改内置的。这就要求我们了解更多知识,比如各个View是怎么画出来的,UI上各种事件是如何分发和处理的等等。这些我都计划写出来和大家分享,网络上中文资料,

    2022年3月10日
    37
  • 达芬奇五年沉浮

    达芬奇五年沉浮达芬奇五年沉浮

    2022年5月18日
    56
  • MAC电脑 系统 恢复出厂设置

    MAC电脑 系统 恢复出厂设置具体步骤:1首先开启Mac的电源开关,同时按住command+R键进入恢复模式,我们就能看MacOSX实用工具。2选择磁盘工具–然后选择上面一般是MacintoshHD这个选项,右侧选择抹掉。意思就是删除所有内容重新安装新的系统。3重新启动Mac,这时出现地球标志,等待更新完成。这里PC6小编要提醒各位进行这项操作的Mac用户,由于在线恢复需要从苹果服务器下载完整的OS

    2022年6月21日
    31
  • navicat连接mysql时报错1251怎么办

    navicat连接mysql时报错1251怎么办1、新安装的mysql8,使用激活成功教程版的navicat连接的时候一直报错,如图所示:2、网上查找原因发现是mysql8之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。解决问题方法有两种,一种是升级navicat驱动;一种是把mysql用户登录密码加密规则还原成mysql_native_password。由于用的是激活成功教程版的navicat,所以只能用第二种方法解决了。3、首先win.

    2022年10月10日
    0
  • lib文件和dll文件的区别_dll2lib

    lib文件和dll文件的区别_dll2lib一.简介1.1C++两种库文件lib包含了函数所在的dll文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的dll提供,称为动态链接库dynamiclinklibrary。

    2022年8月4日
    5
  • cpu流水线工作原理_嵌入式工作原理

    cpu流水线工作原理_嵌入式工作原理现在的CPU处理器一般都是超流水线工作,动不动就是10级以上流水线,超高主频,这两者之间有什么关系呢?今天就跟大家科普下CPU流水线的工作原理,以及他们之间的关系。说到流水线,很多人会想到富士康;说到富士康,很多人会想到张全蛋。作为富士康3号流水线资深质检员,下面就请张全蛋给大家科普下什么是流水线,大家鼓掌欢迎。MichealJack眼中的流水线大家好,我是张全蛋,英文名叫MichealJack,法文名叫霍雷呆-杰Q赖,大家也可以叫我查理。作为iPhone手机3号流水线的资.

    2022年8月20日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号