This project has retired. For details please refer to its
Attic page.
TripleDESCrytor xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.v3.client.cryptor;
17
18 import java.security.InvalidAlgorithmParameterException;
19 import java.security.InvalidKeyException;
20 import java.security.NoSuchAlgorithmException;
21 import java.security.spec.KeySpec;
22 import javax.crypto.BadPaddingException;
23 import javax.crypto.Cipher;
24 import javax.crypto.IllegalBlockSizeException;
25 import javax.crypto.KeyGenerator;
26 import javax.crypto.NoSuchPaddingException;
27 import javax.crypto.SecretKey;
28 import javax.crypto.SecretKeyFactory;
29 import javax.crypto.spec.DESedeKeySpec;
30 import org.apache.commons.codec.binary.Base64;
31
32
33
34
35
36 public class TripleDESCrytor implements Cryptor {
37
38 private static final String UNICODE_FORMAT = "UTF8";
39 private static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
40 private KeySpec ks;
41 private SecretKeyFactory skf;
42 private Cipher cipher;
43 byte[] arrayBytes;
44 private String myEncryptionKey;
45 private String myEncryptionScheme;
46 SecretKey key;
47
48
49
50
51
52 public TripleDESCrytor() throws Exception {
53 String keyfromfile = CryptorFactory.loadKeyFromFile("TripleDESCrytor");
54 if (keyfromfile!=null)
55 myEncryptionKey=keyfromfile;
56 else
57 myEncryptionKey = "rioTEBCe/RAHRs6tTyYxDqettnVbZA6z";
58 myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
59 arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
60 ks = new DESedeKeySpec(arrayBytes);
61 skf = SecretKeyFactory.getInstance(myEncryptionScheme);
62 cipher = Cipher.getInstance(myEncryptionScheme);
63 key = skf.generateSecret(ks);
64 }
65
66 @Override
67 public String encrypt(String clear) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
68 String encryptedString = null;
69 try {
70 cipher.init(Cipher.ENCRYPT_MODE, key);
71 byte[] plainText = clear.getBytes(UNICODE_FORMAT);
72 byte[] encryptedText = cipher.doFinal(plainText);
73 encryptedString = new String(Base64.encodeBase64(encryptedText), UNICODE_FORMAT);
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 return encryptedString;
78 }
79
80
81
82
83
84 public static String GEN() {
85 KeyGenerator kgen;
86 try {
87 kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
88 kgen.init(168);
89 SecretKey skey = kgen.generateKey();
90 byte[] raw = skey.getEncoded();
91 return new String(Base64.encodeBase64(raw),UNICODE_FORMAT);
92 } catch (Exception ex) {
93 ex.printStackTrace();;
94 }
95 return null;
96 }
97
98 @Override
99 public String decrypt(String str) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
100 String encryptedString = str;
101 try {
102 cipher.init(Cipher.DECRYPT_MODE, key);
103 byte[] encryptedText = Base64.decodeBase64(str.getBytes(UNICODE_FORMAT));
104 byte[] plainTest = cipher.doFinal(encryptedText);
105 encryptedString = new String(plainTest, UNICODE_FORMAT);
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
109 return encryptedString;
110 }
111
112 @Override
113 public String newKey() {
114 return GEN();
115 }
116 }