zoukankan      html  css  js  c++  java
  • 在文件中读取私钥和公钥

    在做项目中,碰到是给到定的私钥和公钥,我们得把公钥和私钥读出来才可以使用,私钥一般还会有密码!

    /** * 获取公钥 */ public PublicKey readPublicKey(String cerFileName) { CertificateFactory cf; FileInputStream in; Certificate c; PublicKey pk = null; try { cf = CertificateFactory.getInstance("X.509"); in = new FileInputStream(cerFileName); c = cf.generateCertificate(in); pk = c.getPublicKey(); } catch (CertificateException e) { log.error("获取公钥错误!!!"); log.error(e.getMessage()); e.printStackTrace(); } catch (FileNotFoundException e) { log.error("获取公钥错误!!!"); log.error(e.getMessage()); e.printStackTrace(); } return pk; } /** * 获取私钥 * * @return */ public PrivateKey readPrivateKey(String privateKeyFileName, String privatepass) { KeyStore ks; PrivateKey prk = null; try { ks = KeyStore.getInstance("pkcs12"); FileInputStream fis = new FileInputStream(privateKeyFileName); ks.load(fis, null); String pwd = privatepass; String alias = ks.aliases().nextElement().toString(); prk = (PrivateKey) ks.getKey(alias, pwd.toCharArray()); } catch (Exception e) { log.error("获取私钥错误!!!"); log.error(e.getMessage()); e.printStackTrace(); } return prk; }

      

  • 相关阅读:
    在 Spring 中使用 Quartz
    Quartz 快速进阶
    任务调度概述
    Spring Boot 2.x 整合 Mybatis 3.x
    pwd函数实现
    07-图4 哈利·波特的考试 (25 分)
    06-图3 六度空间 (30 分)
    linux中的目录
    Linux中的文件
    06-图2 Saving James Bond
  • 原文地址:https://www.cnblogs.com/atongmyuxiaowanzi/p/5340867.html
Copyright © 2011-2022 走看看