hcrypto.cipher
Class BlockCipher

java.lang.Object
  |
  +--hcrypto.cipher.CipherEngine
        |
        +--hcrypto.cipher.BlockCipher
Direct Known Subclasses:
AffineEngine, CaesarEngine, PlayfairEngine, PolySubstitutionEngine, RailFenceEngine, SubstitutionEngine, TranspositionEngine, VigenereEngine

public abstract class BlockCipher
extends CipherEngine

This abstract class represents a basic block cipher. It provides implementations of methods to encrypt and decrypt files and strings, for the entire range of cipher engines.

Subclasses of this class must implement three methods: engineInit(HistoricalKey), engineEncode(String) and engineDecode(String). Of course, each particular cipher algorithm will implement these methods in its own way.

Traditional character ciphers, such as Caesar, Simple Substitution, Vigenere, and Enigma, can be represented as special cases of a block cipher by setting the blocksize to 1. This should be done in the engineInit() method.

The engineEncrypt() and engineDecrypt() methods also handle padding of strings that are smaller than blocksize. Details on the padding scheme are provided in Alphabet.

The class contains algorithm-independent implementations of the following methods: engineEncrypt(String), engineDecrypt(String), engineEncryptFile(String, String), and engineDecryptFile(String,String).


Field Summary
protected  Alphabet alphabet
          Refers to the alphabet used by the plaintext of a particular cipher.
protected  int blocksize
          Stores the block size for the particular cipher.
protected  Alphabet cipherAlphabet
          Refers to the alphabet used for the ciphertext of a particular cipher.
 
Fields inherited from class hcrypto.cipher.CipherEngine
alphabetRangeOptions
 
Constructor Summary
BlockCipher()
           
 
Method Summary
protected  char decodeShift(char ch, int k)
          This method shifts a character in the alphabet ahead the specified number of characters in the alphabet.
protected  char encodeShift(char ch, int k)
          This method shifts a character in the alphabet ahead the specified number of characters in the alphabet.
protected abstract  java.lang.String engineDecode(java.lang.String s)
          This abstract method must be implemented in the subclass to perform basic decrypting step for a given cipher algorithm.
protected  java.lang.String engineDecrypt(java.lang.String s)
          This method decrypts a String, returning a String.
protected  void engineDecryptFile(java.lang.String inFileName, java.lang.String outFileName)
          This method decrypts its input file storing the decrypted data in the output file.
protected  void engineDecryptFile(java.lang.String inFileName, java.lang.String outFileName, java.lang.String encoding)
          This method decrypts its input file storing the decrypted data in the output file.
protected abstract  java.lang.String engineEncode(java.lang.String s)
          This abstract method must be implemented in the subclass to perform basic encrypting step for a given cipher algorithm.
protected  java.lang.String engineEncrypt(java.lang.String s)
          This method encrypts a String, returning a String.
protected  void engineEncryptFile(java.lang.String inFileName, java.lang.String outFileName)
          This method encrypts its input file storing the encrypted text in the output file.
protected  void engineEncryptFile(java.lang.String inFileName, java.lang.String outFileName, java.lang.String encoding)
          This method encrypts its input file storing the encrypted text in the output file.
protected abstract  void engineInit(HistoricalKey key)
          This abstract method must be implemented in the subclass to perform initializations required by a given cipher algorithm.
 
Methods inherited from class hcrypto.cipher.CipherEngine
getAlphabetRangeOptions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blocksize

protected int blocksize
Stores the block size for the particular cipher. This variable should be initialized in the subclass.

alphabet

protected Alphabet alphabet
Refers to the alphabet used by the plaintext of a particular cipher. The cipher alphabet is defined as part of a HistoricalKey. This variable should be initialized in engineInit().

cipherAlphabet

protected Alphabet cipherAlphabet
Refers to the alphabet used for the ciphertext of a particular cipher. For most ciphers, cipherAlphabet == alphabet. The cipher alphabet is defined as part of a HistoricalKey. This variable should be initialized in engineInit().
Constructor Detail

BlockCipher

public BlockCipher()
Method Detail

engineInit

protected abstract void engineInit(HistoricalKey key)
                            throws java.lang.Exception
