001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Base64;
020    import com.liferay.portal.kernel.util.Digester;
021    import com.liferay.portal.kernel.util.DigesterUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ServerDetector;
024    
025    import java.security.Key;
026    import java.security.Provider;
027    import java.security.SecureRandom;
028    import java.security.Security;
029    
030    import javax.crypto.Cipher;
031    import javax.crypto.KeyGenerator;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class Encryptor {
037    
038            public static final String ENCODING = Digester.ENCODING;
039    
040            public static final String KEY_ALGORITHM = "DES";
041    
042            public static final String SUN_PROVIDER_CLASS =
043                    "com.sun.crypto.provider.SunJCE";
044    
045            public static final String IBM_PROVIDER_CLASS =
046                    "com.ibm.crypto.provider.IBMJCE";
047    
048            public static final String PROVIDER_CLASS = GetterUtil.getString(
049                    SystemProperties.get(Encryptor.class.getName() + ".provider.class"),
050                    SUN_PROVIDER_CLASS);
051    
052            public static Key generateKey() throws EncryptorException {
053                    return generateKey(KEY_ALGORITHM);
054            }
055    
056            public static Key generateKey(String algorithm) throws EncryptorException {
057                    try {
058                            Security.addProvider(getProvider());
059    
060                            KeyGenerator generator = KeyGenerator.getInstance(algorithm);
061                            generator.init(56, new SecureRandom());
062    
063                            Key key = generator.generateKey();
064    
065                            return key;
066                    }
067                    catch (Exception e) {
068                            throw new EncryptorException(e);
069                    }
070            }
071    
072            public static Provider getProvider()
073                    throws ClassNotFoundException, IllegalAccessException,
074                               InstantiationException {
075    
076                    Class<?> providerClass = null;
077    
078                    try {
079                            providerClass = Class.forName(PROVIDER_CLASS);
080                    }
081                    catch (ClassNotFoundException cnfe) {
082                            if ((ServerDetector.isWebSphere()) &&
083                                    (PROVIDER_CLASS.equals(SUN_PROVIDER_CLASS))) {
084    
085                                    if (_log.isWarnEnabled()) {
086                                            _log.warn(
087                                                    "WebSphere does not have " + SUN_PROVIDER_CLASS +
088                                                            ", using " + IBM_PROVIDER_CLASS + " instead");
089                                    }
090    
091                                    providerClass = Class.forName(IBM_PROVIDER_CLASS);
092                            }
093                            else if (System.getProperty("java.vm.vendor").equals(
094                                                    "IBM Corporation")) {
095    
096                                    if (_log.isWarnEnabled()) {
097                                            _log.warn(
098                                                    "IBM JVM does not have " + SUN_PROVIDER_CLASS +
099                                                            ", using " + IBM_PROVIDER_CLASS + " instead");
100                                    }
101    
102                                    providerClass = Class.forName(IBM_PROVIDER_CLASS);
103                            }
104                            else {
105                                    throw cnfe;
106                            }
107                    }
108    
109                    return (Provider)providerClass.newInstance();
110            }
111    
112            public static String decrypt(Key key, String encryptedString)
113                    throws EncryptorException {
114    
115                    byte[] encryptedBytes = Base64.decode(encryptedString);
116    
117                    return decryptUnencodedAsString(key, encryptedBytes);
118            }
119    
120            public static byte[] decryptUnencodedAsBytes(Key key, byte[] encryptedBytes)
121                    throws EncryptorException {
122    
123                    try {
124                            Security.addProvider(getProvider());
125    
126                            Cipher cipher = Cipher.getInstance(key.getAlgorithm());
127    
128                            cipher.init(Cipher.DECRYPT_MODE, key);
129    
130                            return cipher.doFinal(encryptedBytes);
131                    }
132                    catch (Exception e) {
133                            throw new EncryptorException(e);
134                    }
135            }
136    
137            public static String decryptUnencodedAsString(
138                            Key key, byte[] encryptedBytes)
139                    throws EncryptorException {
140    
141                    try {
142                            byte[] decryptedBytes = decryptUnencodedAsBytes(
143                                    key, encryptedBytes);
144    
145                            return new String(decryptedBytes, ENCODING);
146                    }
147                    catch (Exception e) {
148                            throw new EncryptorException(e);
149                    }
150            }
151    
152            public static String digest(String text) {
153                    return DigesterUtil.digest(text);
154            }
155    
156            public static String digest(String algorithm, String text) {
157                    return DigesterUtil.digest(algorithm, text);
158            }
159    
160            public static String encrypt(Key key, String plainText)
161                    throws EncryptorException {
162    
163                    byte[] encryptedBytes = encryptUnencoded(key, plainText);
164    
165                    return Base64.encode(encryptedBytes);
166            }
167    
168            public static byte[] encryptUnencoded(Key key, byte[] plainBytes)
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                            return cipher.doFinal(plainBytes);
179                    }
180                    catch (Exception e) {
181                            throw new EncryptorException(e);
182                    }
183            }
184    
185            public static byte[] encryptUnencoded(Key key, String plainText)
186                    throws EncryptorException {
187    
188                    try {
189                            byte[] decryptedBytes = plainText.getBytes(ENCODING);
190    
191                            return encryptUnencoded(key, decryptedBytes);
192                    }
193                    catch (Exception e) {
194                            throw new EncryptorException(e);
195                    }
196            }
197    
198            private static Log _log = LogFactoryUtil.getLog(Encryptor.class);
199    
200    }