1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.Base64;
26 import com.liferay.portal.kernel.util.Digester;
27 import com.liferay.portal.kernel.util.DigesterUtil;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.ServerDetector;
30
31 import java.security.Key;
32 import java.security.Provider;
33 import java.security.SecureRandom;
34 import java.security.Security;
35
36 import javax.crypto.Cipher;
37 import javax.crypto.KeyGenerator;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
48 public class Encryptor {
49
50 public static final String ENCODING = Digester.ENCODING;
51
52 public static final String KEY_ALGORITHM = "DES";
53
54 public static final String SUN_PROVIDER_CLASS =
55 "com.sun.crypto.provider.SunJCE";
56
57 public static final String IBM_PROVIDER_CLASS =
58 "com.ibm.crypto.provider.IBMJCE";
59
60 public static final String PROVIDER_CLASS = GetterUtil.getString(
61 SystemProperties.get(Encryptor.class.getName() + ".provider.class"),
62 SUN_PROVIDER_CLASS);
63
64 public static Key generateKey() throws EncryptorException {
65 return generateKey(KEY_ALGORITHM);
66 }
67
68 public static Key generateKey(String algorithm) throws EncryptorException {
69 try {
70 Security.addProvider(getProvider());
71
72 KeyGenerator generator = KeyGenerator.getInstance(algorithm);
73 generator.init(56, new SecureRandom());
74
75 Key key = generator.generateKey();
76
77 return key;
78 }
79 catch (Exception e) {
80 throw new EncryptorException(e);
81 }
82 }
83
84 public static Provider getProvider()
85 throws ClassNotFoundException, IllegalAccessException,
86 InstantiationException {
87
88 Class<?> providerClass = null;
89
90 try {
91 providerClass = Class.forName(PROVIDER_CLASS);
92 }
93 catch (ClassNotFoundException cnfe) {
94 if ((ServerDetector.isWebSphere()) &&
95 (PROVIDER_CLASS.equals(SUN_PROVIDER_CLASS))) {
96
97 if (_log.isWarnEnabled()) {
98 _log.warn(
99 "WebSphere does not have " + SUN_PROVIDER_CLASS +
100 ", using " + IBM_PROVIDER_CLASS + " instead");
101 }
102
103 providerClass = Class.forName(IBM_PROVIDER_CLASS);
104 }
105 else if (System.getProperty("java.vm.vendor").equals(
106 "IBM Corporation")) {
107
108 if (_log.isWarnEnabled()) {
109 _log.warn(
110 "IBM JVM does not have " + SUN_PROVIDER_CLASS +
111 ", using " + IBM_PROVIDER_CLASS + " instead");
112 }
113
114 providerClass = Class.forName(IBM_PROVIDER_CLASS);
115 }
116 else {
117 throw cnfe;
118 }
119 }
120
121 return (Provider)providerClass.newInstance();
122 }
123
124 public static String decrypt(Key key, String encryptedString)
125 throws EncryptorException {
126
127 byte[] encryptedBytes = Base64.decode(encryptedString);
128
129 return decryptUnencodedAsString(key, encryptedBytes);
130 }
131
132 public static byte[] decryptUnencodedAsBytes(Key key, byte[] encryptedBytes)
133 throws EncryptorException {
134
135 try {
136 Security.addProvider(getProvider());
137
138 Cipher cipher = Cipher.getInstance(key.getAlgorithm());
139
140 cipher.init(Cipher.DECRYPT_MODE, key);
141
142 return cipher.doFinal(encryptedBytes);
143 }
144 catch (Exception e) {
145 throw new EncryptorException(e);
146 }
147 }
148
149 public static String decryptUnencodedAsString(
150 Key key, byte[] encryptedBytes)
151 throws EncryptorException {
152
153 try {
154 byte[] decryptedBytes = decryptUnencodedAsBytes(
155 key, encryptedBytes);
156
157 return new String(decryptedBytes, ENCODING);
158 }
159 catch (Exception e) {
160 throw new EncryptorException(e);
161 }
162 }
163
164 public static String digest(String text) {
165 return DigesterUtil.digest(text);
166 }
167
168 public static String digest(String algorithm, String text) {
169 return DigesterUtil.digest(algorithm, text);
170 }
171
172 public static String encrypt(Key key, String plainText)
173 throws EncryptorException {
174
175 byte[] encryptedBytes = encryptUnencoded(key, plainText);
176
177 return Base64.encode(encryptedBytes);
178 }
179
180 public static byte[] encryptUnencoded(Key key, byte[] plainBytes)
181 throws EncryptorException {
182
183 try {
184 Security.addProvider(getProvider());
185
186 Cipher cipher = Cipher.getInstance(key.getAlgorithm());
187
188 cipher.init(Cipher.ENCRYPT_MODE, key);
189
190 return cipher.doFinal(plainBytes);
191 }
192 catch (Exception e) {
193 throw new EncryptorException(e);
194 }
195 }
196
197 public static byte[] encryptUnencoded(Key key, String plainText)
198 throws EncryptorException {
199
200 try {
201 byte[] decryptedBytes = plainText.getBytes(ENCODING);
202
203 return encryptUnencoded(key, decryptedBytes);
204 }
205 catch (Exception e) {
206 throw new EncryptorException(e);
207 }
208 }
209
210 private static Log _log = LogFactory.getLog(Encryptor.class);
211
212 }