1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.service.impl;
17
18 import java.io.UnsupportedEncodingException;
19 import java.security.GeneralSecurityException;
20 import java.security.MessageDigest;
21
22 import javax.crypto.Cipher;
23 import javax.crypto.KeyGenerator;
24 import javax.crypto.SecretKey;
25 import javax.crypto.SecretKeyFactory;
26 import javax.crypto.spec.DESKeySpec;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.commons.lang.StringUtils;
30 import org.kuali.rice.core.config.ConfigContext;
31 import org.kuali.rice.core.service.Demonstration;
32 import org.kuali.rice.core.service.EncryptionService;
33
34
35
36
37
38
39 public class DemonstrationGradeEncryptionServiceImpl implements EncryptionService, Demonstration {
40 public final static String ALGORITHM = "DES/ECB/PKCS5Padding";
41 public final static String HASH_ALGORITHM = "SHA";
42
43 private final static String CHARSET = "UTF-8";
44
45 private transient SecretKey desKey;
46
47 private boolean isEnabled = false;
48
49 public DemonstrationGradeEncryptionServiceImpl() throws Exception {
50 if (desKey != null) {
51 throw new RuntimeException("The secret key must be kept secret. Storing it in the Java source code is a really bad idea.");
52 }
53 String key = ConfigContext.getCurrentContextConfig().getProperty("encryption.key");
54 if (!StringUtils.isEmpty(key)) {
55 setSecretKey(key);
56 }
57 }
58
59 public boolean isEnabled() {
60 return isEnabled;
61 }
62
63 public String encrypt(Object valueToHide) throws GeneralSecurityException {
64 checkEnabled();
65
66 if (valueToHide == null) {
67 return "";
68 }
69
70
71 Cipher cipher = Cipher.getInstance(ALGORITHM);
72 cipher.init(Cipher.ENCRYPT_MODE, desKey);
73
74 try {
75
76 byte[] cleartext = valueToHide.toString().getBytes(CHARSET);
77
78
79 byte[] ciphertext = cipher.doFinal(cleartext);
80
81 return new String(Base64.encodeBase64(ciphertext), CHARSET);
82 } catch (Exception e) {
83 throw new RuntimeException(e);
84 }
85
86 }
87
88 public String decrypt(String ciphertext) throws GeneralSecurityException {
89 checkEnabled();
90
91 if (StringUtils.isBlank(ciphertext)) {
92 return "";
93 }
94
95
96 Cipher cipher = Cipher.getInstance(ALGORITHM);
97 cipher.init(Cipher.DECRYPT_MODE, desKey);
98
99 try {
100
101 byte[] encryptedData = Base64.decodeBase64(ciphertext.getBytes(CHARSET));
102
103
104 byte[] cleartext1 = cipher.doFinal(encryptedData);
105 return new String(cleartext1, CHARSET);
106 } catch (UnsupportedEncodingException e) {
107 throw new RuntimeException(e);
108 }
109 }
110
111 public byte[] encryptBytes(byte[] valueToHide) throws GeneralSecurityException {
112 checkEnabled();
113
114 if (valueToHide == null) {
115 return new byte[0];
116 }
117
118
119 Cipher cipher = Cipher.getInstance(ALGORITHM);
120 cipher.init(Cipher.ENCRYPT_MODE, desKey);
121
122
123 byte[] cleartext = valueToHide;
124
125
126 byte[] ciphertext = cipher.doFinal(cleartext);
127
128 return ciphertext;
129 }
130
131 public byte[] decryptBytes(byte[] ciphertext) throws GeneralSecurityException {
132 checkEnabled();
133
134 if (ciphertext == null) {
135 return new byte[0];
136 }
137
138
139 Cipher cipher = Cipher.getInstance(ALGORITHM);
140 cipher.init(Cipher.DECRYPT_MODE, desKey);
141
142
143 byte[] encryptedData = ciphertext;
144
145
146 byte[] cleartext1 = cipher.doFinal(encryptedData);
147 return cleartext1;
148 }
149
150
151
152
153
154
155
156
157
158 public static String generateEncodedKey() throws Exception {
159 KeyGenerator keygen = KeyGenerator.getInstance("DES");
160 SecretKey desKey = keygen.generateKey();
161
162
163 Cipher cipher = Cipher.getInstance(ALGORITHM);
164 cipher.init((Cipher.WRAP_MODE), desKey);
165
166 SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
167 DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class);
168 byte[] rawDesKey = desSpec.getKey();
169
170 return new String(Base64.encodeBase64(rawDesKey));
171 }
172
173 private SecretKey unwrapEncodedKey(String key) throws Exception {
174 KeyGenerator keygen = KeyGenerator.getInstance("DES");
175 SecretKey desKey = keygen.generateKey();
176
177
178 Cipher cipher = Cipher.getInstance(ALGORITHM);
179 cipher.init((Cipher.UNWRAP_MODE), desKey);
180
181 byte[] bytes = Base64.decodeBase64(key.getBytes());
182
183 SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
184
185 DESKeySpec keyspec = new DESKeySpec(bytes);
186 SecretKey k = desFactory.generateSecret(keyspec);
187
188 return k;
189
190 }
191
192
193
194
195
196
197
198 public void setSecretKey(String secretKey) throws Exception {
199 if (!StringUtils.isEmpty(secretKey)) {
200 desKey = this.unwrapEncodedKey(secretKey);
201 isEnabled = true;
202
203 Cipher cipher = Cipher.getInstance(ALGORITHM);
204 cipher.init((Cipher.WRAP_MODE), desKey);
205 }
206 }
207
208
209
210
211
212
213 public String hash(Object valueToHide) throws GeneralSecurityException {
214 if ( valueToHide == null || StringUtils.isEmpty( valueToHide.toString() ) ) {
215 return "";
216 }
217 try {
218 MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM);
219 return new String( Base64.encodeBase64( md.digest( valueToHide.toString().getBytes( CHARSET ) ) ), CHARSET );
220 } catch ( UnsupportedEncodingException ex ) {
221
222 }
223 return "";
224 }
225
226
227
228
229
230 protected void checkEnabled() {
231 if (!isEnabled()) {
232 throw new IllegalStateException("Illegal use of encryption service. Ecryption service is disabled, to enable please configure 'encryption.key'.");
233 }
234 }
235
236
237
238 }