public class CipherInputStream
extends java.io.FilterInputStream
This class extends the java.io.FilterInputStream class
for combining the capabilities of an InputStream and a
Cipher.
Depending upon the operation mode with which the Cipher has been initialized,
data that is read from the underlying input stream will be returned
either encrypted or decrypted when calling one of the read
methods.
You can use an instance of the CipherInputStream class to
obtain a file in plain text by reading encrypted data from a file. For
example:
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));
}
InputStream,
FilterInputStream,
Cipher,
CipherOutputStream| Modifier | Constructor and Description |
|---|---|
protected |
CipherInputStream(java.io.InputStream is)
Creates a
CipherInputStream object using an
InputStream argument. |
|
CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher)
Creates a
CipherInputStream object. |
|
CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher,
int bufferSize)
Creates an instance of the
CipherInputStream class. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns the number of bytes available without blocking.
|
int |
read()
Reads the next data byte read from this input stream.
|
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)
Ignores a specified number of data bytes in this input stream and
returns the number of bytes ignored.
|
public CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher)
CipherInputStream object.
This constructor creates an instance of the CipherInputStream
class using InputStream and Cipher objects
initialized for either encryption or decryption. The default
buffer size is 1024 bytes.
is - the input streamcipher - an initialized cipherCipherpublic CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher,
int bufferSize)
CipherInputStream class.
This constructor requires an InputStreamCipher initialized for either encryption or decryption,
and a buffer size as arguments. The buffer size denotes the number
of bytes that are read and encrypted or decrypted at once.
is - the input streamcipher - an initialized cipherbufferSize - size of the internal bufferCipherprotected CipherInputStream(java.io.InputStream is)
CipherInputStream object using an
InputStream argument.
As an alternative, because no Cipher is specified,
you can create a CipherInputStream object using an
InputStream and a NullCipher.
is - the input streampublic int read(byte[] b,
int off,
int len)
throws java.io.IOException
byte array.
Beginning at the position specified by the off
argument, the method reads bytes of data to the given byte
array. The decrypted data is unpadded only if the end
of the underlying input stream (EOF) is reached.
read in class java.io.FilterInputStreamb - the byte array into which the data bytes are readoff - the start offset of the data\len - the maximum number of bytes to be readbyte array,
or -1 if no more bytes are available because the end of the
stream has already been reachedjava.io.IOException - if an I/O error occurspublic int read(byte[] b)
throws java.io.IOException
byte array.
The decrypted data is unpadded only if the end
of the underlying input stream (EOF) is reached.
read in class java.io.FilterInputStreamb - the byte array into which the data bytes are readbyte array,
8 or -1 if no more bytes are available because the end of
8 the stream has already been reachedjava.io.IOException - if an I/O error occurspublic int read()
throws java.io.IOException
The method returns the next data byte from this input stream as an integer value between 0 and 255, or -1 if the the end of the stream has already been reached.
The decrypted data is unpadded only if the end
of the underlying input stream (EOF) is reached.
read in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occurspublic long skip(long n)
throws java.io.IOException
Since the number of bytes ignored might not equal (i.e. shorter) the given number of bytes specified, the method returns the actual number of bytes ignored.
skip in class java.io.FilterInputStreamn - the number of data bytes to skipjava.io.IOException - if an I/O error occurspublic int available()
throws java.io.IOException
Returns the number of encrypted or decrypted bytes in the internal buffer.
available in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occurs