This abstract method must be implemented in the subclass to perform initializations required by a given cipher algorithm. Its parameter provides algorithm-specific information that is used to encrypt and decrypt. The key also provides a reference to the Alphabet that defines which plaintext characters are to be encrypted.
Overrides:
engineInit in class CipherEngine
Parameters:
key - an instance of a HistoricalKey subclass appropriate to the particular cipher algorithm

engineEncode

protected abstract java.lang.String engineEncode(java.lang.String s)
                                          throws java.lang.Exception
This abstract method must be implemented in the subclass to perform basic encrypting step for a given cipher algorithm.

engineDecode

protected abstract java.lang.String engineDecode(java.lang.String s)
                                          throws java.lang.Exception
This abstract method must be implemented in the subclass to perform basic decrypting step for a given cipher algorithm.

engineEncryptFile

protected final void engineEncryptFile(java.lang.String inFileName,
                                       java.lang.String outFileName)
                                throws java.lang.Exception
This method encrypts its input file storing the encrypted text in the output file. Note the use of InputStreamReader and OutputStreamReader. These classes perform automatic Unicode conversion to and from platform-dependent and locale-dependent encodings. This is an important internationalization feature.
Overrides:
engineEncryptFile in class CipherEngine
Parameters:
inFileName - a String giving the name of the input file
outFileName - a String giving the name of the output file

engineEncryptFile

protected final void engineEncryptFile(java.lang.String inFileName,
                                       java.lang.String outFileName,
                                       java.lang.String encoding)
                                throws java.lang.Exception
This method encrypts its input file storing the encrypted text in the output file. Note the use of InputStreamReader and OutputStreamReader. These classes perform automatic Unicode conversion to and from platform-dependent and locale-dependent encodings. This is an important internationalization feature.
Overrides:
engineEncryptFile in class CipherEngine
Parameters:
inFileName - a String giving the name of the input file
outFileName - a String giving the name of the output file
encoding - a String giving the name of the encoding scheme -- e.g., "ISO-2022-JP"

engineDecryptFile

protected final void engineDecryptFile(java.lang.String inFileName,
                                       java.lang.String outFileName)
                                throws java.lang.Exception
This method decrypts its input file storing the decrypted data in the output file.
Overrides:
engineDecryptFile in class CipherEngine
Parameters:
inFileName - a String giving the name of the input file
outFileName - a String giving the name of the output file

engineDecryptFile

protected final void engineDecryptFile(java.lang.String inFileName,
                                       java.lang.String outFileName,
                                       java.lang.String encoding)
                                throws java.lang.Exception
This method decrypts its input file storing the decrypted data in the output file.
Overrides:
engineDecryptFile in class CipherEngine
Parameters:
inFileName - a String giving the name of the input file
outFileName - a String giving the name of the output file
encoding - a String giving the name of the encoding scheme -- e.g., "ISO-2022-JP"

engineEncrypt

protected java.lang.String engineEncrypt(java.lang.String s)
                                  throws java.lang.Exception
This method encrypts a String, returning a String. It invokes the abstract method encode(String) which is implemented in an algorithm-specific way in the subclass.
Overrides:
engineEncrypt in class CipherEngine
Parameters:
s - the String to be encrypted

engineDecrypt

protected java.lang.String engineDecrypt(java.lang.String s)
                                  throws java.lang.Exception
This method decrypts a String, returning a String. It invokes the abstract method decode(String) which is implemented in an algorithm-specific way in the subclass.
Overrides:
engineDecrypt in class CipherEngine
Parameters:
s - the String to be encrypted

encodeShift

protected char encodeShift(char ch,
                           int k)
                    throws java.lang.Exception
This method shifts a character in the alphabet ahead the specified number of characters in the alphabet. If the character is not in the alphabet, the character itself is returned. Characters that are shifted beyond the end of the alphabet are wrapped around past the beginning of the alphabet.
Parameters:
ch - the character to be shifted.
k - the size (number of characters) of the shift.

decodeShift

protected char decodeShift(char ch,
                           int k)
                    throws java.lang.Exception
This method shifts a character in the alphabet ahead the specified number of characters in the alphabet. If the character is not in the alphabet, the character itself is returned. Characters that are shifted beyond the end of the alphabet are wrapped around past the beginning of the alphabet.
Parameters:
ch - the character to be shifted.
k - the size (number of characters) of the shift.