All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.security.CipherInputStream

java.lang.Object
   |
   +----java.io.InputStream
           |
           +----java.io.FilterInputStream
                   |
                   +----java.security.CipherInputStream

public synchronized class CipherInputStream
extends FilterInputStream
ATTENTION:

This is NOT the implementation from Sun. This class has been developed by IAIK according to the documentation publically available. Only the documentation from Sun has been inserted into the source files.


A FilterInputStream that encrypts or decrypts the data passing through it. Typically, this stream would be used as a filter to read an encrypted file.

This class has a constructor that takes a Cipher and an input stream as arguments. The cipher is used to encrypt or decrypt all data read through the stream.

For block ciphers, some buffering of the data received from the input stream is done. The buffer length is the cipher's block size. For stream ciphers, that is, ciphers capable of encrypting or decrypting a byte at a time, no buffering is necessary.

The data is encrypted or decrypted, depending on the initialization state of the Cipher. To get the encryption/decryption result bytes, make one or more calls to the read methods. One read method, with no arguments, returns the next result byte. The other read method returns multiple result bytes at once.

For a buffered decrypter stream, every time the buffer is empty, the stream attempts to read a complete buffer, and blocks until it does. When it has read a buffer, it decrypts it. If the next byte is an EOF and the cipher is a padding/unpadding cipher, the decryption result is then unpadded. If an EOF is encountered before a whole buffer has been read, an exception is thrown.

For a buffered encrypter stream, every time the buffer is empty, the stream attempts to read a complete buffer, and blocks until it does. If it reads a whole buffer without reaching EOF, it encrypts it. If it reaches EOF in the middle of a buffer, and the cipher is a padding cipher, it will pad the buffer, and encrypt it. Otherwise, an exception will be thrown.

See Also:
Cipher

Constructor Index

 o CipherInputStream(InputStream, Cipher)
Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT.
 o CipherInputStream(InputStream, Cipher, int)
Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT.

Method Index

 o read()
Returns the next encrypted or decrypted byte, depending on the cipher state.
 o read(byte[], int, int)
Fills up the specified bytes of the b array with the next len encrypted or decrypted bytes (depending on the cipher state).

Constructors

 o CipherInputStream
 public CipherInputStream(InputStream is,
                          Cipher cipher)
Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT.

Parameters:
is - - the input stream.
cipher - - an initialized cipher.
See Also:
Cipher
 o CipherInputStream
 public CipherInputStream(InputStream is,
                          Cipher cipher,
                          int factor)
Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT. Use this constructor to create a CipherInputStream where the internal buffer is several times the blocksize of the cipher.

Parameters:
is - the input stream.
cipher - an initialized cipher.
factor - buffer is factor times the blocksize
See Also:
Cipher

Methods

 o read
 public int read(byte b[],
                 int off,
                 int len) throws IOException
Fills up the specified bytes of the b array with the next len encrypted or decrypted bytes (depending on the cipher state).

Parameters:
b - - the byte array into which the encrypted or decrypted bytes will be read.
off - - the offset into b indicating where the first encrypted or decrypted byte should be read.
len - - the number of encrypted/decrypted bytes to read into b, starting at offset off.
Returns:
the number of bytes read into b, or -1 if fewer than len encrypted/decrypted bytes remained.
Throws: IOException
if an error occurs.
Overrides:
read in class FilterInputStream
 o read
 public int read() throws IOException
Returns the next encrypted or decrypted byte, depending on the cipher state.

Returns:
the next encrypted or decrypted byte, or -1 if the last encrypted/decrypted byte was already returned.
Throws: IOException
if an error occurs.
Overrides:
read in class FilterInputStream

All Packages  Class Hierarchy  This Package  Previous  Next  Index