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