Java读取证书、公钥、私钥


  1. import java.io.ByteArrayOutputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.security.KeyStore;
  6. import java.security.PrivateKey;
  7. import java.security.PublicKey;
  8. import java.security.cert.Certificate;
  9. import java.security.cert.CertificateException;
  10. import java.util.Enumeration;
  11. import com.sun.org.apache.xml.internal.security.utils.Base64;
  12. import com.help.Conf_Info;
  13. public class VerifiSign2
  14. {
  15. public void main(String[] args)
  16. {
  17. getPrivateKeyInfo();
  18. getPublicKeyInfo();
  19. }
  20. /**
  21. * 获取私钥别名等信息
  22. */
  23. public String getPrivateKeyInfo()
  24. {
  25. String privKeyFileString = Conf_Info.PrivatePath;
  26. String privKeyPswdString = "" + Conf_Info.password;
  27. String keyAlias = null;
  28. try
  29. {
  30. KeyStore keyStore = KeyStore.getInstance( "PKCS12");
  31. FileInputStream fileInputStream = new FileInputStream(privKeyFileString);
  32. char[] nPassword = null;
  33. if ((privKeyPswdString == null) || privKeyPswdString.trim().equals( ""))
  34. {
  35. nPassword = null;
  36. } else
  37. {
  38. nPassword = privKeyPswdString.toCharArray();
  39. }
  40. keyStore.load(fileInputStream, nPassword);
  41. fileInputStream.close();
  42. System.out.println( "keystore type=" + keyStore.getType());
  43. Enumeration enumeration = keyStore.aliases();
  44. if (enumeration.hasMoreElements())
  45. {
  46. keyAlias = (String) enumeration.nextElement();
  47. System.out.println( "alias=[" + keyAlias + "]");
  48. }
  49. System.out.println( "is key entry=" + keyStore.isKeyEntry(keyAlias));
  50. PrivateKey prikey = (PrivateKey) keyStore.getKey(keyAlias, nPassword);
  51. Certificate cert = keyStore.getCertificate(keyAlias);
  52. PublicKey pubkey = cert.getPublicKey();
  53. System.out.println( "cert class = " + cert.getClass().getName());
  54. System.out.println( "cert = " + cert);
  55. System.out.println( "public key = " + pubkey);
  56. System.out.println( "private key = " + prikey);
  57. } catch (Exception e)
  58. {
  59. System.out.println(e);
  60. }
  61. return keyAlias;
  62. }
  63. /**
  64. * 获取公钥信息
  65. */
  66. public void getPublicKeyInfo()
  67. {
  68. String tmp0 = Conf_Info.Public_cer;
  69. String tmp1 = "";
  70. try
  71. {
  72. tmp1 = getPublicKey(Conf_Info.PublicCerPath).replaceAll( "\n", "");
  73. System.out.println( "商户公钥字符串:\n" + tmp1);
  74. System.out.println( "\n商户公钥字符给定串:\n" + tmp0);
  75. if (tmp0.equals(tmp1))
  76. System.out.println( "=========");
  77. else
  78. {
  79. System.out.println( "**************");
  80. }
  81. } catch (CertificateException e)
  82. {
  83. e.printStackTrace();
  84. System.out.println(e);
  85. } catch (IOException e)
  86. {
  87. e.printStackTrace();
  88. System.out.println(e);
  89. }
  90. }
  91. /**
  92. * 读取公钥cer
  93. *
  94. * @param path
  95. * .cer文件的路径 如:c:/abc.cer
  96. * @return base64后的公钥串
  97. * @throws IOException
  98. * @throws CertificateException
  99. */
  100. public static String getPublicKey(String path) throws IOException, CertificateException
  101. {
  102. InputStream inStream = new FileInputStream(path);
  103. ByteArrayOutputStream out = new ByteArrayOutputStream();
  104. int ch;
  105. String res = "";
  106. while ((ch = inStream.read()) != - 1)
  107. {
  108. out.write(ch);
  109. }
  110. byte[] result = out.toByteArray();
  111. // res = Base64.byteArrayToBase64(result);
  112. res = Base64.encode(result);
  113. return res;
  114. }
  115. }

你可能感兴趣的:(Java开发)