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