1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.crypto;
22
23 import goldengate.common.digest.FilesystemBasedDigest;
24 import goldengate.common.exception.CryptoException;
25 import goldengate.common.logging.GgInternalLogger;
26 import goldengate.common.logging.GgInternalLoggerFactory;
27
28 import java.io.DataInputStream;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.security.Key;
34
35 import javax.crypto.Cipher;
36 import javax.crypto.KeyGenerator;
37 import javax.crypto.spec.SecretKeySpec;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public abstract class KeyObject {
61
62
63
64 private static final GgInternalLogger logger = GgInternalLoggerFactory
65 .getLogger(KeyObject.class);
66
67
68
69
70 Key secretKey = null;
71
72
73
74
75 public KeyObject() {
76 }
77
78
79
80
81
82 public abstract String getAlgorithm();
83
84
85
86
87 public abstract String getInstance();
88
89
90
91
92 public abstract int getKeySize();
93
94
95
96 public Key getSecretKey() {
97 return secretKey;
98 }
99
100
101
102
103
104 public boolean keyReady() {
105 return secretKey != null;
106 }
107
108
109
110
111
112 public byte[] getSecretKeyInBytes() {
113 if (keyReady()) {
114 return secretKey.getEncoded();
115 } else {
116 return null;
117 }
118 }
119
120
121
122
123
124
125 public void setSecretKey(Key secretKey) {
126 this.secretKey = secretKey;
127 }
128
129
130
131
132 public void setSecretKey(byte[] keyData) {
133 secretKey = new SecretKeySpec(keyData, getAlgorithm());
134 }
135
136
137
138
139
140
141
142 public void setSecretKey(File file) throws CryptoException, IOException {
143 if (file.canRead()) {
144 int len = (int)file.length();
145 byte []key = new byte[len];
146 FileInputStream inputStream = null;
147 inputStream = new FileInputStream(file);
148 DataInputStream dis = new DataInputStream(inputStream);
149 try {
150 dis.readFully(key);
151 } finally {
152 dis.close();
153 }
154 this.setSecretKey(key);
155 } else {
156 throw new CryptoException("Cannot read crypto file");
157 }
158 }
159
160
161
162
163
164
165 public void saveSecretKey(File file) throws CryptoException, IOException {
166 if (keyReady() && ((!file.exists()) || file.canWrite())) {
167 byte []key = getSecretKeyInBytes();
168 FileOutputStream outputStream = new FileOutputStream(file);
169 try {
170 outputStream.write(key);
171 outputStream.flush();
172 } finally {
173 outputStream.close();
174 }
175 } else {
176 throw new CryptoException("Cannot read crypto file");
177 }
178 }
179
180
181
182
183
184 public void generateKey() throws Exception {
185 try {
186 KeyGenerator keyGen = KeyGenerator.getInstance(getAlgorithm());
187 keyGen.init(getKeySize());
188 secretKey = keyGen.generateKey();
189 } catch (Exception e) {
190 logger.warn("GenerateKey Error", e);
191 throw e;
192 }
193 }
194
195
196
197
198
199
200 public Cipher toCrypt() {
201 Cipher cipher;
202 try {
203 cipher = Cipher.getInstance(getInstance());
204 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
205 } catch (Exception e) {
206 return null;
207 }
208 return cipher;
209 }
210
211
212
213
214
215
216
217 public byte[] crypt(byte[] plaintext) throws Exception {
218 if (! keyReady()) {
219 throw new CryptoException("Key not Ready");
220 }
221 try {
222 Cipher cipher = Cipher.getInstance(getInstance());
223 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
224 return cipher.doFinal(plaintext);
225 } catch (Exception e) {
226 logger.warn("Crypt Error", e);
227 throw e;
228 }
229 }
230
231
232
233
234
235
236
237
238 public String cryptToHex(byte[] plaintext) throws Exception {
239 byte []result = crypt(plaintext);
240 return encodeHex(result);
241 }
242
243
244
245
246
247
248
249
250 public byte[] crypt(String plaintext) throws Exception {
251 return crypt(plaintext.getBytes());
252 }
253
254
255
256
257
258
259
260
261 public String cryptToHex(String plaintext) throws Exception {
262 return cryptToHex(plaintext.getBytes());
263 }
264
265
266
267
268
269
270 public Cipher toDecrypt() {
271 Cipher cipher;
272 try {
273 cipher = Cipher.getInstance(getAlgorithm());
274 cipher.init(Cipher.DECRYPT_MODE, secretKey);
275 } catch (Exception e) {
276 return null;
277 }
278 return cipher;
279 }
280
281
282
283
284
285
286
287 public byte[] decrypt(byte[] ciphertext) throws Exception {
288 if (! keyReady()) {
289 throw new CryptoException("Key not Ready");
290 }
291 try {
292 Cipher cipher = Cipher.getInstance(getAlgorithm());
293 cipher.init(Cipher.DECRYPT_MODE, secretKey);
294 return cipher.doFinal(ciphertext);
295 } catch (Exception e) {
296 logger.warn("Decrypt Error", e);
297 throw e;
298 }
299 }
300
301
302
303
304
305
306
307
308 public String decryptInString(byte[] ciphertext) throws Exception {
309 return new String(decrypt(ciphertext));
310 }
311
312
313
314
315
316
317
318
319
320 public byte[] decryptHexInBytes(String ciphertext) throws Exception {
321 byte[] arrayBytes = decodeHex(ciphertext);
322 return decrypt(arrayBytes);
323 }
324
325
326
327
328
329
330
331
332 public byte[] decryptHexInBytes(byte[] ciphertext) throws Exception {
333 byte[] arrayBytes = decodeHex(new String(ciphertext));
334 return decrypt(arrayBytes);
335 }
336
337
338
339
340
341
342
343
344 public String decryptHexInString(String ciphertext) throws Exception {
345 return new String(decryptHexInBytes(ciphertext));
346 }
347
348
349
350
351
352
353
354 public byte[] decryptHexFile(File file) throws Exception {
355 byte [] byteKeys = new byte[(int) file.length()];
356 FileInputStream inputStream = null;
357 DataInputStream dis = null;
358 try {
359 inputStream = new FileInputStream(file);
360 dis = new DataInputStream(inputStream);
361 dis.readFully(byteKeys);
362 dis.close();
363 String skey = new String(byteKeys);
364
365 byteKeys = decryptHexInBytes(skey);
366 return byteKeys;
367 } catch (IOException e) {
368 try {
369 if (dis != null) {
370 dis.close();
371 } else if (inputStream != null)
372 inputStream.close();
373 } catch (IOException e1) {
374 }
375 throw e;
376 }
377 }
378
379
380
381
382
383
384 public byte[] decodeHex(String encoded) {
385 return FilesystemBasedDigest.getFromHex(encoded);
386 }
387
388
389
390
391
392
393 public String encodeHex(byte[] bytes) {
394 return FilesystemBasedDigest.getHex(bytes);
395 }
396 }