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

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   static final long serialVersionUID = 5521257935120563452L;
00046         private BlowfishCipher cypher;
00050         public AcmeBlowfishCypher() {
00051                 super();
00052                 cypher = new BlowfishCipher("abcdefghijklmnopqrstuvwxyz");
00053         }
00057         public AcmeBlowfishCypher(String key) {
00058                 super();
00059                 cypher = new BlowfishCipher(key);
00060         }
00061 
00062   /* (non-Javadoc)
00063    * @see org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher#encryptObject(byte[])
00064    */
00065   public byte[] encryptObject(byte[] object) throws IOException {
00066                 ByteArrayOutputStream encrypted = new ByteArrayOutputStream();
00067                 EncryptedOutputStream encrout = new EncryptedOutputStream(cypher, encrypted);
00068                 encrout.write(object);
00069                 encrout.flush();
00070                 encrout.close();
00071 /*      byte[] eigthbyte = new byte[8];
00072                 byte[] cipherdata = new byte[8];
00073         int i=0;
00074         while (true)    {
00075                 if (i+8 >= object.length)       { //does not encrypt remaining bytes.
00076                         for (int j=i; j<object.length; j++)     {
00077                                         encrypted.write((int)object[j]);
00078                         }
00079                         break;
00080                 } else  {
00081                         System.arraycopy(object, i, eigthbyte, 0, 8);
00082                         i+=8;
00083                                 cypher.encrypt( eigthbyte, cipherdata );
00084                                 encrypted.write(cipherdata);
00085                 }
00086         } */
00087                 return encrypted.toByteArray();
00088   }
00089 
00090   /* (non-Javadoc)
00091    * @see org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher#decryptObject(byte[])
00092    */
00093   public  byte[] decryptObject(byte[] object) throws IOException {
00094                 ByteArrayOutputStream ret = new ByteArrayOutputStream();
00095                 ByteArrayInputStream decrypted = new ByteArrayInputStream(object);
00096                 EncryptedInputStream encint = new EncryptedInputStream(cypher, decrypted);
00097                 int read = 0;
00098                 while ((read = encint.read()) != -1)    {
00099                         ret.write(read);
00100                 }
00101                 ret.flush();
00102                 return ret.toByteArray();
00103   }
00104   
00105         public void setKey( byte[] key )        {
00106                 cypher = new BlowfishCipher(key);
00107         }
00108           
00109         public void setKey(String key ) {
00110                 cypher = new BlowfishCipher(key);
00111         }
00112         
00113         public static void main(String[] args)  {
00114                 //test encryption data
00115                 AcmeBlowfishCypher cypher = new AcmeBlowfishCypher();
00116                 try     {
00117                         byte[] test0 = new byte[0];
00118                         byte[] res = cypher.encryptObject(test0);
00119                         res = cypher.decryptObject(res);
00120                         byte[] test1 = {1, 2, 2, 2, 2, 3};
00121                         res = cypher.encryptObject(test1);
00122                         res = cypher.decryptObject(res);
00123                         byte[] test2 = {1, 2, 2, 2, 2, 2, 2, 3};
00124                         res = cypher.encryptObject(test2);
00125                         res = cypher.decryptObject(res);
00126                         byte[] test3 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00127                         res = cypher.encryptObject(test3);
00128                         res = cypher.decryptObject(res);
00129                         byte[] test4 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00130                         res = cypher.encryptObject(test4);
00131                         res = cypher.decryptObject(res);
00132                         byte[] test5 = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};
00133                         res = cypher.encryptObject(test5);
00134                         res = cypher.decryptObject(res);
00135                         System.out.println("END");
00136                 } catch (Throwable ex)  {
00137                         ex.printStackTrace();
00138                 }
00139                 
00140                 
00141         }
00142 
00143 }

Generated on Wed Dec 14 21:05:32 2005 for OpenMobileIS by  doxygen 1.4.4