Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

AcmeBlowfishCypher.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.database.fastobjectdb.db.crypto;
00029 
00030 import java.io.ByteArrayInputStream;
00031 import java.io.ByteArrayOutputStream;
00032 import java.io.IOException;
00033 
00034 import Acme.Crypto.BlowfishCipher;
00035 import Acme.Crypto.EncryptedInputStream;
00036 import Acme.Crypto.EncryptedOutputStream;
00037 
00044 public final class AcmeBlowfishCypher implements FODBCypher {
00045         private BlowfishCipher cypher;
00049         public AcmeBlowfishCypher() {
00050                 super();
00051                 cypher = new BlowfishCipher("abcdefghijklmnopqrstuvwxyz");
00052         }
00056         public AcmeBlowfishCypher(String key) {
00057                 super();
00058                 cypher = new BlowfishCipher(key);
00059         }
00060 
00061   /* (non-Javadoc)
00062    * @see org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher#encryptObject(byte[])
00063    */
00064   public byte[] encryptObject(byte[] object) throws IOException {
00065                 ByteArrayOutputStream encrypted = new ByteArrayOutputStream();
00066                 EncryptedOutputStream encrout = new EncryptedOutputStream(cypher, encrypted);
00067                 encrout.write(object);
00068                 encrout.flush();
00069                 encrout.close();
00070 /*      byte[] eigthbyte = new byte[8];
00071                 byte[] cipherdata = new byte[8];
00072         int i=0;
00073         while (true)    {
00074                 if (i+8 >= object.length)       { //does not encrypt remaining bytes.
00075                         for (int j=i; j<object.length; j++)     {
00076                                         encrypted.write((int)object[j]);
00077                         }
00078                         break;
00079                 } else  {
00080                         System.arraycopy(object, i, eigthbyte, 0, 8);
00081                         i+=8;
00082                                 cypher.encrypt( eigthbyte, cipherdata );
00083                                 encrypted.write(cipherdata);
00084                 }
00085         } */
00086                 return encrypted.toByteArray();
00087   }
00088 
00089   /* (non-Javadoc)
00090    * @see org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher#decryptObject(byte[])
00091    */
00092   public  byte[] decryptObject(byte[] object) throws IOException {
00093                 ByteArrayOutputStream ret = new ByteArrayOutputStream();
00094                 ByteArrayInputStream decrypted = new ByteArrayInputStream(object);
00095                 EncryptedInputStream encint = new EncryptedInputStream(cypher, decrypted);
00096                 int read = 0;
00097                 while ((read = encint.read()) != -1)    {
00098                         ret.write(read);
00099                 }
00100                 ret.flush();
00101                 return ret.toByteArray();
00102   }
00103   
00104         public void setKey( byte[] key )        {
00105                 cypher = new BlowfishCipher(key);
00106         }
00107           
00108         public void setKey(String key ) {
00109                 cypher = new BlowfishCipher(key);
00110         }
00111         
00112         public static void main(String[] args)  {
00113                 //test encryption data
00114                 AcmeBlowfishCypher cypher = new AcmeBlowfishCypher();
00115                 try     {
00116                         byte[] test0 = new byte[0];
00117                         byte[] res = cypher.encryptObject(test0);
00118                         res = cypher.decryptObject(res);
00119                         byte[] test1 = {1, 2, 2, 2, 2, 3};
00120                         res = cypher.encryptObject(test1);
00121                         res = cypher.decryptObject(res);
00122                         byte[] test2 = {1, 2, 2, 2, 2, 2, 2, 3};
00123                         res = cypher.encryptObject(test2);
00124                         res = cypher.decryptObject(res);
00125                         byte[] test3 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00126                         res = cypher.encryptObject(test3);
00127                         res = cypher.decryptObject(res);
00128                         byte[] test4 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00129                         res = cypher.encryptObject(test4);
00130                         res = cypher.decryptObject(res);
00131                         byte[] test5 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00132                         res = cypher.encryptObject(test5);
00133                         res = cypher.decryptObject(res);
00134                         System.out.println("END");
00135                 } catch (Throwable ex)  {
00136                         ex.printStackTrace();
00137                 }
00138                 
00139                 
00140         }
00141 
00142 }

Generated on Thu Oct 6 10:06:31 2005 for OpenMobileIS by  doxygen 1.4.3