JAVA从一个.p12或.pfx文件中获取公钥和私钥
来源:互联网 发布:pubmed数据库主题词 编辑:程序博客网 时间:2024/04/27 15:44
原文地址:http://www.cjsdn.net/post/view?bid=6&id=57420&sty=3&age=0/** * Read a p12 format digital certificate. Be careful about the file format. * Sometimes, it might be incompatible. If it happens, import/export again * using netscape(p12) or IE(pfx). */import java.security.KeyStore;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Key;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.io.*;import java.util.*;public class ReadP12Cert{ public static void main(String[] args) { final String KEYSTORE_FILE = "cert/dev_coo1.p12"; final String KEYSTORE_PASSWORD = "123"; final String KEYSTORE_ALIAS = "alias"; try { KeyStore ks = KeyStore.getInstance("PKCS12"); FileInputStream fis = new FileInputStream(KEYSTORE_FILE); // If the keystore password is empty(""), then we have to set // to null, otherwise it won't work!!! char[] nPassword = null; if ((KEYSTORE_PASSWORD == null) || KEYSTORE_PASSWORD.trim().equals("")) { nPassword = null; } else { nPassword = KEYSTORE_PASSWORD.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 enum = ks.aliases(); String keyAlias = null; if (enum.hasMoreElements()) // we are readin just one certificate. { keyAlias = (String)enum.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); } catch (Exception e) { e.printStackTrace(); } }}
0 0
- JAVA从一个.p12或.pfx文件中获取公钥和私钥
- java从 .pfx或.p12文件中获取公钥和私钥
- Java从pfx中获取公私钥
- Java从pfx中获取公私钥
- 从PFX文件中获取私钥、公钥证书、公钥
- p12文件中导出公钥和私钥
- 介绍如何从p12/pfx文件中提取密钥对及其长度
- Android之获取证书文件(pfx格式)的公钥和私钥
- pfx文件解析私钥和公钥
- .NET pfx文件解析私钥和公钥
- openssl从PFX导出私钥、公钥
- openssl从PFX导出私钥、公钥
- openssl从PFX导出私钥、公钥
- 每日一得--*.PFX(*.p12)&个人信息交换文件
- Java加密技术(十二)——*.PFX(*.p12)&个人信息交换文件
- Java加密技术(十二)——*.PFX(*.p12)&个人信息交换文件
- Java加密技术(十二)——*.PFX(*.p12)&个人信息交换文件
- Java加密技术(十二)——*.PFX(*.p12)&个人信息交换文件
- iOS APP多语言开发(国际化)
- c++ getline正确读取多行内容
- Makefile 教程
- 全java代码实现Spring与ActiveMQ整合
- linux常用解压压缩命令
- JAVA从一个.p12或.pfx文件中获取公钥和私钥
- (一)linux常用,但容易忘记的命令
- jenkins配置用户名和密码
- 浏览器 HTTP 协议缓存机制详解
- 利用python爬虫大量基金股票的净值写入到csv文件中方便做其他计算
- 游标
- python+beautifulsoup/xpath实现新浪微博某博主全部高清大图下载器
- dd
- 2016Y FJNU 2016-2017学年第二十届低年级程序设计竞赛(正式赛) Problem E: 杂物整理