1 19 package org.lucane.common.crypto; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 28 public class KeyTool 29 { 30 private static String getKeytoolFullPath() 31 { 32 String javaHome = System.getProperty("java.home"); 33 String keyTool = javaHome + "/bin/keytool"; 34 35 return keyTool.replace('\\', '/'); 36 } 37 38 public static String sixCharsMin(String base) 39 { 40 String my = base; 41 while(my.length() < 6) 42 my += base; 43 44 return my; 45 } 46 47 public static String createPrivateStore(String name, String keyPasswd) 48 throws IOException , InterruptedException 49 { 50 File privateStore = File.createTempFile("private", null); 52 privateStore.delete(); 53 54 String [] cmd = { 56 getKeytoolFullPath(), 57 "-genkey", 58 "-alias", 59 name, 60 "-keystore", 61 privateStore.getPath(), 62 "-storetype", 63 "JKS", 64 "-keyalg", 65 "rsa", 66 "-dname", 67 "CN=" + name + ", OU=, O=, L=, S=, C=", 68 "-storepass", 69 sixCharsMin(name), 70 "-keypass", 71 sixCharsMin(keyPasswd)}; 72 73 Process p = Runtime.getRuntime().exec(cmd); 74 int errno = p.waitFor(); 75 if(errno != 0) 76 throw new IOException ("Unable to generate private store : errno=" + errno); 77 78 InputStream input = new FileInputStream (privateStore); 80 byte[] data = new byte[input.available()]; 81 input.read(data); 82 input.close(); 83 84 privateStore.delete(); 86 return Base64.encode(data); 87 } 88 89 public static String reencodePrivateStore(String privateKey, String name, String oldPasswd, String newPasswd) 90 throws IOException , InterruptedException 91 { 92 File privateStore = File.createTempFile("private", null); 94 OutputStream output = new FileOutputStream (privateStore); 95 output.write(Base64.decode(privateKey)); 96 97 String [] cmd = { 99 getKeytoolFullPath(), 100 "-keypasswd", 101 "-alias", 102 name, 103 "-keystore", 104 privateStore.getPath(), 105 "-keypass", 106 sixCharsMin(oldPasswd), 107 "-new", 108 sixCharsMin(newPasswd)}; 109 Process p = Runtime.getRuntime().exec(cmd); 110 int errno = p.waitFor(); 111 if(errno != 0) 112 throw new IOException ("Unable to change key password : errno=" + errno); 113 114 InputStream input = new FileInputStream (privateStore); 116 byte[] data = new byte[input.available()]; 117 input.read(data); 118 input.close(); 119 120 privateStore.delete(); 122 return Base64.encode(data); 123 } 124 125 public static String createPublicStore(String privateKey, String name) 126 throws IOException , InterruptedException 127 { 128 File privateStore = File.createTempFile("private", null); 130 OutputStream output = new FileOutputStream (privateStore); 131 output.write(Base64.decode(privateKey)); 132 133 File certificate = File.createTempFile("x509", null); 135 String [] certCmd = { 136 getKeytoolFullPath(), 137 "-export", 138 "-alias", 139 name, 140 "-keystore", 141 privateStore.getPath(), 142 "-file", 143 certificate.getPath(), 144 "-storepass", 145 sixCharsMin(name)}; 146 147 Process p = Runtime.getRuntime().exec(certCmd); 148 int errno = p.waitFor(); 149 if(errno != 0) 150 throw new IOException ("Unable to extract certificate : errno=" + errno); 151 152 153 File publicStore = File.createTempFile("public", null); 155 publicStore.delete(); 156 String [] cmd = { 157 getKeytoolFullPath(), 158 "-import", 159 "-noprompt", 160 "-alias", 161 name, 162 "-keystore", 163 publicStore.getPath(), 164 "-file", 165 certificate.getPath(), 166 "-storepass", 167 sixCharsMin(name)}; 168 169 p = Runtime.getRuntime().exec(cmd); 170 errno = p.waitFor(); 171 if(errno != 0) 172 throw new IOException ("Unable to extract public key : errno=" + errno); 173 174 InputStream input = new FileInputStream (publicStore); 176 byte[] data = new byte[input.available()]; 177 input.read(data); 178 input.close(); 179 180 privateStore.delete(); 182 certificate.delete(); 183 publicStore.delete(); 184 return Base64.encode(data); 185 } 186 187 public static void main(String [] args) 188 throws IOException , InterruptedException 189 { 190 String priv = createPrivateStore("admin", "admin"); 191 String pub = createPublicStore(priv, "admin"); 192 System.out.println("priv : " + priv.length()); 193 System.out.println("pub : " + pub.length()); 194 } 195 } | Popular Tags |