org.logi.crypto.io
Class EncryptStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FilterOutputStream
              |
              +--org.logi.crypto.io.EncryptStream

public class EncryptStream
extends java.io.FilterOutputStream

This OutputStream encrypts everything written to it using the specified EncryptSession. It optionally first executes a non-interactive key-exchange protocol.

The DecryptStream must be initialized with a NoninterKeyExServer object complementing the NoninterKeyExClient object used in the EncryptStream and a DecryptMode object complementing the EncryptSession object used in the DecryptStream.

Author:
Logi Ragnarsson (logi@logi.org)
See Also:
DecryptStream, EncryptSession

Constructor Summary
EncryptStream(java.io.OutputStream out, NoninterKeyExClient kex, EncryptSession emode)
          Create a new EncryptStream.
 
Method Summary
 void close()
          Closes this output stream and releases any system resources associated with this stream.
 void execute(NoninterProtocolClient prot, boolean encrypt)
          Executes a non-interactive protocol.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out to the stream.
 EncryptionKey getKey()
          Return the key used for encrypting this stream, or null if one has not been specified.
 void reKey(NoninterKeyExClient kex, boolean encrypt)
          Executes a non-interactive key-exchange protocol.
 void setKey(CipherKey key)
          Re-key the EncryptSession used by this DecryptStream.
 void write(byte[] buf, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EncryptStream

public EncryptStream(java.io.OutputStream out,
                     NoninterKeyExClient kex,
                     EncryptSession emode)
              throws CryptoProtocolException,
                     java.io.IOException
Create a new EncryptStream. Ciphertext is written to out, emode is used for encryption and if kex is not null it will be used to generate a session key and/or send it to the server. See the various key-exchange client classes for details.

Note that if kex is not null, it controls which session key is used and emode receives the session key when it has been decided. If kex is null, then emode must be initialized with a key, and the same symmetric key or matching asymmetric key must be used on the server.

Throws:
CryptoProtocolException - if a problem arises with the key-exchange protocol.
java.io.IOException - if problems arise with the underlying OutputStream.
Method Detail

getKey

public EncryptionKey getKey()
Return the key used for encrypting this stream, or null if one has not been specified.

setKey

public void setKey(CipherKey key)
            throws java.io.IOException,
                   CryptoException
Re-key the EncryptSession used by this DecryptStream. This induces a flush of the stream.
Throws:
java.io.IOException - if there is a low-level problem.
CryptoException - if the internal buffer in the EncryptSession is not empty.

reKey

public void reKey(NoninterKeyExClient kex,
                  boolean encrypt)
           throws java.io.IOException,
                  CryptoProtocolException
Executes a non-interactive key-exchange protocol. If encrypt is true, messages will be sent through the encrypted channel. This induces a flush of the stream.

After the protocol has been executed, the exchanged key will be used for encryption.

Throws:
java.io.IOException - if there is a low-level problem.
CryptoProtocolException - if the protocol could not execute.

execute

public void execute(NoninterProtocolClient prot,
                    boolean encrypt)
             throws java.io.IOException,
                    CryptoProtocolException
Executes a non-interactive protocol. If encrypt is true, messages will be sent through the encrypted channel.
Throws:
CryptoProtocolException - if there is a problem with the protocol.
java.io.IOException - if there is a problem with the underlying streams.

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.
Overrides:
write in class java.io.FilterOutputStream

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.
Overrides:
write in class java.io.FilterOutputStream

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out to the stream. If the number of bytes written is not a multiple of the plainBlockSize of the CipherKey used for encryption, up to one byte less than a whole block of garbage may be appended to the data when flush is called.

It is possible to get rid of the random bytes by calling drain() in the DecryptStream object at the corresponding time.

Overrides:
flush in class java.io.FilterOutputStream
See Also:
DecryptStream.drain()

close

public void close()
           throws java.io.IOException
Closes this output stream and releases any system resources associated with this stream.
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException - if an I/O error occurs.


Copyright (c) 1997-2001 Logi Ragnarsson - Distributed under the GPL