org.openuat.util
Class SimpleBlockCipher

java.lang.Object
  extended by org.openuat.util.SimpleBlockCipher

public class SimpleBlockCipher
extends java.lang.Object

This class implements a simple interface to a block cipher (AES/Rijndael) with as little parameters as possible.

Author:
Rene Mayrhofer

Field Summary
static int BlockByteLength
          The current block size of the used cipher in bytes.
 java.lang.String instanceId
          This may be set to distinguish multiple instances running on the same machine.
static int KeyByteLength
          The current length in byte of the key.
 
Constructor Summary
SimpleBlockCipher(boolean useJSSE)
          Construct the simple block cipher object.
 
Method Summary
 byte[] decrypt(byte[] cipherText, int numMessageBits, byte[] sharedKey)
          Decrypt the cipher text message with the shared key set in the constructor.
 byte[] encrypt(byte[] plainText, int numMessageBits, byte[] sharedKey)
          Encrypt the plain text message with the shared key set in the constructor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KeyByteLength

public static final int KeyByteLength
The current length in byte of the key.

See Also:
Constant Field Values

BlockByteLength

public static final int BlockByteLength
The current block size of the used cipher in bytes.

See Also:
Constant Field Values

instanceId

public java.lang.String instanceId
This may be set to distinguish multiple instances running on the same machine.

Constructor Detail

SimpleBlockCipher

public SimpleBlockCipher(boolean useJSSE)
Construct the simple block cipher object.

Parameters:
useJSSE - If set to true, the JSSE API with the default JCE provider of the JVM will be used for cryptographic operations. If set to false, an internal copy of the Bouncycastle Lightweight API classes will be used.
Method Detail

encrypt

public byte[] encrypt(byte[] plainText,
                      int numMessageBits,
                      byte[] sharedKey)
               throws InternalApplicationException
Encrypt the plain text message with the shared key set in the constructor. If the message length equals the block size of the cipher, it is assumed to be a nonce and is encrypted as a single block in ECB mode. If it is larger, it is encrypted in CBC mode with a random IV prepended.

Parameters:
plainText - The message to encrypt. It must contain exactly as many bits as specified in the numMessageBits parameter in the constructor.
numMessageBits - The number of bits to use of this message. If set to -1, will use all bits from plainText.
sharedKey - The key to use for encryption. It must be of length KeyByteLength.
Returns:
The cipher text, which is either one block long or the number of blocks necessary to encrypt numMessageBits plus one block for the IV.
Throws:
InternalApplicationException

decrypt

public byte[] decrypt(byte[] cipherText,
                      int numMessageBits,
                      byte[] sharedKey)
               throws InternalApplicationException
Decrypt the cipher text message with the shared key set in the constructor. If the message length equals the block size of the cipher, the plain text is assumed to have been a nonce and is decrypted as a single block in ECB mode. If it is larger, it is decrypted in CBC mode with a random IV prepended.

Parameters:
cipherText - The cipher text to decrypt. It must be either one block long or the number of blocks necessary to encrypt numMessageBits plus one block for the IV.
numMessageBits - The number of bits to extract from cipherText after decrypting.
sharedKey - The key to use for encryption. It must be of length KeyByteLength.
Returns:
The plain text, which contains exactly as many bits as specified in the numMessageBits parameter in the constructor.
Throws:
InternalApplicationException


2005-2009, Rene Mayrhofer.