1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Base64;
20  import com.liferay.portal.kernel.util.Digester;
21  import com.liferay.portal.kernel.util.DigesterUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.ServerDetector;
24  
25  import java.security.Key;
26  import java.security.Provider;
27  import java.security.SecureRandom;
28  import java.security.Security;
29  
30  import javax.crypto.Cipher;
31  import javax.crypto.KeyGenerator;
32  
33  /**
34   * <a href="Encryptor.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class Encryptor {
39  
40      public static final String ENCODING = Digester.ENCODING;
41  
42      public static final String KEY_ALGORITHM = "DES";
43  
44      public static final String SUN_PROVIDER_CLASS =
45          "com.sun.crypto.provider.SunJCE";
46  
47      public static final String IBM_PROVIDER_CLASS =
48          "com.ibm.crypto.provider.IBMJCE";
49  
50      public static final String PROVIDER_CLASS = GetterUtil.getString(
51          SystemProperties.get(Encryptor.class.getName() + ".provider.class"),
52          SUN_PROVIDER_CLASS);
53  
54      public static Key generateKey() throws EncryptorException {
55          return generateKey(KEY_ALGORITHM);
56      }
57  
58      public static Key generateKey(String algorithm) throws EncryptorException {
59          try {
60              Security.addProvider(getProvider());
61  
62              KeyGenerator generator = KeyGenerator.getInstance(algorithm);
63              generator.init(56, new SecureRandom());
64  
65              Key key = generator.generateKey();
66  
67              return key;
68          }
69          catch (Exception e) {
70              throw new EncryptorException(e);
71          }
72      }
73  
74      public static Provider getProvider()
75          throws ClassNotFoundException, IllegalAccessException,
76                 InstantiationException {
77  
78          Class<?> providerClass = null;
79  
80          try {
81              providerClass = Class.forName(PROVIDER_CLASS);
82          }
83          catch (ClassNotFoundException cnfe) {
84              if ((ServerDetector.isWebSphere()) &&
85                  (PROVIDER_CLASS.equals(SUN_PROVIDER_CLASS))) {
86  
87                  if (_log.isWarnEnabled()) {
88                      _log.warn(
89                          "WebSphere does not have " + SUN_PROVIDER_CLASS +
90                              ", using " + IBM_PROVIDER_CLASS + " instead");
91                  }
92  
93                  providerClass = Class.forName(IBM_PROVIDER_CLASS);
94              }
95              else if (System.getProperty("java.vm.vendor").equals(
96                          "IBM Corporation")) {
97  
98                  if (_log.isWarnEnabled()) {
99                      _log.warn(
100                         "IBM JVM does not have " + SUN_PROVIDER_CLASS +
101                             ", using " + IBM_PROVIDER_CLASS + " instead");
102                 }
103 
104                 providerClass = Class.forName(IBM_PROVIDER_CLASS);
105             }
106             else {
107                 throw cnfe;
108             }
109         }
110 
111         return (Provider)providerClass.newInstance();
112     }
113 
114     public static String decrypt(Key key, String encryptedString)
115         throws EncryptorException {
116 
117         byte[] encryptedBytes = Base64.decode(encryptedString);
118 
119         return decryptUnencodedAsString(key, encryptedBytes);
120     }
121 
122     public static byte[] decryptUnencodedAsBytes(Key key, byte[] encryptedBytes)
123         throws EncryptorException {
124 
125         try {
126             Security.addProvider(getProvider());
127 
128             Cipher cipher = Cipher.getInstance(key.getAlgorithm());
129 
130             cipher.init(Cipher.DECRYPT_MODE, key);
131 
132             return cipher.doFinal(encryptedBytes);
133         }
134         catch (Exception e) {
135             throw new EncryptorException(e);
136         }
137     }
138 
139     public static String decryptUnencodedAsString(
140             Key key, byte[] encryptedBytes)
141         throws EncryptorException {
142 
143         try {
144             byte[] decryptedBytes = decryptUnencodedAsBytes(
145                 key, encryptedBytes);
146 
147             return new String(decryptedBytes, ENCODING);
148         }
149         catch (Exception e) {
150             throw new EncryptorException(e);
151         }
152     }
153 
154     public static String digest(String text) {
155         return DigesterUtil.digest(text);
156     }
157 
158     public static String digest(String algorithm, String text) {
159         return DigesterUtil.digest(algorithm, text);
160     }
161 
162     public static String encrypt(Key key, String plainText)
163         throws EncryptorException {
164 
165         byte[] encryptedBytes = encryptUnencoded(key, plainText);
166 
167         return Base64.encode(encryptedBytes);
168     }
169 
170     public static byte[] encryptUnencoded(Key key, byte[] plainBytes)
171         throws EncryptorException {
172 
173         try {
174             Security.addProvider(getProvider());
175 
176             Cipher cipher = Cipher.getInstance(key.getAlgorithm());
177 
178             cipher.init(Cipher.ENCRYPT_MODE, key);
179 
180             return cipher.doFinal(plainBytes);
181         }
182         catch (Exception e) {
183             throw new EncryptorException(e);
184         }
185     }
186 
187     public static byte[] encryptUnencoded(Key key, String plainText)
188         throws EncryptorException {
189 
190         try {
191             byte[] decryptedBytes = plainText.getBytes(ENCODING);
192 
193             return encryptUnencoded(key, decryptedBytes);
194         }
195         catch (Exception e) {
196             throw new EncryptorException(e);
197         }
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(Encryptor.class);
201 
202 }