iaik.utils
Class CipherInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--iaik.utils.CipherInputStream

public class CipherInputStream
extends FilterInputStream

Class for en/decrypting data read from an input stream.

This class provides the same functionality as javax.crypto.CipherInputStream but allows to specify a buffer size for supporting block oriented encoding as required by PKCS#7. This class extends the java.io.FilterInputStream class for combining the functionality of an InputStream and a Cipher.

According to the operation mode the Cipher has been initialized with, data that is read from the underlying input stream will be returned en/decrypted when calling one of the read() methods.

You may use a CipherInputStream for reading encrypted data from a file thereby decrypting it for obtaining the plain text, e.g.:

 KeyGenerator key_gen = KeyGenerator.getInstance("DES");
 Key des_key = key_gen.generateKey();
 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
 cipher.init(Cipher.DECRYPT_MODE, des_key);
 is = new FileInputStream("encrypted_data.enc");
 CipherInputStream cis = new CipherInputStream(is, cipher);
 byte[] buffer = new byte[100];
 int r;
 while ((r = cis.read(buffer)) != -1) {
       System.out.print(new String(buffer));
 }
 

Version:
File Revision 9

Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
protected CipherInputStream(InputStream is)
          Creates a CipherInputStream only from an InputStream.
  CipherInputStream(InputStream is, Cipher cipher)
          Creates a CipherInputStream using an InputStream and a Cipher initialized for either encryption or decryption.
  CipherInputStream(InputStream is, Cipher cipher, int bufferSize)
          Creates a CipherInputStream using an InputStream, a Cipher initialized for either encryption or decryption and a buffer size.
 
Method Summary
 int available()
          Returns the number of bytes available without blocking.
 int read()
          Reads the next data byte read from this input stream and returns it as an int value between 0 and 255, or -1 if the the end of the stream already has been reached.
 int read(byte[] b)
          Reads a number of data bytes from this input stream into a byte array.
 int read(byte[] b, int off, int len)
          Reads a specified number of data bytes from this input stream into a byte array.
 long skip(long n)
          Skips over a specified number of data bytes of this input stream and returns the number of bytes skipped.
 
Methods inherited from class java.io.FilterInputStream
close, mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CipherInputStream

public CipherInputStream(InputStream is,
                         Cipher cipher)
Creates a CipherInputStream using an InputStream and a Cipher initialized for either encryption or decryption. This constructor uses a default buffer size of 1024 bytes.
Parameters:
is - the input stream
cipher - an initialized cipher

CipherInputStream

public CipherInputStream(InputStream is,
                         Cipher cipher,
                         int bufferSize)
Creates a CipherInputStream using an InputStream, a Cipher initialized for either encryption or decryption and a buffer size. Buffer size denotes the number of bytes which are read and en/decrypted at once.
Parameters:
is - the input stream
cipher - an initialized cipher
bufferSize - size of the internal buffer

CipherInputStream

protected CipherInputStream(InputStream is)
Creates a CipherInputStream only from an InputStream. Since no Cipher is specified, alternatively you may create a CipherInputStream using an InputStream and a NullCipher.
Parameters:
is - the input stream
Method Detail

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads a specified number of data bytes from this input stream into a byte array. The bytes are read to the given byte array, beginning at position off. The decrypted data is unpadded only if the end (EOF) of the underlying input stream is reached.
Overrides:
read in class FilterInputStream
Parameters:
b - the byte array into which the data bytes are read
off - the start offset of the data
len - the maximum number of bytes to be read
Returns:
the number of bytes read into the byte array, or -1 if no more bytes are available because the end of the stream already has been reached
Throws:
IOException - if an I/O error occurs

read

public int read(byte[] b)
         throws IOException
Reads a number of data bytes from this input stream into a byte array. The decrypted data is unpadded only if the end (EOF) of the underlying input stream is reached.
Overrides:
read in class FilterInputStream
Parameters:
b - the byte array into which the data bytes are read
Returns:
the number of bytes read into the byte array, or -1 if no more bytes are available because the end of the stream already has been reached
Throws:
IOException - if an I/O error occurs

read

public int read()
         throws IOException
Reads the next data byte read from this input stream and returns it as an int value between 0 and 255, or -1 if the the end of the stream already has been reached. The decrypted data is unpadded only if the end (EOF) of the underlying input stream is reached.
Overrides:
read in class FilterInputStream
Returns:
the next data byte read from this stream, or -1 if the end of the stream has been reached
Throws:
IOException - if an I/O error occurs

skip

public long skip(long n)
          throws IOException
Skips over a specified number of data bytes of this input stream and returns the number of bytes skipped.

Since the number of bytes actually skipped may not equal (i.e. shorter) the given number of bytes to skip, the number of bytes actually skipped are returned.

Overrides:
skip in class FilterInputStream
Parameters:
n - the number of data bytes to skip
Returns:
the number of bytes actually have been skipped
Throws:
IOException - if an I/O error occurs

available

public int available()
              throws IOException
Returns the number of bytes available without blocking. Returns the number of en/decrypted bytes currently in the internal buffer.
Overrides:
available in class FilterInputStream
Returns:
the number of bytes that can be read from this input stream without blocking
Throws:
IOException - if an I/O error occurs

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK