KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > crypto > BlowFishCrypt


1 /*
2  * $Id: BlowFishCrypt.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.crypto;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.security.NoSuchAlgorithmException JavaDoc;
33
34 import javax.crypto.Cipher;
35 import javax.crypto.KeyGenerator;
36 import javax.crypto.SecretKey;
37 import javax.crypto.spec.SecretKeySpec;
38
39 /**
40  * Blowfish (Two-Way) Byte/String encryption
41  *
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 2.0
45  */

46 public class BlowFishCrypt {
47
48     private SecretKeySpec secretKeySpec = null;
49
50     /**
51      * Creates a new BlowFishCrypt object.
52      * @param secretKeySpec A SecretKeySpec object.
53      */

54     public BlowFishCrypt(SecretKeySpec secretKeySpec) {
55         this.secretKeySpec = secretKeySpec;
56     }
57
58     /**
59      * Creates a new BlowFishCrypt object.
60      * @param key An encoded secret key
61      */

62     public BlowFishCrypt(byte[] key) {
63         try {
64             secretKeySpec = new SecretKeySpec(key, "Blowfish");
65         } catch (Exception JavaDoc e) {}
66     }
67
68     /**
69      * Creates a new BlowFishCrypt object.
70      * @param keyFile A file object containing the secret key as a String object.
71      */

72     public BlowFishCrypt(File JavaDoc keyFile) {
73         try {
74             FileInputStream JavaDoc is = new FileInputStream JavaDoc(keyFile);
75             ObjectInputStream JavaDoc os = new ObjectInputStream JavaDoc(is);
76             String JavaDoc keyString = (String JavaDoc) os.readObject();
77
78             is.close();
79
80             byte[] keyBytes = keyString.getBytes();
81
82             secretKeySpec = new SecretKeySpec(keyBytes, "Blowfish");
83         } catch (Exception JavaDoc e) {}
84     }
85
86     /**
87      * Encrypt the string with the secret key.
88      * @param string The string to encrypt.
89      */

90     public byte[] encrypt(String JavaDoc string) {
91         return encrypt(string.getBytes());
92     }
93
94     /**
95      * Decrypt the string with the secret key.
96      * @param string The string to decrypt.
97      */

98     public byte[] decrypt(String JavaDoc string) {
99         return decrypt(string.getBytes());
100     }
101
102     /**
103      * Encrypt the byte array with the secret key.
104      * @param bytes The array of bytes to encrypt.
105      */

106     public byte[] encrypt(byte[] bytes) {
107         byte[] resp = null;
108
109         try {
110             resp = crypt(bytes, Cipher.ENCRYPT_MODE);
111         } catch (Exception JavaDoc e) {
112             return null;
113         }
114         return resp;
115     }
116
117     /**
118      * Decrypt the byte array with the secret key.
119      * @param bytes The array of bytes to decrypt.
120      */

121     public byte[] decrypt(byte[] bytes) {
122         byte[] resp = null;
123
124         try {
125             resp = crypt(bytes, Cipher.DECRYPT_MODE);
126         } catch (Exception JavaDoc e) {
127             return null;
128         }
129         return resp;
130     }
131
132     private byte[] crypt(byte[] bytes, int mode) throws Exception JavaDoc {
133         if (secretKeySpec == null)
134             throw new Exception JavaDoc("SecretKey cannot be null.");
135         Cipher cipher = Cipher.getInstance("Blowfish");
136
137         cipher.init(mode, secretKeySpec);
138         return cipher.doFinal(bytes);
139     }
140     
141     public static byte[] generateKey() throws NoSuchAlgorithmException JavaDoc {
142         KeyGenerator keyGen = keyGen = KeyGenerator.getInstance("Blowfish");
143         keyGen.init(448);
144         
145         SecretKey secretKey = keyGen.generateKey();
146         byte[] keyBytes = secretKey.getEncoded();
147         
148         return keyBytes;
149     }
150     
151     public static boolean testKey(byte[] key) {
152         String JavaDoc testString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz";
153         BlowFishCrypt c = new BlowFishCrypt(key);
154         byte[] encryptedBytes = c.encrypt(testString);
155         String JavaDoc encryptedMessage = new String JavaDoc(encryptedBytes);
156                                    
157         byte[] decryptedBytes = c.decrypt(encryptedMessage);
158         String JavaDoc decryptedMessage = new String JavaDoc(decryptedBytes);
159         
160         if (testString.equals(decryptedMessage)) {
161             return true;
162         }
163         
164         return false;
165     }
166
167     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
168         if (args[0] == null) {
169             args[0] = "ofbkey";
170         }
171         
172         byte[] key = generateKey();
173         if (testKey(key)) {
174             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(args[0]);
175             ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(fos);
176             String JavaDoc keyString = new String JavaDoc(key);
177             os.writeObject(keyString);
178             fos.close();
179         }
180     }
181 }
182
Popular Tags