edu.rit.crypto.blockcipher
Class BlockCipher
- java.lang.Object
-
- edu.rit.crypto.blockcipher.BlockCipher
-
- Direct Known Subclasses:
- AES256Cipher, AES256CipherSmp
public abstract class BlockCipher extends java.lang.ObjectClass BlockCipher is the abstract base class for a block cipher. A block cipher object can encrypt a block of plaintext, yielding a block of ciphertext. (Decryption is defined in another class.) The encryption key is specified when the block cipher object is constructed and may also be specified by calling the setKey() method. A block cipher object can report the number of bytes in the block (the block length) and the number of bytes in the key (the key length).The actual encryption algorithm is implemented in a subclass.
-
-
Constructor Summary
Constructors Constructor and Description BlockCipher(int theBlockLength, int theKeyLength)Construct a new block cipher object.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description voidencrypt(byte[] theBlock)Encrypt the given plaintext block in place.abstract voidencrypt(byte[] thePlaintext, byte[] theCiphertext)Encrypt the given plaintext block.abstract voiderase()Erase this block cipher object's key material.intgetBlockLength()Determine the block length of this block cipher.intgetKeyLength()Determine the key length of this block cipher.abstract voidsetKey(byte[] theKey)Set the key to be used for all subsequent encryptions and decryptions.
-
-
-
Constructor Detail
-
BlockCipher
public BlockCipher(int theBlockLength, int theKeyLength)Construct a new block cipher object.- Parameters:
theBlockLength- Number of bytes in the plaintext or ciphertext block.theKeyLength- Number of bytes in the encryption key.- Throws:
java.lang.IllegalArgumentException- (unchecked exception) Thrown if theBlockLength <= 0 or theKeyLength <= 0.
-
-
Method Detail
-
getBlockLength
public int getBlockLength()
Determine the block length of this block cipher.- Returns:
- Number of bytes in the plaintext or ciphertext block.
-
getKeyLength
public int getKeyLength()
Determine the key length of this block cipher.- Returns:
- Number of bytes in the key.
-
setKey
public abstract void setKey(byte[] theKey)
Set the key to be used for all subsequent encryptions and decryptions. After this method returns, theKey is no longer needed and may be erased (set to all 0s). The length of theKey must be at least getKeyLength(). Only the first getKeyLength() bytes are used.A subclass must override this method to implement the key setting algorithm.
- Parameters:
theKey- Key (byte array).- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theKey is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if theKey.length < getKeyLength().
-
encrypt
public void encrypt(byte[] theBlock)
Encrypt the given plaintext block in place. On input, theBlock contains the plaintext. On output, the contents of theBlock have been replaced by the ciphertext. The length of theBlock must be at least getBlockLength(). Only the first getBlockLength() bytes are encrypted.This is a convenience method that simply calls encrypt (theBlock, theBlock).
- Parameters:
theBlock- Block to be encrypted.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theBlock is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if theBlock.length < getBlockLength().java.lang.IllegalStateException- (unchecked exception) Thrown if the key has been erased and not re-set.
-
encrypt
public abstract void encrypt(byte[] thePlaintext, byte[] theCiphertext)Encrypt the given plaintext block. On input, thePlaintext contains the plaintext. On output, the contents of theCiphertext have been replaced by the ciphertext. thePlaintext and theCiphertext may be the same block. The length of thePlaintext must be at least getBlockLength(). Only the first getBlockLength() bytes are read. The length of theCiphertext must be at least getBlockLength(). Only the first getBlockLength() bytes are written.A subclass must override this method to implement the encryption algorithm.
- Parameters:
thePlaintext- Input plaintext block to be encrypted.theCiphertext- Output ciphertext block.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if thePlaintext is null or theCiphertext is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if thePlaintext.length < getBlockLength() or theCiphertext.length < getBlockLength().java.lang.IllegalStateException- (unchecked exception) Thrown if the key has been erased and not re-set.
-
erase
public abstract void erase()
Erase this block cipher object's key material.A subclass must override this method to set all of this block cipher object's key material to innocuous values (like 0). It is not acceptable merely to null out references to the key material, as that leaves the key material still stored in memory.
-
-
DMelt 3.0 © DataMelt by jWork.ORG