KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > Blowfish


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.base;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.nio.ByteBuffer JavaDoc;
23 import java.security.InvalidKeyException JavaDoc;
24 import java.security.Key JavaDoc;
25 import java.security.NoSuchAlgorithmException JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.crypto.BadPaddingException;
29 import javax.crypto.Cipher;
30 import javax.crypto.IllegalBlockSizeException;
31 import javax.crypto.NoSuchPaddingException;
32 import javax.crypto.spec.SecretKeySpec;
33
34 import org.columba.ristretto.coder.Base64;
35
36 public class Blowfish {
37     private static final Logger JavaDoc LOG = Logger
38         .getLogger("org.columba.util.blowfish"); //$NON-NLS-1$
39

40     private static final byte[] BYTES = { -127, 88, 27, -88, -13, -56, 19, -4,
41         45, 25, 38, 70, -17, 40, 36, -23 };
42
43     private static final Key JavaDoc KEY = new SecretKeySpec(BYTES, "Blowfish"); //$NON-NLS-1$
44

45     public static String JavaDoc encrypt(char[] source) {
46     try {
47         // Create the cipher
48
Cipher blowCipher = Cipher.getInstance("Blowfish"); //$NON-NLS-1$
49

50         // Initialize the cipher for encryption
51
blowCipher.init(Cipher.ENCRYPT_MODE, KEY);
52
53         // Our cleartext as bytes
54
byte[] cleartext = new String JavaDoc(source).getBytes("UTF-8"); //$NON-NLS-1$
55

56         // Encrypt the cleartext
57
byte[] ciphertext = blowCipher.doFinal(cleartext);
58
59         // Return a String representation of the cipher text
60
return Base64.encode(ByteBuffer.wrap(ciphertext)).toString();
61     } catch (InvalidKeyException JavaDoc e) {
62         LOG.severe(e.toString());
63     } catch (NoSuchAlgorithmException JavaDoc e) {
64         LOG.severe(e.toString());
65     } catch (NoSuchPaddingException e) {
66         LOG.severe(e.toString());
67     } catch (UnsupportedEncodingException JavaDoc e) {
68         LOG.severe(e.toString());
69     } catch (IllegalStateException JavaDoc e) {
70         LOG.severe(e.toString());
71     } catch (IllegalBlockSizeException e) {
72         LOG.severe(e.toString());
73     } catch (BadPaddingException e) {
74         LOG.severe(e.toString());
75     }
76
77     return new String JavaDoc();
78     }
79
80     public static char[] decrypt(String JavaDoc source) {
81     try {
82         // Create the cipher
83
Cipher blowCipher = Cipher.getInstance("Blowfish"); //$NON-NLS-1$
84

85         // Initialize the cipher for encryption
86
blowCipher.init(Cipher.DECRYPT_MODE, KEY);
87
88         // Encrypt the cleartext
89
ByteBuffer JavaDoc ciphertext = Base64.decode(source);
90         byte[] cipherArray = new byte[ciphertext.limit()];
91         ciphertext.get(cipherArray);
92
93         // Our cleartext as bytes
94
byte[] cleartext = blowCipher.doFinal(cipherArray);
95
96         // Return a String representation of the cipher text
97
return new String JavaDoc(cleartext, "UTF-8").toCharArray(); //$NON-NLS-1$
98
} catch (InvalidKeyException JavaDoc e) {
99         LOG.severe(e.toString());
100     } catch (NoSuchAlgorithmException JavaDoc e) {
101         LOG.severe(e.toString());
102     } catch (NoSuchPaddingException e) {
103         LOG.severe(e.toString());
104     } catch (UnsupportedEncodingException JavaDoc e) {
105         LOG.severe(e.toString());
106     } catch (IllegalStateException JavaDoc e) {
107         LOG.severe(e.toString());
108     } catch (IllegalBlockSizeException e) {
109         LOG.severe(e.toString());
110     } catch (BadPaddingException e) {
111         LOG.severe(e.toString());
112     }
113
114     return new char[0];
115     }
116 }
Popular Tags