密钥文件读取保存为String,并转PublicKey/PrivateKey

开发中得到密钥文件,需要将密钥获取出来,转为String,然后存储在数据库中,后又需要获取然后转为PublicKey/PrivateKey使用,以下是针对一种农行公钥私钥类型的密钥做转换,不一定通用其他,因为还没试过

获取.cer公钥文件并读取

	/********************************
     *
     * @Description  获取.cer公钥文件并读取
     * @MethodName   readPublicKey
     * @param        file
     * @return       java.lang.String
     * @Author       fancw
     * @Date         2019/1/4  9:30
     *
     *******************************/
    private static String readPublicKey(File file) {
   

        String publicKeyString = null;

        // 从指定流中获取数据并生成证书
        X509Certificate cert = null;
        try {
   
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            cert = (X509Certificate) cf.generateCertificate(new FileInputStream(file));
            PublicKey publicKey = cert.getPublicKey();

            System.out.println("-----------------获取的公钥--------------------");
            System.out.println(publicKey);

            publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
            System.out.println("-----------------Base64encode后的公钥--------------------");
            System.out.println(publicKeyString);

        } catch (CertificateException e) {
   
            e.printStackTrace();
        } catch (FileNotFoundException e) {
   
            e.printStackTrace();
        }
        return publicKeyString;
    }

获取.pfx文件并读取

	/********************************
     *
     * @Description  获取.pfx文件并读取
     * @MethodName   readMerchantKey
     * @param        map
     * @param        file
     * @param        keystorePassword
     * @return       java.util.HashMap<java.lang.String,java.lang.String>
     * @Author       fancw
     * @Date         2019/1/4  9:32
     *
     *******************************/
    private static HashMap<String
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值