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 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
00063
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
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 return encrypted.toByteArray();
00088 }
00089
00090
00091
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
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 }