1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.Base64;
28  import com.liferay.portal.kernel.util.Digester;
29  import com.liferay.portal.kernel.util.DigesterUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.ServerDetector;
32  
33  import java.security.Key;
34  import java.security.Provider;
35  import java.security.SecureRandom;
36  import java.security.Security;
37  
38  import javax.crypto.Cipher;
39  import javax.crypto.KeyGenerator;
40  
41  /**
42   * <a href="Encryptor.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class Encryptor {
47  
48      public static final String ENCODING = Digester.ENCODING;
49  
50      public static final String KEY_ALGORITHM = "DES";
51  
52      public static final String SUN_PROVIDER_CLASS =
53          "com.sun.crypto.provider.SunJCE";
54  
55      public static final String IBM_PROVIDER_CLASS =
56          "com.ibm.crypto.provider.IBMJCE";
57  
58      public static final String PROVIDER_CLASS = GetterUtil.getString(
59          SystemProperties.get(Encryptor.class.getName() + ".provider.class"),
60          SUN_PROVIDER_CLASS);
61  
62      public static Key generateKey() throws EncryptorException {
63          return generateKey(KEY_ALGORITHM);
64      }
65  
66      public static Key generateKey(String algorithm) throws EncryptorException {
67          try {
68              Security.addProvider(getProvider());
69  
70              KeyGenerator generator = KeyGenerator.getInstance(algorithm);
71              generator.init(56, new SecureRandom());
72  
73              Key key = generator.generateKey();
74  
75              return key;
76          }
77          catch (Exception e) {
78              throw new EncryptorException(e);
79          }
80      }
81  
82      public static Provider getProvider()
83          throws ClassNotFoundException, IllegalAccessException,
84                 InstantiationException {
85  
86          Class<?> providerClass = null;
87  
88          try {
89              providerClass = Class.forName(PROVIDER_CLASS);
90          }
91          catch (ClassNotFoundException cnfe) {
92              if ((ServerDetector.isWebSphere()) &&
93                  (PROVIDER_CLASS.equals(SUN_PROVIDER_CLASS))) {
94  
95                  if (_log.isWarnEnabled()) {
96                      _log.warn(
97                          "WebSphere does not have " + SUN_PROVIDER_CLASS +
98                              ", using " + IBM_PROVIDER_CLASS + " instead");
99                  }
100 
101                 providerClass = Class.forName(IBM_PROVIDER_CLASS);
102             }
103             else if (System.getProperty("java.vm.vendor").equals(
104                         "IBM Corporation")) {
105 
106                 if (_log.isWarnEnabled()) {
107                     _log.warn(
108                         "IBM JVM does not have " + SUN_PROVIDER_CLASS +
109                             ", using " + IBM_PROVIDER_CLASS + " instead");
110                 }
111 
112                 providerClass = Class.forName(IBM_PROVIDER_CLASS);
113             }
114             else {
115                 throw cnfe;
116             }
117         }
118 
119         return (Provider)providerClass.newInstance();
120     }
121 
122     public static String decrypt(Key key, String encryptedString)
123         throws EncryptorException {
124 
125         byte[] encryptedBytes = Base64.decode(encryptedString);
126 
127         return decryptUnencodedAsString(key, encryptedBytes);
128     }
129 
130     public static byte[] decryptUnencodedAsBytes(Key key, byte[] encryptedBytes)
131         throws EncryptorException {
132 
133         try {
134             Security.addProvider(getProvider());
135 
136             Cipher cipher = Cipher.getInstance(key.getAlgorithm());
137 
138             cipher.init(Cipher.DECRYPT_MODE, key);
139 
140             return cipher.doFinal(encryptedBytes);
141         }
142         catch (Exception e) {
143             throw new EncryptorException(e);
144         }
145     }
146 
147     public static String decryptUnencodedAsString(
148             Key key, byte[] encryptedBytes)
149         throws EncryptorException {
150 
151         try {
152             byte[] decryptedBytes = decryptUnencodedAsBytes(
153                 key, encryptedBytes);
154 
155             return new String(decryptedBytes, ENCODING);
156         }
157         catch (Exception e) {
158             throw new EncryptorException(e);
159         }
160     }
161 
162     public static String digest(String text) {
163         return DigesterUtil.digest(text);
164     }
165 
166     public static String digest(String algorithm, String text) {
167         return DigesterUtil.digest(algorithm, text);
168     }
169 
170     public static String encrypt(Key key, String plainText)
171         throws EncryptorException {
172 
173         byte[] encryptedBytes = encryptUnencoded(key, plainText);
174 
175         return Base64.encode(encryptedBytes);
176     }
177 
178     public static byte[] encryptUnencoded(Key key, byte[] plainBytes)
179         throws EncryptorException {
180 
181         try {
182             Security.addProvider(getProvider());
183 
184             Cipher cipher = Cipher.getInstance(key.getAlgorithm());
185 
186             cipher.init(Cipher.ENCRYPT_MODE, key);
187 
188             return cipher.doFinal(plainBytes);
189         }
190         catch (Exception e) {
191             throw new EncryptorException(e);
192         }
193     }
194 
195     public static byte[] encryptUnencoded(Key key, String plainText)
196         throws EncryptorException {
197 
198         try {
199             byte[] decryptedBytes = plainText.getBytes(ENCODING);
200 
201             return encryptUnencoded(key, decryptedBytes);
202         }
203         catch (Exception e) {
204             throw new EncryptorException(e);
205         }
206     }
207 
208     private static Log _log = LogFactoryUtil.getLog(Encryptor.class);
209 
210 }