Posted By

geekiuscaesar on 08/16/09


Tagged

java JCE


Versions (?)

Save/Load Private and Public Key to/from a file


 / Published in: Java
 

URL: http://java.sun.com/docs/books/tutorial/security/apisign/vstep2.html

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. }

Report this snippet  

You need to login to post a comment.