001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.Base64;
024: import com.liferay.portal.kernel.util.Digester;
025: import com.liferay.portal.kernel.util.DigesterUtil;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.ServerDetector;
028:
029: import java.security.Key;
030: import java.security.Provider;
031: import java.security.SecureRandom;
032: import java.security.Security;
033:
034: import javax.crypto.Cipher;
035: import javax.crypto.KeyGenerator;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * <a href="Encryptor.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class Encryptor {
047:
048: public static final String ENCODING = Digester.ENCODING;
049:
050: public static final String KEY_ALGORITHM = "DES";
051:
052: public static final String SUN_PROVIDER_CLASS = "com.sun.crypto.provider.SunJCE";
053:
054: public static final String IBM_PROVIDER_CLASS = "com.ibm.crypto.provider.IBMJCE";
055:
056: public static final String PROVIDER_CLASS = GetterUtil.getString(
057: SystemProperties.get(Encryptor.class.getName()
058: + ".provider.class"), SUN_PROVIDER_CLASS);
059:
060: public static Key generateKey() throws EncryptorException {
061: return generateKey(KEY_ALGORITHM);
062: }
063:
064: public static Key generateKey(String algorithm)
065: throws EncryptorException {
066: try {
067: Security.addProvider(getProvider());
068:
069: KeyGenerator generator = KeyGenerator
070: .getInstance(algorithm);
071: generator.init(56, new SecureRandom());
072:
073: Key key = generator.generateKey();
074:
075: return key;
076: } catch (Exception e) {
077: throw new EncryptorException(e);
078: }
079: }
080:
081: public static Provider getProvider() throws ClassNotFoundException,
082: IllegalAccessException, InstantiationException {
083:
084: Class providerClass = null;
085:
086: try {
087: providerClass = Class.forName(PROVIDER_CLASS);
088: } catch (ClassNotFoundException cnfe) {
089: if ((ServerDetector.isWebSphere())
090: && (PROVIDER_CLASS.equals(SUN_PROVIDER_CLASS))) {
091:
092: if (_log.isWarnEnabled()) {
093: _log.warn("WebSphere does not have "
094: + SUN_PROVIDER_CLASS + ", using "
095: + IBM_PROVIDER_CLASS + " instead");
096: }
097:
098: providerClass = Class.forName(IBM_PROVIDER_CLASS);
099: } else if (System.getProperty("java.vm.vendor").equals(
100: "IBM Corporation")) {
101:
102: if (_log.isWarnEnabled()) {
103: _log.warn("IBM JVM does not have "
104: + SUN_PROVIDER_CLASS + ", using "
105: + IBM_PROVIDER_CLASS + " instead");
106: }
107:
108: providerClass = Class.forName(IBM_PROVIDER_CLASS);
109: } else {
110: throw cnfe;
111: }
112: }
113:
114: return (Provider) providerClass.newInstance();
115: }
116:
117: public static String decrypt(Key key, String encryptedString)
118: throws EncryptorException {
119:
120: byte[] encryptedBytes = Base64.decode(encryptedString);
121:
122: return decryptRaw(key, encryptedBytes);
123: }
124:
125: public static String decryptRaw(Key key, byte[] encryptedBytes)
126: throws EncryptorException {
127:
128: try {
129: Security.addProvider(getProvider());
130:
131: Cipher cipher = Cipher.getInstance(key.getAlgorithm());
132:
133: cipher.init(Cipher.DECRYPT_MODE, key);
134:
135: byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
136:
137: String decryptedString = new String(decryptedBytes,
138: ENCODING);
139:
140: return decryptedString;
141: } catch (Exception e) {
142: throw new EncryptorException(e);
143: }
144: }
145:
146: public static String digest(String text) {
147: return DigesterUtil.digest(text);
148: }
149:
150: public static String digest(String algorithm, String text) {
151: return DigesterUtil.digest(algorithm, text);
152: }
153:
154: public static String encrypt(Key key, String plainText)
155: throws EncryptorException {
156:
157: byte[] encryptedBytes = encryptRaw(key, plainText);
158:
159: return Base64.encode(encryptedBytes);
160: }
161:
162: public static byte[] encryptRaw(Key key, String plainText)
163: throws EncryptorException {
164:
165: try {
166: Security.addProvider(getProvider());
167:
168: Cipher cipher = Cipher.getInstance(key.getAlgorithm());
169:
170: cipher.init(Cipher.ENCRYPT_MODE, key);
171:
172: byte[] decryptedBytes = plainText.getBytes(ENCODING);
173: byte[] encryptedBytes = cipher.doFinal(decryptedBytes);
174:
175: return encryptedBytes;
176: } catch (Exception e) {
177: throw new EncryptorException(e);
178: }
179: }
180:
181: private static Log _log = LogFactory.getLog(Encryptor.class);
182:
183: }
|