001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.security;
066:
067: import com.jcorporate.expresso.core.misc.StringUtil;
068: import com.jcorporate.expresso.kernel.exception.ChainedException;
069: import org.apache.log4j.Logger;
070:
071: /**
072: * This class represents a StringEncryption Interface
073: *
074: * @author Michael Rimov
075: */
076: abstract public class AbstractStringEncryption {
077: private static final String this Class = "com.jcorporate.expresso.core.security.AbstracStringEncryption";
078: private static final String defaultPW = "Expresso Rocks";
079: private byte[] passKey = null;
080: boolean initialized = false;
081: static Logger logCat = Logger
082: .getLogger(AbstractStringEncryption.class);
083:
084: /**
085: * Instance to the parent
086: */
087: private CryptoManager cryptoManager;
088:
089: /**
090: * Default Constructor
091: */
092: public AbstractStringEncryption() {
093: initialized = true;
094: } /* AbstractStringEncryption() */
095:
096: public synchronized void init() throws ChainedException {
097: preparePassKey();
098: }
099:
100: /**
101: * Retrieve the pass key prepped by the base class.
102: *
103: * @return byte[]
104: */
105: protected byte[] getPreparedPassKey() {
106: return passKey;
107: }
108:
109: /**
110: * Implemented by lower levels to destroy the cryptographic managers.
111: */
112: public synchronized void destroy() {
113: }
114:
115: /**
116: * Same as decryptString, but only deals in byte arrays. This method must be
117: * implemented by descendants of this class.
118: *
119: * @param inputData A byte aray of data to decrypt
120: * @return A byte array of decrypted data
121: * @throws ChainedException If there's an error decrypting the data
122: */
123: abstract public byte[] decrypt(byte[] inputData)
124: throws ChainedException;
125:
126: /* decrypt(byte) */
127: /**
128: * Decode a string. Derived classes determine what strength/type of
129: * encoding is done.
130: *
131: * @param inputData A greater than zero length byte array. that was encoded
132: * with the encryptString() routine.
133: * @return the decrypted data in a String format.
134: */
135: public String decryptString(byte[] inputData)
136: throws IllegalStateException {
137: if (initialized == false) {
138: throw new IllegalStateException(
139: "StringEncryptionClass is not initialized!");
140: }
141: try {
142: if (inputData == null) {
143: throw new IllegalArgumentException(
144: "inputData should not be null");
145: }
146: if (inputData.length == 0) {
147: throw new IllegalArgumentException(
148: "inputData should be of length > 0");
149: }
150:
151: return new String(decrypt(inputData));
152: } catch (Exception e) {
153:
154: //If we have a problem with the data, the exception should be logged
155: logCat.error(this Class + ".decryptString(byte)", e);
156:
157: return "";
158: }
159: } /* decryptString(Byte[]) */
160:
161: /**
162: * Same as encryptString, but only deals in byte arrays. This must be implemented
163: * by the descendants of this class.
164: *
165: * @param inputData A byte array to encrypt
166: * @return An encrypted byte array.
167: */
168: abstract public byte[] encrypt(byte[] inputData)
169: throws ChainedException;
170:
171: /* encrypt(byte) */
172: /**
173: * Encode a string. Derived classes determine what strength/type of
174: * encoding is done.
175: *
176: * @param inputData a non-null string.
177: * @return a byte array that is the inputData encoded.
178: */
179: public byte[] encryptString(String inputData)
180: throws IllegalStateException, IllegalArgumentException,
181: ChainedException {
182:
183: if (initialized == false) {
184: final String myName = this Class + ".encryptString";
185: throw new IllegalStateException(myName
186: + ":StringEncryptionClass is not initialized!");
187: }
188: if (inputData == null || inputData.length() == 0) {
189: return "".getBytes();
190: }
191:
192: return encrypt(inputData.getBytes());
193: } /* encryptString(String) */
194:
195: /**
196: * Basic implementation puts a hash of the config manager's key into
197: * the passKey variable.
198: */
199: public void preparePassKey() throws ChainedException {
200: String pw = StringUtil.notNull(this .getCryptoManager()
201: .getCryptoKey());
202:
203: // pw = StringUtil.notNull(ConfigManager.getConfig().getCryptoKey());
204:
205: //Check if this property is set. If not, then set it to the default
206: //static key above.
207: if (pw.length() == 0) {
208: pw = defaultPW;
209: logCat
210: .warn("Didn't find cryptoKey in config file, using default key.");
211: }
212:
213: StringHash sha = new StringHash();
214:
215: //Allocate a 256-bit key
216: passKey = new byte[256 / 8];
217:
218: byte[] hash = sha.produceHash(pw.getBytes());
219:
220: //
221: //The following operation truncates the 160-bit return of the SHA
222: //algorithm down to 128 bits.
223: //
224: for (int i = 0; i < hash.length; i++) {
225: passKey[i] = hash[i];
226: }
227: //
228: //Fill out the rest. Note at best we have only a 160 bit key.
229: //For a lousy salt, I simply repeat the passKey again until we've
230: //filled all the bits.
231: //
232: for (int i = hash.length; i < passKey.length; i++) {
233: passKey[i] = hash[i - hash.length];
234: }
235: }
236:
237: public void setCryptoManager(CryptoManager cryptoManager) {
238: this .cryptoManager = cryptoManager;
239: }
240:
241: public CryptoManager getCryptoManager() {
242: return cryptoManager;
243: } /* preparePassKey() */
244:
245: } /* AbstractStringEncryption */
|