1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.uip;
22
23 import goldengate.common.crypto.Blowfish;
24 import goldengate.common.crypto.Des;
25 import goldengate.common.crypto.KeyObject;
26 import goldengate.common.exception.CryptoException;
27
28 import java.io.BufferedReader;
29 import java.io.DataInputStream;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35
36
37
38
39
40
41
42 public class GgPassword {
43 public static boolean desModel = true;
44 public static boolean clearPasswordView = false;
45 public static String HELPOPTIONS = "Options available\r\n"+
46 "* -ki file to specify the Key File by default\r\n"+
47 "* -ko file to specify a new Key File to build and save\r\n\r\n"+
48 "* -des to specify DES format (default)\r\n"+
49 "* -blf to specify BlowFish format\r\n\r\n"+
50 "* -pi file to specify a GGP File by default(password)\r\n"+
51 "* -pwd to specify a clear ggp password as entry\r\n"+
52 "* -cpwd to specify a crypted ggp password as entry\r\n"+
53 "* -po file to specify a GGP File as output for the password\r\n"+
54 "* -clear to specify uncrypted password shown as clear text";
55 public static String GGPEXTENSION = "ggp";
56 public static String ki = null;
57 public static String ko = null;
58 public static String pi = null;
59 public static String po = null;
60 public static String pwd = null;
61 public static String cpwd = null;
62
63 private File keyFile = null;
64 private File passwordFile = null;
65 private String clearPassword = null;
66 private String cryptedPassword = null;
67
68 private KeyObject currentKey;
69
70
71
72
73
74
75 public static void main(String[] args) throws Exception {
76
77 if (! GgPassword.loadOptions(args)) {
78
79 System.exit(2);
80 }
81 GgPassword ggPassword = new GgPassword();
82 if (po == null && pi == null) {
83
84 System.err.println("Key written");
85 System.exit(0);
86 }
87 if (ggPassword.clearPassword == null || ggPassword.clearPassword.length() == 0) {
88 System.err.println("Password to crypt:");
89 String newp = ggPassword.readString();
90 if (newp == null || newp.length() == 0) {
91 System.err.println("No password as input");
92 System.exit(4);
93 }
94 ggPassword.setClearPassword(newp);
95 if (po != null) {
96 ggPassword.setPasswordFile(new File(po));
97 ggPassword.savePasswordFile();
98 }
99 if (clearPasswordView) {
100 System.err.println("ClearPwd: "+ggPassword.getClearPassword());
101 System.err.println("CryptedPwd: "+ggPassword.getCryptedPassword());
102 }
103 }
104 }
105
106 public static boolean loadOptions(String[] args) {
107 int i = 0;
108 if (args.length == 0) {
109 System.err.println(HELPOPTIONS);
110 return false;
111 }
112 for (i = 0; i < args.length; i++) {
113 if (args[i].equalsIgnoreCase("-ki")) {
114 i++;
115 if (i < args.length) {
116 ki = args[i];
117 } else {
118 System.err.println("-ki needs a file as argument");
119 return false;
120 }
121 } else if (args[i].equalsIgnoreCase("-ko")) {
122 i++;
123 if (i < args.length) {
124 ko = args[i];
125 } else {
126 System.err.println("-ko needs a file as argument");
127 return false;
128 }
129 } else if (args[i].equalsIgnoreCase("-pi")) {
130 i++;
131 if (i < args.length) {
132 pi = args[i];
133 } else {
134 System.err.println("-pi needs a file as argument");
135 return false;
136 }
137 } else if (args[i].equalsIgnoreCase("-po")) {
138 i++;
139 if (i < args.length) {
140 po = args[i];
141 } else {
142 System.err.println("-po needs a file as argument");
143 return false;
144 }
145 } else if (args[i].equalsIgnoreCase("-des")) {
146 desModel = true;
147 } else if (args[i].equalsIgnoreCase("-blf")) {
148 desModel = false;
149 } else if (args[i].equalsIgnoreCase("-pwd")) {
150 i++;
151 if (i < args.length) {
152 pwd = args[i];
153 } else {
154 System.err.println("-pwd needs a password as argument");
155 return false;
156 }
157 } else if (args[i].equalsIgnoreCase("-cpwd")) {
158 i++;
159 if (i < args.length) {
160 cpwd = args[i];
161 } else {
162 System.err.println("-cpwd needs a crypted password as argument");
163 return false;
164 }
165 } else if (args[i].equalsIgnoreCase("-clear")) {
166 clearPasswordView = true;
167 } else {
168 System.err.println("Unknown option: "+args[i]);
169 return false;
170 }
171 }
172 if (ki == null && ko == null) {
173 System.err.println("You must specify one of ki or ko options");
174 return false;
175 }
176 if (ki == null) {
177 ki = ko;
178 }
179 if ((ki == null) && (po != null || pi != null)) {
180 System.err.println("If pi or po options are set, ki or ko options must be set also!\n");
181 return false;
182 }
183 if (pi == null && po == null && (pwd != null || cpwd != null)) {
184 System.err.println("Cannot create a password if no password GGP file is specified with pi or po options");
185 return false;
186 }
187 return true;
188 }
189
190 public GgPassword() throws Exception {
191 if (desModel) {
192 currentKey = new Des();
193 } else {
194 currentKey = new Blowfish();
195 }
196 if (ko != null) {
197 createNewKey();
198 saveKey(new File(ko));
199 }
200 if (ki != null) {
201 loadKey(new File(ki));
202 }
203 if (pi != null) {
204 setPasswordFile(new File(pi));
205 loadPasswordFile();
206 }
207 if (pwd != null) {
208 setClearPassword(pwd);
209 }
210 if (cpwd != null) {
211 setCryptedPassword(cpwd);
212 }
213 if (po != null) {
214 setPasswordFile(new File(po));
215 savePasswordFile();
216 }
217 if (clearPassword != null) {
218 if (clearPasswordView) {
219 System.err.println("ClearPwd: "+getClearPassword());
220 }
221 System.err.println("CryptedPwd: "+getCryptedPassword());
222 }
223 }
224
225 private String readString() {
226 String read = "";
227 InputStreamReader input = new InputStreamReader(System.in);
228 BufferedReader reader = new BufferedReader(input);
229 try {
230 read = reader.readLine();
231 } catch (IOException e) {
232
233 e.printStackTrace();
234 }
235 return read;
236 }
237
238
239
240
241
242 public void createNewKey() throws Exception {
243 try {
244 currentKey.generateKey();
245 } catch (Exception e) {
246 throw new CryptoException("Create New Key in error", e);
247 }
248 if (clearPassword != null) {
249 setClearPassword(clearPassword);
250 }
251 }
252
253
254
255
256
257 public void loadKey(File file) throws CryptoException {
258 keyFile = file;
259 try {
260 currentKey.setSecretKey(file);
261 } catch (IOException e) {
262 throw new CryptoException("Load Key in error", e);
263 }
264 }
265
266
267
268
269
270
271 public void saveKey(File file) throws CryptoException {
272 if (file != null) {
273 keyFile = file;
274 }
275 try {
276 currentKey.saveSecretKey(keyFile);
277 } catch (IOException e) {
278 throw new CryptoException("Save Key in error", e);
279 }
280 }
281
282
283
284
285
286 public boolean keyReady() {
287 return currentKey.keyReady();
288 }
289
290
291
292
293
294 public File getKeyFile() {
295 return keyFile;
296 }
297
298
299
300
301
302
303 public void setClearPassword(String passwd) throws Exception {
304 clearPassword = passwd;
305 cryptedPassword = currentKey.cryptToHex(clearPassword);
306 }
307
308
309
310
311 public File getPasswordFile() {
312 return passwordFile;
313 }
314
315
316
317
318
319 public void setPasswordFile(File passwordFile) {
320 this.passwordFile = passwordFile;
321 }
322
323
324
325
326
327 public void savePasswordFile() throws IOException {
328 FileOutputStream outputStream = new FileOutputStream(passwordFile);
329 outputStream.write(cryptedPassword.getBytes());
330 outputStream.flush();
331 outputStream.close();
332 }
333
334
335
336
337
338 public void loadPasswordFile() throws Exception {
339 if (passwordFile.canRead()) {
340 int len = (int)passwordFile.length();
341 byte []key = new byte[len];
342 FileInputStream inputStream = null;
343 inputStream = new FileInputStream(passwordFile);
344 DataInputStream dis = new DataInputStream(inputStream);
345 dis.readFully(key);
346 dis.close();
347 setCryptedPassword(new String(key));
348 } else {
349 throw new CryptoException("Cannot read crypto file");
350 }
351 }
352
353
354
355 public String getCryptedPassword() {
356 return cryptedPassword;
357 }
358
359
360
361
362
363 public void setCryptedPassword(String cryptedPassword) throws Exception {
364 this.cryptedPassword = cryptedPassword;
365 this.clearPassword = currentKey.decryptHexInString(cryptedPassword);
366 }
367
368
369
370
371 public String getClearPassword() {
372 return clearPassword;
373 }
374
375 }