1 21 22 package uk.org.primrose.pool.jmx; 23 24 import java.security.*; 25 import java.io.*; 26 import javax.crypto.*; 27 import sun.misc.*; 28 import java.util.*; 29 30 31 public class PoolCryptoTool { 32 public void usuage() { 33 System.out.println("This will encrypt all passwords in a given file, or encrypt single password."); 34 System.out.println("Usuage : java uk.co.landmarkinfo.pool.jmx.PoolCryptoTool -file <full_path_to_poolConfig.properties_file>"); 35 System.exit(0); 38 } 39 40 public static void main (String []args)throws Exception { 41 PoolCryptoTool e = new PoolCryptoTool(); 42 if (args.length != 2) { 43 e.usuage(); 44 } 45 46 if (args[0].equals("-file")) { 47 File f = new File(args[1]); 48 if (f.isFile()) { 49 if (!f.exists()) { 50 System.out.println("Cannot find the file you specified to encrypt !!!\n\n"); 51 e.usuage(); 52 } else { 53 File f2 = new File(f.getParentFile().toString() +"/pool.keys"); 54 if (!f2.exists()) { 55 System.out.println("Cannot find the encryption key file - please run GenKeys before hand !\n\n\n"); 56 e.usuage(); 57 } 58 } 59 60 e.encryptFile(args[1], f.getParentFile().toString() +"/pool.keys"); 61 } else { 62 File f2 = new File(f.toString() +"/pool.keys"); 63 if (!f2.exists()) { 64 System.out.println("Cannot find the encryption key file - please run GenKeys before hand !\n\n\n"); 65 e.usuage(); 66 } else { 67 e.encryptFile(f.toString() +"/poolConfig.properties", f.toString() +"/pool.keys"); 68 } 69 } 70 71 72 73 } else { 74 e.usuage(); 75 String s = e.getEncryptedString(args[1], args[2]); 76 System.out.println("Your encrypted password is : '" +s +"' (no quotes)"); 77 String s1 = e.getDecryptedString(s, args[2]); 78 System.out.println("Testing decryption : '" +s1 +"' (no quotes)"); 79 } 80 } 81 82 public void encryptFile(String file, String keyFile) throws Exception { 83 System.out.println("Encrypting passwords in file : '" +file +"'"); 84 BufferedReader br = new BufferedReader(new FileReader(file)); 85 PrintWriter pw = new PrintWriter(new FileOutputStream(file +".tmp")); 86 String line = ""; 87 String poolName = "admin tool"; 88 while ((line = br.readLine()) != null) { 89 if (!line.trim().startsWith("#") && !line.trim().equals("")) { 90 StringTokenizer st = new StringTokenizer(line, "="); 91 int cnt = st.countTokens(); 92 String key = st.nextToken(); 93 String value = ""; 94 while (st.hasMoreTokens()) { 95 value += (st.nextToken() +"="); 96 } 97 98 if (value.length() != 0) { 99 value = value.substring(0, value.length() -1); 100 } 101 102 if (key.equals("poolName")) { 103 poolName = value; 104 } 105 106 if (key.equals("password") || key.equals("adminPassword")) { 107 if (value == null || value.equals("")) { 108 pw.println(line); 109 } else { 110 System.out.println("\n---- " +poolName +" ----"); 111 pw.println(key +"=" +getEncryptedString(value, keyFile)); 112 System.out.println("Encrypting from '" +value +"' to '" +getEncryptedString(value, keyFile) +"'"); 113 } 114 } else { 115 pw.println(line); 116 } 117 } else { 118 pw.println(line); 119 } 120 } 121 122 br.close(); 123 pw.flush(); 124 pw.close(); 125 126 File f = new File(file); 127 f.delete(); 128 f = new File(file +".tmp"); 129 f.renameTo(new File(file)); 130 new File(file +".tmp").delete(); 131 132 System.out.println("\nDone !"); 133 134 } 135 136 137 public String getEncryptedString(String input, String keyFile)throws Exception { 138 139 Security.addProvider( new com.sun.crypto.provider.SunJCE() ); 140 Key key = null; 141 142 File f = new File(keyFile); 143 FileInputStream fis = new FileInputStream(f); 144 byte[] keyBytes = new byte[(int)f.length()]; 145 fis.read(keyBytes); 146 fis.close(); 147 148 ByteArrayInputStream keyArrayStream = new ByteArrayInputStream(keyBytes); 149 ObjectInputStream keyObjectStream = new ObjectInputStream(keyArrayStream); 150 key= (Key)keyObjectStream.readObject(); 151 152 153 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 154 cipher.init(Cipher.ENCRYPT_MODE, key); 155 156 157 158 byte[]inputBytes = input.getBytes(); 159 160 byte[] outputBytes = cipher.doFinal(inputBytes); 161 162 BASE64Encoder encoder = new BASE64Encoder(); 163 String base64 = encoder.encode(outputBytes); 164 base64 = base64.replaceAll("=", "__EQUALS__"); 165 return base64; 166 } 167 168 public String getDecryptedString(String base64Input, String keyFile)throws Exception { 169 base64Input = base64Input.replaceAll("__EQUALS__", "="); 170 BASE64Decoder encoder = new BASE64Decoder(); 171 byte[] inputBytes = encoder.decodeBuffer(base64Input); 172 173 174 Security.addProvider( new com.sun.crypto.provider.SunJCE() ); 175 Key key = null; 176 177 File f = new File(keyFile); 178 FileInputStream fis = new FileInputStream(f); 179 byte[] keyBytes = new byte[(int)f.length()]; 180 fis.read(keyBytes); 181 fis.close(); 182 183 ByteArrayInputStream keyArrayStream = new ByteArrayInputStream(keyBytes); 184 ObjectInputStream keyObjectStream = new ObjectInputStream(keyArrayStream); 185 key = (Key)keyObjectStream.readObject(); 186 187 188 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 189 cipher.init(Cipher.DECRYPT_MODE, key); 190 191 byte[] outputBytes = cipher.doFinal(inputBytes); 192 193 return new String (outputBytes); 194 } 195 196 } 197 198 199 200 201 | Popular Tags |