1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.enc;
17
18 import java.util.List;
19 import java.util.Properties;
20 import java.util.SortedSet;
21
22 import org.jasypt.util.text.TextEncryptor;
23 import org.kuali.common.util.Assert;
24 import org.kuali.common.util.PropertyUtils;
25
26 import com.google.common.collect.Sets;
27
28
29
30
31 @Deprecated
32 public final class DefaultEncryptionService implements EncryptionService {
33
34 private final TextEncryptor encryptor;
35
36 public DefaultEncryptionService(TextEncryptor encryptor) {
37 Assert.noNulls(encryptor);
38 this.encryptor = encryptor;
39 }
40
41 @Override
42 public String encrypt(String text) {
43 if (EncUtils.isEncrypted(text)) {
44 return text;
45 }
46 Assert.notEncrypted(text);
47 String encryptedText = encryptor.encrypt(text);
48 return EncUtils.wrap(encryptedText);
49 }
50
51 @Override
52 public String decrypt(String text) {
53 if (!EncUtils.isEncrypted(text)) {
54 return text;
55 }
56 Assert.encrypted(text);
57 String unwrapped = EncUtils.unwrap(text);
58 return encryptor.decrypt(unwrapped);
59 }
60
61
62
63
64 @Override
65 public void decrypt(Properties properties) {
66 List<String> keys = PropertyUtils.getEncryptedKeys(properties);
67 for (String key : keys) {
68 String encrypted = properties.getProperty(key);
69 String decrypted = decrypt(encrypted);
70 properties.setProperty(key, decrypted);
71 }
72 }
73
74
75
76
77 @Override
78 public void encrypt(Properties properties) {
79 SortedSet<String> allKeys = Sets.newTreeSet(properties.stringPropertyNames());
80 SortedSet<String> encKeys = Sets.newTreeSet(PropertyUtils.getEncryptedKeys(properties));
81 SortedSet<String> keys = Sets.newTreeSet(Sets.difference(allKeys, encKeys));
82 for (String key : keys) {
83 String plaintext = properties.getProperty(key);
84 String encrypted = encrypt(plaintext);
85 properties.setProperty(key, encrypted);
86 }
87 }
88
89 public TextEncryptor getEncryptor() {
90 return encryptor;
91 }
92
93 }