1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.store.raw.data.DataFactory; 26 import org.apache.derby.iapi.store.raw.RawStoreFactory; 27 28 import org.apache.derby.iapi.services.io.CompressedNumber; 29 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 33 37 public class DecryptInputStream extends BufferedByteHolderInputStream { 38 39 42 protected DataFactory dataFactory; 43 protected InputStream in; 44 45 public DecryptInputStream(InputStream in, ByteHolder bh, DataFactory dataFactory) 46 throws IOException { 47 48 super(bh); 49 this.in = in; 50 this.dataFactory = dataFactory; 51 fillByteHolder(); 52 } 53 54 public void fillByteHolder() throws IOException { 55 56 if (this.bh.available() == 0) { 57 58 this.bh.clear(); 59 60 try { 61 int realLen = CompressedNumber.readInt(in); 64 if (realLen == -1) 67 return; 68 69 int tail = realLen % dataFactory.getEncryptionBlockSize(); 71 int padding = (tail == 0) ? 0 : (dataFactory.getEncryptionBlockSize() - tail); 72 int encryptedLen = realLen + padding; 73 74 byte[] ciphertext = new byte[encryptedLen]; 76 in.read(ciphertext, 0, encryptedLen); 77 byte[] cleartext = new byte[encryptedLen]; 78 dataFactory.decrypt(ciphertext, 0, encryptedLen, cleartext, 0); 80 81 bh.write(cleartext, padding, realLen); 83 84 } catch (StandardException se) { 85 throw new IOException (); 86 } 87 88 this.bh.startReading(); 90 } 91 } 92 } 93 | Popular Tags |