001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.core.base;
020:
021: import java.io.UnsupportedEncodingException;
022: import java.nio.ByteBuffer;
023: import java.security.InvalidKeyException;
024: import java.security.Key;
025: import java.security.NoSuchAlgorithmException;
026: import java.util.logging.Logger;
027:
028: import javax.crypto.BadPaddingException;
029: import javax.crypto.Cipher;
030: import javax.crypto.IllegalBlockSizeException;
031: import javax.crypto.NoSuchPaddingException;
032: import javax.crypto.spec.SecretKeySpec;
033:
034: import org.columba.ristretto.coder.Base64;
035:
036: public class Blowfish {
037: private static final Logger LOG = Logger
038: .getLogger("org.columba.util.blowfish"); //$NON-NLS-1$
039:
040: private static final byte[] BYTES = { -127, 88, 27, -88, -13, -56,
041: 19, -4, 45, 25, 38, 70, -17, 40, 36, -23 };
042:
043: private static final Key KEY = new SecretKeySpec(BYTES, "Blowfish"); //$NON-NLS-1$
044:
045: public static String encrypt(char[] source) {
046: try {
047: // Create the cipher
048: Cipher blowCipher = Cipher.getInstance("Blowfish"); //$NON-NLS-1$
049:
050: // Initialize the cipher for encryption
051: blowCipher.init(Cipher.ENCRYPT_MODE, KEY);
052:
053: // Our cleartext as bytes
054: byte[] cleartext = new String(source).getBytes("UTF-8"); //$NON-NLS-1$
055:
056: // Encrypt the cleartext
057: byte[] ciphertext = blowCipher.doFinal(cleartext);
058:
059: // Return a String representation of the cipher text
060: return Base64.encode(ByteBuffer.wrap(ciphertext))
061: .toString();
062: } catch (InvalidKeyException e) {
063: LOG.severe(e.toString());
064: } catch (NoSuchAlgorithmException e) {
065: LOG.severe(e.toString());
066: } catch (NoSuchPaddingException e) {
067: LOG.severe(e.toString());
068: } catch (UnsupportedEncodingException e) {
069: LOG.severe(e.toString());
070: } catch (IllegalStateException e) {
071: LOG.severe(e.toString());
072: } catch (IllegalBlockSizeException e) {
073: LOG.severe(e.toString());
074: } catch (BadPaddingException e) {
075: LOG.severe(e.toString());
076: }
077:
078: return new String();
079: }
080:
081: public static char[] decrypt(String source) {
082: try {
083: // Create the cipher
084: Cipher blowCipher = Cipher.getInstance("Blowfish"); //$NON-NLS-1$
085:
086: // Initialize the cipher for encryption
087: blowCipher.init(Cipher.DECRYPT_MODE, KEY);
088:
089: // Encrypt the cleartext
090: ByteBuffer ciphertext = Base64.decode(source);
091: byte[] cipherArray = new byte[ciphertext.limit()];
092: ciphertext.get(cipherArray);
093:
094: // Our cleartext as bytes
095: byte[] cleartext = blowCipher.doFinal(cipherArray);
096:
097: // Return a String representation of the cipher text
098: return new String(cleartext, "UTF-8").toCharArray(); //$NON-NLS-1$
099: } catch (InvalidKeyException e) {
100: LOG.severe(e.toString());
101: } catch (NoSuchAlgorithmException e) {
102: LOG.severe(e.toString());
103: } catch (NoSuchPaddingException e) {
104: LOG.severe(e.toString());
105: } catch (UnsupportedEncodingException e) {
106: LOG.severe(e.toString());
107: } catch (IllegalStateException e) {
108: LOG.severe(e.toString());
109: } catch (IllegalBlockSizeException e) {
110: LOG.severe(e.toString());
111: } catch (BadPaddingException e) {
112: LOG.severe(e.toString());
113: }
114:
115: return new char[0];
116: }
117: }
|