hcrypto.cipher
Class Alphabet

java.lang.Object
  |
  +--hcrypto.cipher.Alphabet

public class Alphabet
extends java.lang.Object

This class defines properties of the alphabet used in a cipher key. Each HistoricalKey has two associated alphabets, a plaintext alphabet and a ciphertext alphabet. In the default case, both alphabets are identical. An encryption engine encodes a character by mapping it from the plaintext alphabet to a corresponding character in the ciphertext alphabet. It decodes a character by mapping it fromt the ciphertext alphabet to a corresponding character in the plaintext alphabet.

An alphabet is simply the set of characters for which encryption and decryption are defined. Characters in the plaintext that are not members of the plaintext alphabet are simply left unencrypted. All of the ciphers assume that the encrypt and decrypt steps provide a reciprocal mapping from plaintext alphabet to ciphertext alphabet and vice versa.

An Alphabet is defined completely by its rangeId which selects a subset of the Unicode character codes. Currently the Alphabet class defines 6 different subsets. The subsets needn't be contiguous. The subsets are selected in the constructor by passing a valid rangeId or range descriptor. The following table shows the valid options:
Range Id Range Descriptor ASCII Characters
AlphabetFactory.ALPH_az az 'a'..'z'
AlphabetFactory.ALPH_AZ AZ 'A'..'Z'
AlphabetFactory.ALPH_azAZ azAZ 'a'..'z''A'..'Z'
AlphabetFactory.ALPH_azAZ09 azAZ09 'a'..'z''A'..'Z''0'..'9'
AlphabetFactory.ALPH_printable printable ASCII 32 .. ASCII 126
AlphabetFactory.ALPH_ascii ascii ASCII 0 .. ASCII 127

For example, the following code segment would create a CaesarKey that would be defined for all printable ASCII characters.

         CaesarKey key = HistoricalKey.getInstance("Caesar");
key.init("55/printable");
 

In this case, because only one alphabet is described (printable), the init() method will construct an alphabet consisting of all printable ASCII characters that will serve as both the plaintext alphabet and the ciphertext alphabet. The string "55" in this case gives the Caesar keyword i.e., the shift used by the cipher engine.

As another example, the following code segment would create a PolySubstitutionKey that would for a PolySubstitutionEngine that maps lowercase characters to uppercase characters.

         PolySubstitutionKey key = HistoricalKey.getInstance("PolySubstitution");
key.init("javahastwoas/az/AZ");
 

In this case, the string "javahastwoas" will be used as the PolySubstitution keyword and the init() method will construct a plaintext alphabet consisting of the lowercase characters a..z and a ciphertext alphabet consisting of the uppercase characters A..Z.

It is the responsibility of the cipher algorithm to enforce the constraints described by the alphabet. This is usually done by ignoring invalid characters. For example, here is the definition of the engineEncode() method from CaesarEngine:

public String engineEncode(String s ) throws Exception {
if (blocksize != 1)
throw new Exception("Invalid blocksize for Caesar cipher " + blocksize);
char ch = s.charAt(0);
if (alphabet.isInAlphabet(ch)) {
return "" + encodeShift(ch, shift);
}
else
return s;
}

The Alphabet class also defines methods for adding a padding string to a short string in order to bring it up to block size before encrypting and a method for removing padding from the last block of a decrypted message. A form of the modern PKCS#5 padding scheme is used to choose the padding. Our scheme uses the ranking of a character in the alphabet to represent the number of characters added where the first letter of the alphabet represents 0. For example, if 3 characters are needed for padding where the alphabet used is a..z then the string "ddd" is used. If a decrypted message has last block "endddd" the last 3 characters are deleted and the message ends with the letters "end" because d represents 3. This scheme requires the last block of the decrypted message to have some padding so if no padding is needed, an entire block of padding is added anyway. The *BlockCipher class contains the methods to add and remove padding during encryption.

See also:


BlockCipher
HistoricalKey
CaesarEngine
CaesarKey
PolySubstitutionEngine
PolySubstitutionKey
Last modified by REW on 01/20/2002


Field Summary
protected  int rangeId
          A unique identifier that represents the characters that make up the character set for a particular key.
 
Constructor Summary
protected Alphabet()
          The default constructor sets the range to 'a' to 'z'.
protected Alphabet(char[] ranges)
          Creates an alphabet using an array char ranges.
protected Alphabet(char[] ranges, int rangeId)
          This constructor lets the user set the range using one of the static constants.
protected Alphabet(char[] ranges, java.lang.String rangeDesc)
          This constructor lets the user set the range using one of a set of descriptors.
protected Alphabet(java.lang.Character.UnicodeBlock[] blocks)
          This constructor creates an alphabet composed of characters form one or more sets of Unicode blocks.
