001: package org.enhydra.dm.util;
002:
003: import java.security.spec.AlgorithmParameterSpec;
004: import java.util.Properties;
005:
006: import javax.crypto.Cipher;
007: import javax.crypto.SecretKey;
008: import javax.crypto.SecretKeyFactory;
009: import javax.crypto.spec.PBEKeySpec;
010: import javax.crypto.spec.PBEParameterSpec;
011:
012: import org.enhydra.dm.api.exceptions.BaseException;
013: import org.enhydra.dm.api.loggers.Log;
014: import org.enhydra.dm.api.util.DesEncription;
015:
016: import com.lutris.util.Config;
017:
018: /**
019: * Encrypting with DES Using a Pass Phrase This class uses pass phrase (a string of
020: * multiple words) for encryption. Web page:
021: * http://www.exampledepot.com/egs/javax.crypto/PassKey.html?l=rel
022: */
023:
024: public class DesEncriptionImpl implements DesEncription {
025:
026: public Cipher ecipher;
027:
028: public Cipher dcipher;
029:
030: public final String encodeSecretDefault = "SomeStringValue";
031:
032: public final String encodeEncodingDefault = "UTF-8";
033:
034: public String encoding;
035:
036: public String secret;
037:
038: public Log logger;
039:
040: // 8-byte Salt
041: byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
042: (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
043:
044: // Iteration count
045: int iterationCount = 19;
046:
047: /**
048: * @param strPass Secret phrase parameter
049: * @param strPass Encoding parameter
050: * @throws BaseException
051: */
052: public DesEncriptionImpl(String strPass, String strEncoding) {
053:
054: if (strPass != null && !"".equalsIgnoreCase(strPass)) {
055: secret = strPass;
056: }
057:
058: if (strEncoding != null && !"".equalsIgnoreCase(strEncoding)) {
059: encoding = strEncoding;
060: }
061:
062: }
063:
064: public DesEncriptionImpl() {
065:
066: }
067:
068: private void init() throws BaseException {
069: try {
070: // Create the key
071: PBEKeySpec keySpec = new PBEKeySpec(secret.toCharArray(),
072: salt, iterationCount);
073: SecretKey key = SecretKeyFactory.getInstance(
074: "PBEWithMD5AndDES").generateSecret(keySpec);
075: ecipher = Cipher.getInstance(key.getAlgorithm());
076: dcipher = Cipher.getInstance(key.getAlgorithm());
077:
078: // Prepare the parameter to the ciphers
079: AlgorithmParameterSpec paramSpec = new PBEParameterSpec(
080: salt, iterationCount);
081:
082: // Create the ciphers
083: ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
084: dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
085:
086: } catch (Exception e) {
087: throw new BaseException(e.getMessage());
088: }
089:
090: }
091:
092: /**
093: * @param strPass Secret phrase parameter
094: * @throws BaseException
095: */
096: public DesEncriptionImpl(String strPass) {
097: this (strPass, null);
098: }
099:
100: /**
101: * Encrypt the parametar
102: *
103: * @param str parametar to encrypt
104: * @param strEncoding type of encoding
105: * @return encrypted object
106: * @throws BaseException
107: */
108: public String encrypt(String str, String strEncoding)
109: throws BaseException {
110:
111: if (null != logger) {
112: logger.log(Log.DEBUG, "Encrypt method for parameters!");
113: }
114: try {
115:
116: byte[] input = str.getBytes(strEncoding);
117:
118: // Encrypt
119: byte[] encript = ecipher.doFinal(input);
120:
121: // Encode bytes to base64 to get a string
122: return Base64.encodeBytes(encript, 16);
123:
124: } catch (Exception e) {
125: throw new BaseException(e.getMessage());
126: }
127:
128: }
129:
130: /**
131: * Encrypt the parametar
132: *
133: * @param str parametar to encrypt
134: * @return encrypted object
135: * @throws BaseException
136: */
137: public String encrypt(String str) throws BaseException {
138: return encrypt(str, encoding);
139: }
140:
141: /**
142: * Decrypt the parametar
143: *
144: * @param strEncripted encrypted parametar
145: * @param strEncoding type of encoding
146: * @return decrypted object
147: * @throws BaseException
148: */
149: public String decrypt(String strEncrypted, String strEncoding)
150: throws BaseException {
151:
152: if (null != logger) {
153: logger.log(Log.DEBUG, "Decrypt method for parameters!");
154: }
155: try {
156: // Decode base64 to get bytes
157: byte[] input = Base64.decode(strEncrypted, 16);
158:
159: // Decrypt
160: byte[] decrypted = dcipher.doFinal(input);
161:
162: // Decode
163: return new String(decrypted, strEncoding);
164:
165: } catch (Exception e) {
166: throw new BaseException(e.getMessage());
167: }
168:
169: }
170:
171: /**
172: * Decrypt the parametar
173: *
174: * @param strEncripted encrypted parametar
175: * @return decrypted object
176: * @throws BaseException
177: */
178: public String decrypt(String strEncrypted) throws BaseException {
179: return decrypt(strEncrypted, encoding);
180: }
181:
182: public static void main(String[] args) {
183: DesEncriptionImpl de;
184: try {
185: de = new DesEncriptionImpl("SomeStringValue");
186: de.init();
187: String parameter = EnhydraDMConstants.ACTION + "=edit&"
188: + EnhydraDMConstants.DOCUMENT_ID + "=1000000";
189: System.out.println("1. " + parameter);
190: parameter = de.encrypt(parameter);
191: System.out.println("2. " + parameter);
192: parameter = de.decrypt(parameter);
193: System.out.println("3. " + parameter);
194: System.out.println();
195: } catch (BaseException e) {
196:
197: e.printStackTrace();
198: }
199: }
200:
201: public void configure(Config config) throws BaseException {
202: try {
203: this .secret = config.getString("Encode.Secret");
204: this .encoding = config.getString("Encode.Encoding");
205: if (this .secret == null || "".equalsIgnoreCase(this .secret)) {
206: this .secret = encodeSecretDefault;
207: }
208:
209: if (this .encoding == null
210: || "".equalsIgnoreCase(this .encoding)) {
211: this .encoding = encodeEncodingDefault;
212: }
213:
214: init();
215:
216: } catch (Exception e) {
217: throw new BaseException(e.getMessage());
218: }
219: }
220:
221: public void configure(Properties properties) throws BaseException {
222: try {
223: this .secret = properties.getProperty("Encode.Secret");
224: this .encoding = properties.getProperty("Encode.Encoding");
225: // Create the key
226: if (this .secret == null || "".equalsIgnoreCase(this .secret)) {
227: this .secret = encodeSecretDefault;
228: }
229:
230: if (this .encoding == null
231: || "".equalsIgnoreCase(this .encoding)) {
232: this .encoding = encodeEncodingDefault;
233: }
234: init();
235:
236: } catch (Exception e) {
237: throw new BaseException(e.getMessage());
238: }
239: }
240:
241: /**
242: *
243: */
244: public Log getLogger() {
245: return logger;
246: }
247:
248: /**
249: *
250: */
251: public void setLogger(Log logger) {
252: this.logger = logger;
253: }
254:
255: }
|