org.logi.crypto.modes
Class EncryptCBC

java.lang.Object
  |
  +--org.logi.crypto.Crypto
        |
        +--org.logi.crypto.modes.EncryptMode
              |
              +--org.logi.crypto.modes.EncryptCBC
All Implemented Interfaces:
EncryptSession

public class EncryptCBC
extends org.logi.crypto.modes.EncryptMode

Cipher Block Chaining mode xors each plain block with the previous cipher block before encrypting. See FIPS PUB 81 or ANSI X3.106 or ISO IS8372 for a complete specification.

A padding engine can be passed to the constructor of the EncryptCBC object to allow the encryption of data which is not a whole multiple of plaintext blocks. If no padding engine is given, then the padding used depends on the key class being used.

If the key implements the CipherKey interface, which makes it a key for a symmetric cipher, then PKCS#5 padding is used. Otherwise PKCS#1 padding is used, which is appropriate for RSA and Diffie-Hellman/El-Gamal.

Author:
Logi Ragnarsson (logi@logi.org)
See Also:
DecryptCBC

Fields inherited from class org.logi.crypto.Crypto
BIT, cdsPath, EMPTY_ARRAY, FOUR, keySource, NIBBLE, ONE, primeCertainty, random, TWO, ZERO
 
Constructor Summary
EncryptCBC()
          Create a new CBC-mode encrypt session with no key and PKCS#5 padding.
EncryptCBC(EncryptionKey key)
          Create a new CBC-mode encrypt session with the specified key and PKCS#5 padding.
EncryptCBC(EncryptionKey key, Padding padding)
          Create a new CBC-mode encrypt session with the specified key.
EncryptCBC(Padding padding)
          Create a new CBC-mode encrypt session with no key.
 
Method Summary
 void close()
           
 byte[] encrypt(byte[] source, int i, int length)
          Send bytes to the EncryptCBC object for encryption.
 void finalize()
           
 byte[] flush()
          Pads the internal buffer acording to PKCS#5 (See RFC 1423, sec 1.1), encrypts it and returns the ciphertext.
 byte[] flush(byte[] source, int i, int length)
          Equivalent to calling encrypt(source,i,length) followed by flush().
 EncryptionKey getKey()
          Return the key used for encryption.
 int plainBlockSize()
          Return the size of the blocks of plaintext encrypted by this object.
 void setKey(EncryptionKey key)
          Set the key to use for encryption.
 
Methods inherited from class org.logi.crypto.Crypto
binString, binString, ensureArrayLength, ensureArrayLength, equal, equalRelaxed, equalSub, fromHexNibble, fromHexString, fromString, fromString, hexString, hexString, hexString, hexString, hexString, initRandom, initRandom, makeClass, makeInt, makeLong, makeSessionKey, pastSpace, pickBits, pickBits, prettyPrint, prettyPrint, readBlock, readInt, trimArrayLength, trimArrayLength, trimLeadingZeroes, writeBytes, writeBytes, writeInt
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EncryptCBC

public EncryptCBC(EncryptionKey key,
                  Padding padding)
Create a new CBC-mode encrypt session with the specified key.
Parameters:
key - The key to use for encryption.
padding - The padding engine to use.

EncryptCBC

public EncryptCBC(EncryptionKey key)
Create a new CBC-mode encrypt session with the specified key and PKCS#5 padding.
Parameters:
key - The key to use for encryption.

EncryptCBC

public EncryptCBC(Padding padding)
Create a new CBC-mode encrypt session with no key. No encryption can be performed until the setKey() method has been called.
Parameters:
padding - The padding engine to use.

EncryptCBC

public EncryptCBC()
Create a new CBC-mode encrypt session with no key and PKCS#5 padding. No encryption can be performed until the setKey() method has been called.
Parameters:
padding - The padding engine to use.
Method Detail

getKey

public EncryptionKey getKey()
Return the key used for encryption.

plainBlockSize

public int plainBlockSize()
Return the size of the blocks of plaintext encrypted by this object.

setKey

public void setKey(EncryptionKey key)
            throws CryptoException
Set the key to use for encryption. Do not call this method when there may be data in the internal buffer.
Throws:
CryptoException - if there is data in the internal buffer which should be encrypted with the old key.

encrypt

public byte[] encrypt(byte[] source,
                      int i,
                      int length)
Send bytes to the EncryptCBC object for encryption.

Encrypt length bytes from source, starting at i and return the ciphertext. Data is encrypted in blocks, so only whole blocks of ciphertext are written to dest. Any remaining plaintext will be stored and prepended to source in the next call to encrypt.


flush

public byte[] flush()
             throws PaddingException
Pads the internal buffer acording to PKCS#5 (See RFC 1423, sec 1.1), encrypts it and returns the ciphertext.

flush

public byte[] flush(byte[] source,
                    int i,
                    int length)
             throws PaddingException
Equivalent to calling encrypt(source,i,length) followed by flush().
Specified by:
flush in interface EncryptSession

close

public void close()

finalize

public void finalize()


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