1 /**
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * If it is not possible or desirable to put the notice in a particular
7 * file, then You may include the notice in a location (such as a LICENSE
8 * file in a relevant directory) where a recipient would be likely to look
9 * for such a notice.
10 *
11 *
12 */
13 /* ---------------------------------------------------------------------------
14 * U.S. Government, Department of the Army
15 * Army Materiel Command
16 * Research Development Engineering Command
17 * Communications Electronics Research Development and Engineering Center
18 * ---------------------------------------------------------------------------
19 */
20 package org.miloss.fgsms.common;
21
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24
25 /**
26 * Command line utility to encrypt passwords and run database
27 * connectivity tests for command line aux services (non pooled
28 * connections)
29 *
30 * @author AO
31 */
32 public class Main {
33
34 public static void main(String[] args) {
35 if (args==null || args.length == 0) {
36 PrintUsage();
37 } else if (args.length == 1 && args[0].equalsIgnoreCase("en")) {
38
39 try {
40 System.out.print("Enter password: ");
41 String s = new String(System.console().readPassword());
42 System.out.print("Enter password to confirm: ");
43 String s2 = new String(System.console().readPassword());
44 if (s.equalsIgnoreCase(s2)) {
45 //String s = System.console().readLine();
46 System.out.println("Cipher Text: " + AES.EN(s));
47 } else {
48 System.out.println("Passwords do not match");
49 }
50 } catch (Exception ex) {
51 System.out.println("Error caught encrypting string. This usually indicates that you have not yet installed the unlimited strenth Java Crypto Extensions. This can be downloaded at http://www.oracle.com/technetwork/java/javase/downloads/index.html " + ex.getMessage());
52 ex.printStackTrace();
53 }
54 } else if (args.length >= 1 && args[0].equalsIgnoreCase("gen")) {
55
56 short keysize = 256;
57 if (args.length > 1) {
58 try {
59 keysize = Short.parseShort(args[1]);
60 } catch (Exception ex) {
61 ex.printStackTrace();
62 }
63 }
64 String key = AES.GEN(keysize);
65 if (key != null) {
66 System.out.println(key);
67 } else {
68 System.out.println("Unable to generate key. This usually indicates that you have not yet installed the unlimited strenth Java Crypto Extensions. This can be downloaded at http://www.oracle.com/technetwork/java/javase/downloads/index.html ");
69 }
70
71 } else {
72 PrintUsage();
73 }
74
75 }
76
77 private static void PrintUsage() {
78 System.out.println("Usage ....");
79 System.out.println("java -jar fgsms.Common.jar en\t\tThis will encrypt a password using AES.");
80 System.out.println("java -jar fgsms.Common.jar gen\t\tThis will generate a new 256 bit encryption key.");
81 System.out.println("java -jar fgsms.Common.jar gen (size)\t\tThis will generate a new (size) bit encryption key.");
82 //System.out.println("java -jar fgsms.Common.jar de \"someString\" This will decrypt a password using AES.");
83 // System.out.println("java -jar fgsms.Common.jar md5 \"someString\" This will hash a password using MD5.");
84 }
85 /*
86 * public static String getMD5(String input) { try { MessageDigest md =
87 * MessageDigest.getInstance("MD5"); byte[] messageDigest =
88 * md.digest(input.getBytes()); BigInteger number = new BigInteger(1,
89 * messageDigest); String hashtext = number.toString(16); // Now we need to
90 * zero pad it if you actually want the full 32 chars. while
91 * (hashtext.length() < 32) { hashtext = "0" + hashtext; } return hashtext;
92 * } catch (Exception e) { System.out.println("Error caught performing md5
93 * calculation: " + e.getMessage()); } return ""; }
94 */
95 }