Class CryptoInputStream


public class CryptoInputStream

Read data blocks encrypted via an instance of the CryptoOutputStream class. The fact that the data is written in 16-byte blocks with a 1 byte length prefix is completely transparent to the user of this class. The user can simply read bytes as from any other InputStream implementation, and of course the writer to the stream is not restricted by the 16 byte divider. Data read from this secure stream is decrypted using a 128-bit key Square symetric cryptography algorithm. The password supplied via the constructor is translated to a 16 byte key via the SHA-1 hashing algorithm.

Constructor Summary
CryptoInputStream( underlying, java.lang.String password)
          Construct the CryptoInputStream object.
Method Summary
static void main(java.lang.String[] args)
 int read()
          Read the next decoded byte from the input stream.
 void setPassword(java.lang.String password)
          Set the password.
Methods inherited from class
available, close, mark, markSupported, read, read, reset, skip
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public CryptoInputStream( underlying,
                         java.lang.String password)
Construct the CryptoInputStream object.

underlying - the InputStream from which to read the encrypted data.
password - the password string to use to decrypt the encrypted data.
Method Detail


public static void main(java.lang.String[] args)


public int read()
Read the next decoded byte from the input stream. Since the Square symetric encryption algorithm must always encrypt CRYPTO_LEN byte blocks, we must read one of these blocks fully before any bytes can be returned. The CryptoOutputStream writes these blocks with a one byte prefix indicating how many of the block's bytes are actually part of the written data (It had better be a value in the range 0..CRYPTO_LEN, otherwise an IOExcepti will be thrown.). If a block has been read and all of it's useable bytes haven't been returned yet, then this method will return the next byte in that block. Otherwise, another block will be read and the first byte of that block will be returned. If there are no blocks to be read, a value of -1 is returned to indicate "end of stream". If an incomplete block is read, then an IOException is thrown since this is an illegal condition: byte should only be written in full blocks.

Specified by:
read in class
int the next decrypted byte in the stream.
Throws: - thrown if there's a problem reading from the stream, or if "end of stream" is encountered while reading a crypto block.


public void setPassword(java.lang.String password)
Set the password. After calling this with a new password value, all subsequent blocks will be decrypted using this value. Note that this method is relatively expensive, creating a SHA1 hash and initializing a new instance of the Square crypto object. This method would normally not be called by the user since the password is set via the constructor and normally all data written to an output stream will be using a single password.

password - the password string to use to decrypt the encrypted data.