1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.utils;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.security.InvalidKeyException;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24
25 import javax.crypto.Cipher;
26 import javax.crypto.NoSuchPaddingException;
27 import javax.crypto.SecretKey;
28 import javax.crypto.spec.SecretKeySpec;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import sun.misc.BASE64Decoder;
34 import sun.misc.BASE64Encoder;
35
36
37
38
39
40 public class Encryptor {
41
42 private static Log log = LogFactory.getLog(Encryptor.class);
43
44 public static byte[] createMD5Hash(String key) {
45 return createMD5Hash(key.getBytes());
46 }
47
48 public static byte[] createMD5Hash(byte[] key) {
49
50 MessageDigest longHash;
51 try {
52 longHash = MessageDigest.getInstance("MD5");
53 longHash.update(key);
54 return longHash.digest();
55 } catch (NoSuchAlgorithmException e) {
56 log.fatal("Security not available because basic infrastructure to provide it is not available.", e);
57 }
58 return null;
59 }
60
61 private byte[] md5Hash;
62
63 public Encryptor(String password) {
64 this(createMD5Hash(password));
65 }
66
67 public Encryptor(byte[] md5Bytes) {
68
69
70 md5Hash = md5Bytes;
71 }
72
73 public byte[] encrypt(byte[] target) throws Exception {
74 Cipher cipher = createCipher(Cipher.ENCRYPT_MODE);
75
76 byte[] encryptedText = cipher.doFinal(target);
77 ByteArrayOutputStream bos = new ByteArrayOutputStream();
78 new BASE64Encoder().encode(encryptedText, bos);
79 byte[] encodedEncryptedText = bos.toByteArray();
80
81
82 byte[] result = encodedEncryptedText;
83 return result;
84 }
85
86 public byte[] decrypt(byte[] target) throws Exception {
87 Cipher cipher = createCipher(Cipher.DECRYPT_MODE);
88
89 byte[] decodedText = new BASE64Decoder().decodeBuffer(new ByteArrayInputStream(target));
90
91 byte[] result = cipher.doFinal(decodedText);
92
93 return result;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 private Cipher createCipher(int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
116 SecretKey sk = new SecretKeySpec(md5Hash, "AES");
117 Cipher cipher = Cipher.getInstance("AES");
118 cipher.init(mode, sk);
119 return cipher;
120 }
121
122 public static void main(String[] args) throws Exception {
123 test("user/password", "");
124 test("user/password", ".This is my message");
125 test("user/password", "This is my message-");
126 test("user/password", "This is my messagee");
127 test("user/password", "This is my message ");
128 test("userpassword", "This is other message");
129 test("user=password", "This is my message");
130 test("user/password", "This is my message ");
131 test("user/password", "This is other message, and it is far longer than the other ones. The $ ~~ 513443:;[]{} Its encrypted version should be longer as well, if not then it may just be a hashing function and not an actual encryptor");
132
133 String pwd = "my-password";
134 System.out.println("pwd: " + pwd);
135
136 byte[] pwdHash = Encryptor.createMD5Hash(pwd);
137 System.out.println("pwdHash: " + bytesToHex(pwdHash));
138
139 String daemonPwd = pwd + "daemon-location";
140 Encryptor enc = new Encryptor(daemonPwd);
141 System.out.println("daemonPwd: " + daemonPwd);
142
143 System.out.println("daemonPwdHash: " + bytesToHex(enc.md5Hash));
144
145
146
147
148
149
150
151 }
152
153 public static void test(String key, String message) throws Exception {
154 System.out.println("Key: " + key);
155 System.out.println("Message: " + message);
156 Encryptor encrypter = new Encryptor(key);
157 byte[] enc = encrypter.encrypt(message.getBytes());
158 System.out.println("Encrypted: " + bytesToHex(enc));
159 System.out.println("Decrypted: " + encrypter.decrypt(enc) + "\n");
160
161 System.out.println("Key Hash: " + bytesToHex(encrypter.md5Hash));
162
163
164
165
166
167 }
168
169 public static String bytesToHex(String str) {
170 return bytesToHex(str.getBytes());
171 }
172
173 public static String bytesToHex(byte[] b) {
174 char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
175 StringBuffer buf = new StringBuffer();
176 for (int j = 0; j < b.length; j++) {
177 buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
178 buf.append(hexDigit[b[j] & 0x0f]);
179 }
180 return buf.toString();
181 }
182 }