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