1 21 package com.db4o.io.crypt; 22 23 import java.io.*; 24 25 import com.db4o.io.*; 26 27 42 public class XTeaEncryptionFileAdapter extends IoAdapter { 43 private IoAdapter _adapter; 45 46 private String _key; 48 49 private XTEA _xtea; 51 52 private long _pos; 53 54 private XTEA.IterationSpec _iterat; 55 56 private static final boolean DEBUG = false; 57 58 66 public XTeaEncryptionFileAdapter(String password) { 67 this(new RandomAccessFileAdapter(), password, XTEA.ITERATIONS32); 68 69 } 70 71 82 public XTeaEncryptionFileAdapter(String password, XTEA.IterationSpec iterat) { 83 this(new RandomAccessFileAdapter(), password, iterat); 84 85 } 86 87 99 public XTeaEncryptionFileAdapter(IoAdapter adapter, String password, 100 XTEA.IterationSpec iterat) { 101 _adapter = adapter; 102 _key = password; 103 _iterat = iterat; 104 } 105 116 public XTeaEncryptionFileAdapter(IoAdapter adapter, String password) { 117 _adapter = adapter; 118 _key = password; 119 _iterat = XTEA.ITERATIONS32; 120 } 121 122 private XTeaEncryptionFileAdapter(IoAdapter adapter, XTEA xtea) { 123 _adapter = adapter; 124 _xtea = xtea; 125 } 126 127 130 public void close() throws IOException { 131 _adapter.close(); 132 } 133 134 public void delete(String path) { 135 _adapter.delete(path); 136 } 137 138 public boolean exists(String path) { 139 return _adapter.exists(path); 140 } 141 142 145 public long getLength() throws IOException { 146 return _adapter.getLength(); 147 } 148 149 152 public IoAdapter open(String path, boolean lockFile, long initialLength) 153 throws IOException { 154 return new XTeaEncryptionFileAdapter(_adapter.open(path, lockFile, 156 initialLength), new XTEA(_key, _iterat)); 157 158 } 159 160 163 public int read(byte[] bytes, int length) throws IOException { 164 long origPos = _pos; 165 int fullLength = length; 166 int prePad = (int) (_pos % 8); 167 fullLength += prePad; 168 int overhang = fullLength % 8; 169 int postPad = (overhang == 0 ? 0 : 8 - (overhang)); 170 fullLength += postPad; 171 byte[] pb = new byte[fullLength]; 172 if (prePad != 0) { 173 seek(_pos - prePad); 174 } 175 int readResult = _adapter.read(pb); 176 if (DEBUG) { 177 log("3. before dencrypt/read->", pb); 178 } 179 _xtea.decrypt(pb); 180 181 System.arraycopy(pb, prePad, bytes, 0, length); 182 if (DEBUG) { 183 log("4. after dencrypt/read->", pb); 184 } 185 seek(origPos + length); 186 return length; 187 } 188 189 192 public void seek(long pos) throws IOException { 193 _pos = pos; 194 _adapter.seek(pos); 195 } 196 197 200 public void sync() throws IOException { 201 _adapter.sync(); 202 } 203 204 207 public void write(byte[] buffer, int length) throws IOException { 208 long origPos = _pos; 209 int fullLength = length; 210 int prePad = (int) (_pos % 8); 211 fullLength += prePad; 212 int overhang = fullLength % 8; 213 int postPad = (overhang == 0 ? 0 : 8 - (overhang)); 214 fullLength += postPad; 215 byte[] pb = new byte[fullLength]; 216 if (prePad != 0) { 217 seek(origPos - prePad); 218 } 219 if (blockSize() % 8 != 0 || prePad != 0) { 224 read(pb); 225 seek(origPos - prePad); 226 } 227 System.arraycopy(buffer, 0, pb, prePad, length); 228 if (prePad == 0) { 229 } 233 if (DEBUG) { 234 log("1. before encrypt/write->", pb); 235 } 236 237 _xtea.encrypt(pb); 238 if (DEBUG) { 239 log("2. after encrypt/write->", pb); 240 } 241 _adapter.write(pb, pb.length); 242 seek(origPos + length); 243 } 244 245 private void log(String msg, byte[] buf) { 246 System.out.println("\n " + msg); 247 for (int idx = 0; idx < buf.length; idx++) { 248 System.out.print(buf[idx] + " "); 249 } 250 } 251 252 } 253 | Popular Tags |