在文件中保存/加载私钥和公钥

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在文件中保存/加载私钥和公钥相关的知识,希望对你有一定的参考价值。

Save/Load or.. Write/Read or.. Store/Retrieve Private Key/Public Key to/from disk/file :D
  1. package net.java.edem;
  2.  
  3. import java.io.*;
  4. import java.security.*;
  5. import java.security.spec.*;
  6.  
  7. public class Adam {
  8.  
  9. public static void main(String args[]) {
  10. Adam adam = new Adam();
  11. try {
  12. String path = "C:\Documents and Settings\george\My Documents\workspaces\gsoc09\playground\tmp";
  13.  
  14. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
  15.  
  16. keyGen.initialize(1024);
  17. KeyPair generatedKeyPair = keyGen.genKeyPair();
  18.  
  19. System.out.println("Generated Key Pair");
  20. adam.dumpKeyPair(generatedKeyPair);
  21. adam.SaveKeyPair(path, generatedKeyPair);
  22.  
  23. KeyPair loadedKeyPair = adam.LoadKeyPair(path, "DSA");
  24. System.out.println("Loaded Key Pair");
  25. adam.dumpKeyPair(loadedKeyPair);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. return;
  29. }
  30. }
  31.  
  32. private void dumpKeyPair(KeyPair keyPair) {
  33. PublicKey pub = keyPair.getPublic();
  34. System.out.println("Public Key: " + getHexString(pub.getEncoded()));
  35.  
  36. PrivateKey priv = keyPair.getPrivate();
  37. System.out.println("Private Key: " + getHexString(priv.getEncoded()));
  38. }
  39.  
  40. private String getHexString(byte[] b) {
  41. String result = "";
  42. for (int i = 0; i < b.length; i++) {
  43. result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
  44. }
  45. return result;
  46. }
  47.  
  48. public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
  49. PrivateKey privateKey = keyPair.getPrivate();
  50. PublicKey publicKey = keyPair.getPublic();
  51.  
  52. // Store Public Key.
  53. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
  54. publicKey.getEncoded());
  55. FileOutputStream fos = new FileOutputStream(path + "/public.key");
  56. fos.write(x509EncodedKeySpec.getEncoded());
  57. fos.close();
  58.  
  59. // Store Private Key.
  60. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
  61. privateKey.getEncoded());
  62. fos = new FileOutputStream(path + "/private.key");
  63. fos.write(pkcs8EncodedKeySpec.getEncoded());
  64. fos.close();
  65. }
  66.  
  67. public KeyPair LoadKeyPair(String path, String algorithm)
  68. // Read Public Key.
  69. File filePublicKey = new File(path + "/public.key");
  70. FileInputStream fis = new FileInputStream(path + "/public.key");
  71. byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
  72. fis.read(encodedPublicKey);
  73. fis.close();
  74.  
  75. // Read Private Key.
  76. File filePrivateKey = new File(path + "/private.key");
  77. fis = new FileInputStream(path + "/private.key");
  78. byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
  79. fis.read(encodedPrivateKey);
  80. fis.close();
  81.  
  82. // Generate KeyPair.
  83. KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
  84. encodedPublicKey);
  85. PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
  86.  
  87. encodedPrivateKey);
  88. PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
  89.  
  90. return new KeyPair(publicKey, privateKey);
  91. }
  92. }

以上是关于在文件中保存/加载私钥和公钥的主要内容,如果未能解决你的问题,请参考以下文章

如何读取 .pem 文件以获取私钥和​​公钥

RSA公私钥和签名、验签过程

如何在 microsoft azure vault 中配置 RSA HSM 私钥和公钥?

.pem 文件是不是同时包含私钥和公钥?

获取 X509Certificate2 私钥和公钥

如何使用 web3 创建私钥和公钥?