00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00062
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
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 return encrypted.toByteArray();
00087 }
00088
00089
00090
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
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 }