001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose;
021:
022: import java.security.*;
023: import java.io.*;
024: import java.util.*;
025: import javax.crypto.*;
026: import sun.misc.*;
027:
028: public class ConfigEncrypter {
029: static String keyfile = null;
030:
031: static BufferedReader br = new BufferedReader(
032: new InputStreamReader(System.in));
033:
034: public static void main(String args[]) throws Exception {
035: start();
036: }
037:
038: public static void start() throws Exception {
039: printMenu();
040:
041: String line = br.readLine();
042: if (line.trim().equals("q")) {
043: br.close();
044: System.exit(0);
045: }
046: while (!(line.trim().equals("1") || line.trim().equals("2"))) {
047: printMenu();
048: line = br.readLine();
049: if (line.trim().equals("q")) {
050: br.close();
051: System.exit(0);
052: }
053:
054: }
055:
056: if (line.trim().equals("1")) {
057: makeKey();
058: } else if (line.trim().equals("2")) {
059: encrpytConfig();
060: }
061: }
062:
063: public static void makeKey() throws Exception {
064: System.out
065: .print("\nEnter the full directory path (eg /usr/local/keys) \nto where you would like the encryption key to be stored :: ");
066: String line = br.readLine();
067: while (!(new File(line).exists() && new File(line)
068: .isDirectory())) {
069: System.out.println("\nFile does not exist ...");
070: System.out
071: .print("Enter the full directory path (eg /usr/local/keys) \nto where you would like the encryption key to be stored :: ");
072: line = br.readLine();
073: }
074:
075: String dir = line;
076:
077: System.out
078: .print("\nEnter the filename (eg mykey.key) of the encryption key to be stored :: ");
079: String filename = dir + File.separator + br.readLine();
080: keyfile = filename;
081:
082: //Security.addProvider( new com.sun.crypto.provider.SunJCE() );
083: KeyGenerator generator = KeyGenerator.getInstance("DES",
084: "SunJCE");
085:
086: //generate a new random key
087: generator.init(56, new SecureRandom());
088: Key key = generator.generateKey();
089:
090: ByteArrayOutputStream keyStore = new ByteArrayOutputStream();
091: ObjectOutputStream keyObjectStream = new ObjectOutputStream(
092: keyStore);
093: keyObjectStream.writeObject(key);
094:
095: byte[] keyBytes = keyStore.toByteArray();
096:
097: FileOutputStream fos = new FileOutputStream(filename);
098: fos.write(keyBytes);
099: fos.flush();
100: fos.close();
101:
102: System.out.print("\nSuccessfully created encryption key : "
103: + filename);
104:
105: start();
106:
107: }
108:
109: public static void encrpytConfig() throws Exception {
110: String bla = "/usr/local/keys/mykey.key";
111: if (keyfile != null)
112: bla = keyfile;
113: System.out
114: .print("\nEnter the full path and filename (eg "
115: + bla
116: + ") \nto where the encryption key is stored :: ");
117: String line = br.readLine();
118: while (!new File(line).exists()) {
119: System.out.println("\nFile does not exist ...");
120: System.out.print("\nEnter the full path and filename (eg "
121: + bla
122: + ") \nto where the encryption key is stored :: ");
123: line = br.readLine();
124: }
125: keyfile = line;
126:
127: System.out
128: .print("\nEnter the full path and filename (eg /usr/tomcat/conf/primrose.config) \nto where the primrose config is :: ");
129: line = br.readLine();
130: while (!new File(line).exists()) {
131: System.out.println("\nFile does not exist ...");
132: System.out
133: .print("\nEnter the full path and filename (eg /usr/tomcat/conf/primrose.config) \nto where the primrose config is :: ");
134: line = br.readLine();
135: }
136:
137: encryptFile(line, keyfile);
138:
139: }
140:
141: public static void encryptFile(String file, String keyFile)
142: throws Exception {
143: System.out.println("Encrypting passwords in file : '" + file
144: + "'");
145: BufferedReader br = new BufferedReader(new FileReader(file));
146: PrintWriter pw = new PrintWriter(new FileOutputStream(file
147: + ".tmp"));
148: String line = "";
149: String poolName = "admin tool";
150: while ((line = br.readLine()) != null) {
151: if (!line.trim().startsWith("#") && !line.trim().equals("")) {
152: StringTokenizer st = new StringTokenizer(line, "=");
153: int cnt = st.countTokens();
154: String key = st.nextToken();
155: String value = "";
156: while (st.hasMoreTokens()) {
157: value += (st.nextToken() + "=");
158: }
159:
160: if (value.length() != 0) {
161: value = value.substring(0, value.length() - 1);
162: }
163:
164: if (key.equals("poolName")) {
165: poolName = value;
166: }
167:
168: if (key.equals("password")) {
169: if (value == null || value.equals("")) {
170: pw.println(line);
171: } else {
172: System.out.println("\n---- " + poolName
173: + " ----");
174: pw.println(key + "="
175: + getEncryptedString(value, keyFile));
176: System.out.println("Encrypting from '" + value
177: + "' to '"
178: + getEncryptedString(value, keyFile)
179: + "'");
180: }
181: } else {
182: pw.println(line);
183: }
184: } else {
185: pw.println(line);
186: }
187: }
188:
189: br.close();
190: pw.flush();
191: pw.close();
192:
193: File f = new File(file);
194: f.delete();
195: f = new File(file + ".tmp");
196: f.renameTo(new File(file));
197: new File(file + ".tmp").delete();
198:
199: System.out.println("\nDone !");
200:
201: }
202:
203: public static String getEncryptedString(String input, String keyFile)
204: throws Exception {
205:
206: //Security.addProvider( new com.sun.crypto.provider.SunJCE() );
207: Key key = null;
208:
209: File f = new File(keyFile);
210: FileInputStream fis = new FileInputStream(f);
211: byte[] keyBytes = new byte[(int) f.length()];
212: fis.read(keyBytes);
213: fis.close();
214:
215: ByteArrayInputStream keyArrayStream = new ByteArrayInputStream(
216: keyBytes);
217: ObjectInputStream keyObjectStream = new ObjectInputStream(
218: keyArrayStream);
219: key = (Key) keyObjectStream.readObject();
220:
221: Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
222: cipher.init(Cipher.ENCRYPT_MODE, key);
223:
224: byte[] inputBytes = input.getBytes();
225:
226: byte[] outputBytes = cipher.doFinal(inputBytes);
227:
228: BASE64Encoder encoder = new BASE64Encoder();
229: String base64 = encoder.encode(outputBytes);
230: return base64;
231: }
232:
233: public static String getDecryptedString(String base64Input,
234: String keyFile) throws Exception {
235: BASE64Decoder encoder = new BASE64Decoder();
236: byte[] inputBytes = encoder.decodeBuffer(base64Input);
237:
238: //Security.addProvider( new com.sun.crypto.provider.SunJCE() );
239: Key key = null;
240:
241: File f = new File(keyFile);
242: FileInputStream fis = new FileInputStream(f);
243: byte[] keyBytes = new byte[(int) f.length()];
244: fis.read(keyBytes);
245: fis.close();
246:
247: ByteArrayInputStream keyArrayStream = new ByteArrayInputStream(
248: keyBytes);
249: ObjectInputStream keyObjectStream = new ObjectInputStream(
250: keyArrayStream);
251: key = (Key) keyObjectStream.readObject();
252:
253: Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
254: cipher.init(Cipher.DECRYPT_MODE, key);
255:
256: byte[] outputBytes = cipher.doFinal(inputBytes);
257:
258: return new String(outputBytes);
259: }
260:
261: public static void printMenu() {
262: System.out.println("\n\n\n\n\n------- MENU -------");
263: System.out
264: .println("1) Create an encryption key for your pools.");
265: System.out.println("2) Encrypt your primrose config files.");
266: System.out
267: .print("\nChoose an option (enter '1' or '2' or 'q' to quit) :: ");
268: }
269: }
|