Java从pfx中获取公私钥
2010-07-23 14:58
253 查看
package com.Jinhill; import java.io.*; import java.util.*; import java.security.*; import java.security.cert.Certificate; 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 & 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; } private 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; } }
相关文章推荐
- java读取pfx格式的证书-并获取公钥私钥
- java从 .pfx或.p12文件中获取公钥和私钥
- JAVA从一个.p12或.pfx文件中获取公钥和私钥
- Android之获取证书文件(pfx格式)的公钥和私钥
- 从PFX文件中获取私钥、公钥证书、公钥
- RSA - Java编程: 从KeyStore中获取私钥与证书
- Java密码学 - 4. 非对称密钥的生成&获取(公私钥)
- java获取证书私钥,并加密签名
- JAVA 获取RSA非对称加密,私钥加密、公钥解密
- Java从pfx中获取公私钥
- java加载(读取)pfx私钥的方法
- java读取pfx或P12格式的个人交换库公私钥
- Java 获取当前时间
- 获取JAVA文件路径
- java获取当前路径
- java反射---获取类型信息
- JAVA 获取Set集合和List集合中相同的元素
- java代码获取当前类类名、方法名
- Java获取request对象
- [置顶] 使用Java-selenium自动化测试技术获取网页数据(以上交所公告信息及监管信息为例)