1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.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  /**
42   * <a href="Encryptor.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
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 }