KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > jmx > PoolCryptoTool


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

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.out.println("OR");
36
//System.out.println("Usuage : java uk.co.landmarkinfo.pool.jmx.PoolCryptoTool -password <password_to_encrypt> <generated_key_file>");
37
System.exit(0);
38     }
39
40     public static void main (String JavaDoc[]args)throws Exception JavaDoc {
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 JavaDoc s = e.getEncryptedString(args[1], args[2]);
76             System.out.println("Your encrypted password is : '" +s +"' (no quotes)");
77             String JavaDoc s1 = e.getDecryptedString(s, args[2]);
78             System.out.println("Testing decryption : '" +s1 +"' (no quotes)");
79         }
80     }
81
82     public void encryptFile(String JavaDoc file, String JavaDoc keyFile) throws Exception JavaDoc {
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 JavaDoc line = "";
87         String JavaDoc 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 JavaDoc key = st.nextToken();
93                 String JavaDoc 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 JavaDoc getEncryptedString(String JavaDoc input, String JavaDoc keyFile)throws Exception JavaDoc{
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 JavaDoc base64 = encoder.encode(outputBytes);
164         base64 = base64.replaceAll("=", "__EQUALS__");
165         return base64;
166     }
167
168     public String JavaDoc getDecryptedString(String JavaDoc base64Input, String JavaDoc keyFile)throws Exception JavaDoc{
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 JavaDoc(outputBytes);
194     }
195
196 }
197
198
199
200
201
Popular Tags