001: /*
002: * $Id: BlowFishCrypt.java,v 1.1 2003/08/19 17:48:50 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.base.crypto;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.security.NoSuchAlgorithmException;
033:
034: import javax.crypto.Cipher;
035: import javax.crypto.KeyGenerator;
036: import javax.crypto.SecretKey;
037: import javax.crypto.spec.SecretKeySpec;
038:
039: /**
040: * Blowfish (Two-Way) Byte/String encryption
041: *
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.1 $
044: * @since 2.0
045: */
046: public class BlowFishCrypt {
047:
048: private SecretKeySpec secretKeySpec = null;
049:
050: /**
051: * Creates a new BlowFishCrypt object.
052: * @param secretKeySpec A SecretKeySpec object.
053: */
054: public BlowFishCrypt(SecretKeySpec secretKeySpec) {
055: this .secretKeySpec = secretKeySpec;
056: }
057:
058: /**
059: * Creates a new BlowFishCrypt object.
060: * @param key An encoded secret key
061: */
062: public BlowFishCrypt(byte[] key) {
063: try {
064: secretKeySpec = new SecretKeySpec(key, "Blowfish");
065: } catch (Exception e) {
066: }
067: }
068:
069: /**
070: * Creates a new BlowFishCrypt object.
071: * @param file A file object containing the secret key as a String object.
072: */
073: public BlowFishCrypt(File keyFile) {
074: try {
075: FileInputStream is = new FileInputStream(keyFile);
076: ObjectInputStream os = new ObjectInputStream(is);
077: String keyString = (String) os.readObject();
078:
079: is.close();
080:
081: byte[] keyBytes = keyString.getBytes();
082:
083: secretKeySpec = new SecretKeySpec(keyBytes, "Blowfish");
084: } catch (Exception e) {
085: }
086: }
087:
088: /**
089: * Encrypt the string with the secret key.
090: * @param string The string to encrypt.
091: */
092: public byte[] encrypt(String string) {
093: return encrypt(string.getBytes());
094: }
095:
096: /**
097: * Decrypt the string with the secret key.
098: * @param string The string to decrypt.
099: */
100: public byte[] decrypt(String string) {
101: return decrypt(string.getBytes());
102: }
103:
104: /**
105: * Encrypt the byte array with the secret key.
106: * @param bytes The array of bytes to encrypt.
107: */
108: public byte[] encrypt(byte[] bytes) {
109: byte[] resp = null;
110:
111: try {
112: resp = crypt(bytes, Cipher.ENCRYPT_MODE);
113: } catch (Exception e) {
114: return null;
115: }
116: return resp;
117: }
118:
119: /**
120: * Decrypt the byte array with the secret key.
121: * @param bytes The array of bytes to decrypt.
122: */
123: public byte[] decrypt(byte[] bytes) {
124: byte[] resp = null;
125:
126: try {
127: resp = crypt(bytes, Cipher.DECRYPT_MODE);
128: } catch (Exception e) {
129: return null;
130: }
131: return resp;
132: }
133:
134: private byte[] crypt(byte[] bytes, int mode) throws Exception {
135: if (secretKeySpec == null)
136: throw new Exception("SecretKey cannot be null.");
137: Cipher cipher = Cipher.getInstance("Blowfish");
138:
139: cipher.init(mode, secretKeySpec);
140: return cipher.doFinal(bytes);
141: }
142:
143: public static byte[] generateKey() throws NoSuchAlgorithmException {
144: KeyGenerator keyGen = keyGen = KeyGenerator
145: .getInstance("Blowfish");
146: keyGen.init(448);
147:
148: SecretKey secretKey = keyGen.generateKey();
149: byte[] keyBytes = secretKey.getEncoded();
150:
151: return keyBytes;
152: }
153:
154: public static boolean testKey(byte[] key) {
155: String testString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz";
156: BlowFishCrypt c = new BlowFishCrypt(key);
157: byte[] encryptedBytes = c.encrypt(testString);
158: String encryptedMessage = new String(encryptedBytes);
159:
160: byte[] decryptedBytes = c.decrypt(encryptedMessage);
161: String decryptedMessage = new String(decryptedBytes);
162:
163: if (testString.equals(decryptedMessage)) {
164: return true;
165: }
166:
167: return false;
168: }
169:
170: public static void main(String args[]) throws Exception {
171: if (args[0] == null) {
172: args[0] = "ofbkey";
173: }
174:
175: byte[] key = generateKey();
176: if (testKey(key)) {
177: FileOutputStream fos = new FileOutputStream(args[0]);
178: ObjectOutputStream os = new ObjectOutputStream(fos);
179: String keyString = new String(key);
180: os.writeObject(keyString);
181: fos.close();
182: }
183: }
184: }
|