protected Alphabet(java.lang.String rangeStr)
          Creates an alphabet using given an even-length String of character ranges.
 
Method Summary
 int charToInt(char ch)
          Returns the position of the parameter ch in the alphabet in the range 0 to size - 1.
protected  java.lang.String getPadding(int n)
          Returns a String of n characters of padding each equal to the n-th character in the alphabet counting from 0.
 java.lang.String getRangeDesc()
          returns a descriptor that specifies the range of this alphabet
 int getRangeId()
          returns the rangeId
 int getSize()
          returns the number of characters in this alphabet.
 char intToChar(int n)
          Returns the character in the alphabet which has the position of the parameter n.
 boolean isInAlphabet(char ch)
          returns true iff its char parameter gives a character contained in this alphabet's characters set.
static void main(java.lang.String[] args)
           
protected  java.lang.String removePadding(java.lang.String s, int blocksize)
          removes the padding characters from its string parameter assumed to have been decrypted so that the characters at the end were added while encrypting.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rangeId

protected int rangeId
A unique identifier that represents the characters that make up the character set for a particular key.
Constructor Detail

Alphabet

protected Alphabet()
The default constructor sets the range to 'a' to 'z'.

Alphabet

protected Alphabet(char[] ranges)
            throws java.lang.Exception
Creates an alphabet using an array char ranges.
Parameters:
ranges - an array of char values of even length thought of as a series of pairs of a beginning char value and an ending char value of a sequence of char values in the alphabet. For example, ['A','Z','a','z', '0','9'] would specifie the alphanumeric ascii characters.

Alphabet

protected Alphabet(java.lang.String rangeStr)
            throws java.lang.Exception
Creates an alphabet using given an even-length String of character ranges.
Parameters:
rangeStr - a String of even length thought of as a series of pairs of a beginning char value and an ending char value of a sequence of char values in the alphabet. For example "AZaz09" would specify the alphanumeric ascii characters and "??" would specify the Hiragana character set.

Alphabet

protected Alphabet(java.lang.Character.UnicodeBlock[] blocks)
            throws java.lang.Exception
This constructor creates an alphabet composed of characters form one or more sets of Unicode blocks.
Parameters:
blocks - An array of Unicode blocks. For example, [Character.Unicode.HIRAGAN, Character.Unicode.KATAKANA].

Alphabet

protected Alphabet(char[] ranges,
                   int rangeId)
            throws java.lang.Exception
This constructor lets the user set the range using one of the static constants.
Parameters:
rangeId - an integer that specifies the character set

Alphabet

protected Alphabet(char[] ranges,
                   java.lang.String rangeDesc)
            throws java.lang.Exception
This constructor lets the user set the range using one of a set of descriptors.
Parameters:
rangeDesc - a String giving the range descriptor. It should be one of: az, AZ, azAZ, azAZ09, printable, ascii
Method Detail

getRangeId

public int getRangeId()
returns the rangeId

getRangeDesc

public java.lang.String getRangeDesc()
                              throws java.lang.Exception
returns a descriptor that specifies the range of this alphabet

getSize

public int getSize()
returns the number of characters in this alphabet.

charToInt

public int charToInt(char ch)
              throws java.lang.Exception
Returns the position of the parameter ch in the alphabet in the range 0 to size - 1. For example. For the alphabet a...z, charToInt('c') returns 2 and charToInt('z') returns 25.
Parameters:
ch - a char representing a character in the alphabet. An Exception is thrown if ch is not in the alphabet.

intToChar

public char intToChar(int n)
               throws java.lang.Exception
Returns the character in the alphabet which has the position of the parameter n. For example, for the alphabet a...z, intToChar(3) returns 'd' and intToChar(25) returns 'z'.
Parameters:
ch - a char representing a character in the alphabet. An Exception is thrown if n is not in the range 0 to size - 1.

isInAlphabet

public boolean isInAlphabet(char ch)
returns true iff its char parameter gives a character contained in this alphabet's characters set.
Parameters:
ch - a char giving the character to be tested for validity

getPadding

protected java.lang.String getPadding(int n)
                               throws java.lang.Exception
Returns a String of n characters of padding each equal to the n-th character in the alphabet counting from 0. This is called from BlockCipher.encrypt() to bring blocks up to the required blocksize.
Parameters:
n - an int indicates the number of characters needed. Example: If the character set is A..Z and 5 padding characters are needed, this method will return "FFFFF" since F is the sixth character.

removePadding

protected java.lang.String removePadding(java.lang.String s,
                                         int blocksize)
                                  throws java.lang.Exception
removes the padding characters from its string parameter assumed to have been decrypted so that the characters at the end were added while encrypting.
Parameters:
s - a String giving a block of characters from which padding should be removed
blocksize - an int giving the blocksize for a particular cipher

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception