01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19: package org.lucane.common.crypto;
20:
21: import javax.crypto.*;
22: import javax.crypto.spec.*;
23: import sun.misc.*;
24:
25: public class BlowFish {
26: public static String cipher(String key, String str)
27: throws CryptoException {
28: if (key.length() > 16)
29: key = key.substring(0, 16);
30:
31: SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),
32: "BlowFish");
33: Cipher cipher;
34: byte[] encrypted;
35:
36: try {
37: cipher = Cipher.getInstance("BlowFish");
38: cipher.init(Cipher.ENCRYPT_MODE, secretKey);
39: encrypted = cipher.doFinal(str.getBytes());
40: } catch (Exception e) {
41: throw new CryptoException(e);
42: }
43:
44: return new BASE64Encoder().encode(encrypted);
45: }
46:
47: public static String decipher(String key, String str)
48: throws CryptoException {
49: if (key.length() > 16)
50: key = key.substring(0, 16);
51:
52: SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),
53: "BlowFish");
54: Cipher cipher;
55: byte[] decrypted;
56:
57: try {
58: cipher = Cipher.getInstance("BlowFish");
59: cipher.init(Cipher.DECRYPT_MODE, secretKey);
60: decrypted = cipher.doFinal(new BASE64Decoder()
61: .decodeBuffer(str));
62: } catch (Exception e) {
63: throw new CryptoException(e);
64: }
65:
66: return new String(decrypted);
67: }
68:
69: public static void main(String[] args) throws Exception {
70: System.out.println(cipher("guest", "str"));
71: }
72: }
|