Java读取证书、公钥、私钥
2015-12-19 21:16
621 查看
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.util.Enumeration; import com.sun.org.apache.xml.internal.security.utils.Base64; import com.help.Conf_Info; public class VerifiSign2 { public void main(String[] args) { getPrivateKeyInfo(); getPublicKeyInfo(); } /** * 获取私钥别名等信息 */ public String getPrivateKeyInfo() { String privKeyFileString = Conf_Info.PrivatePath; String privKeyPswdString = "" + Conf_Info.password; String keyAlias = null; try { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream fileInputStream = new FileInputStream(privKeyFileString); char[] nPassword = null; if ((privKeyPswdString == null) || privKeyPswdString.trim().equals("")) { nPassword = null; } else { nPassword = privKeyPswdString.toCharArray(); } keyStore.load(fileInputStream, nPassword); fileInputStream.close(); System.out.println("keystore type=" + keyStore.getType()); Enumeration<string> enumeration = keyStore.aliases(); if (enumeration.hasMoreElements()) { keyAlias = (String) enumeration.nextElement(); System.out.println("alias=[" + keyAlias + "]"); } System.out.println("is key entry=" + keyStore.isKeyEntry(keyAlias)); PrivateKey prikey = (PrivateKey) keyStore.getKey(keyAlias, nPassword); Certificate cert = keyStore.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) { System.out.println(e); } return keyAlias; } /** * 获取公钥信息 */ public void getPublicKeyInfo() { String tmp0 = Conf_Info.Public_cer; String tmp1 = ""; try { tmp1 = getPublicKey(Conf_Info.PublicCerPath).replaceAll("\n", ""); System.out.println("商户公钥字符串:\n" + tmp1); System.out.println("\n商户公钥字符给定串:\n" + tmp0); if (tmp0.equals(tmp1)) System.out.println("========="); else { System.out.println("**************"); } } catch (CertificateException e) { e.printStackTrace(); System.out.println(e); } catch (IOException e) { e.printStackTrace(); System.out.println(e); } } /** * 读取公钥cer * * @param path * .cer文件的路径 如:c:/abc.cer * @return base64后的公钥串 * @throws IOException * @throws CertificateException */ public static String getPublicKey(String path) throws IOException, CertificateException { InputStream inStream = new FileInputStream(path); ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch; String res = ""; while ((ch = inStream.read()) != -1) { out.write(ch); } byte[] result = out.toByteArray(); // res = Base64.byteArrayToBase64(result); res = Base64.encode(result); return res; } }
[/code]
相关文章推荐
- Java中九大内置对象
- Java基础:死锁示例
- Struts 2 基本语法
- JAVA 清理垃圾文件简单实现
- Jdk1.7环境变量的配置
- java中的异常
- java(九)双重for循环
- java 注解
- 实战WEB 服务器(JAVA编写WEB服务器)
- java实现简单web服务器(分析+源代码)
- Java中常见的5种WEB服务器介绍
- Struts2基于注解的Action配置
- 一个例子让你了解Java反射机制
- Mac eclipse 连接 手机调试
- JAVA的Date类与Calendar类【转】
- 正则表达式
- PAT乙级测试题及JAVA参考例程
- 【Java EE 学习 78 上】【数据采集系统第十天】【Service使用Spring缓存模块】
- ArrayList LinkList Vector
- java web session+cookie实现用户自动登录