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 cipher key. Each HistoricalKey has an associated alphabet. An alphabet is simply the set of characters for which encryption and decryption are defined. Characters in the plaintext that not members of the alphabet are simply left unencrypted. All of the ciphers assume that the encrypt and decrypt steps provide a reciprocal mapping from Alphabet to Alphabet.

An Alphabet is defined completely by its rangeId which selects a subset of the ASCII 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 IdRange DescriptorASCII Characters
AlphabetFactory.ALPH_azaz'a'..'z'
AlphabetFactory.ALPH_AZAZ'A'..'Z'
AlphabetFactory.ALPH_azAZazAZ'a'..'z''A'..'Z'
AlphabetFactory.ALPH_azAZ09azAZ09'a'..'z''A'..'Z''0'..'9'
AlphabetFactory.ALPH_printableprintableASCII 32 .. ASCII 126
AlphabetFactory.ALPH_asciiasciiASCII 0 .. ASCII 127

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

         CaesarKey key = new CaesarKey("55/printable");
 

The CaesarKey constructor will pass the descriptor "printable" to the Alphabet constructor. The string "55" in this case gives the Caesar keyword i.e., the shift used by the cipher engine.

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

   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)) {
           char low = alphabet.getLowBound(ch); // e.g., 'a'
           int range = alphabet.getRange(ch);   // e.g., 26
           return "" + (char)(((ch - low) + shift) % range + low);
       }
       else 
           return s;
   }
 

The Alphabet class also defines the padBaseChar, the character used to define the range of padding characters that may be added to bring a block up to the block size. The padding characters must be selected from outside the set of alphabet characters. For example, if the alphabet is 'a' to 'z', then the digits '0'..'9' could be used as padding, since no ambiguity will be created. On the other hand, if the alphabet is '0' to '9', then the digits cannot be used as padding. In that case we use the character range beginning at '!' to serve as padding. In this case, the '!' would represent 0, " would be 1, # would be 2, and so on.

A form of PKCS#5 padding is used in which the padding itself represents the number of padding characters present in the ciphertext. For example, if 5 padding characters are needed, then the string "55555" would be added to the ciphertext. If the plain text included digits (azAZ09), then the 5 padding characters would be "&&&&&" because ('&' - '!') equals 5. The BlockCipher class contains the methods to add and remove padding during encryption.

See also:


BlockCipher
HistoricalSecretKey
CaesarCipherSpi
CaesarKey


Field Summary
 char padBaseChar
          The first character in a range of characters used to pad blocks that are less than the cipher's blocksize in length.
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 the given Unicode 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 a set of Unicode blocks
 
Method Summary
 char getHighBound()
          returns the characters the represents the highest character contained in this (possibly noncontiguous) character set
 char getHighBound(char ch)
          returns the character that represents the highest character contained in the contiguous range of characters that includes its parameter.
 char getLowBound()
          returns the characters the represents the lowest character contained in this (possibly noncontiguous) character set
 char getLowBound(char ch)
          returns the character that represents the lowest character contained in the contiguous range of characters that includes its parameter.
protected  java.lang.String getPadding(int n)
          Returns a String of n characters of padding.
 int getRange(char ch)
          returns the size of the range of contiguous characters containing its parameter.
 java.lang.String getRangeDesc()
          returns a descriptor that specifies the range of this alphabet
 int getRangeId()
          returns the rangeId
 boolean isInAlphabet(char ch)
          returns true iff its char parameter gives a character contained in this alphabet's characters set.
 boolean isPaddingChar(char ch)
          returns true iff its parameter is a member of the padding character set.
protected  java.lang.String removePadding(java.lang.String s, int blocksize)
          removes the padding characters from its string parameter.
protected  char setPadBaseChar()
          sets the first character in the padding character set based on the principle that padding characters must not be contained in the alphabet's character set.
 
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.

padBaseChar

public char padBaseChar
The first character in a range of characters used to pad blocks that are less than the cipher's blocksize in length. A version of PKCS#5 padding is used in which each padding character represents the number of padding characters present in the encrypted text. For example, "55555" would indicate that the last 5 characters are padding and should not be decrypted.
Constructor Detail

Alphabet

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

Alphabet

protected Alphabet(char[] ranges)
Creates an alphabet using the given Unicode ranges.

Alphabet

protected Alphabet(java.lang.Character.UnicodeBlock[] blocks)
            throws java.lang.Exception
This constructor creates an alphabet composed of a set of Unicode blocks
Parameters:
rangeId - an integer that specifies the character set

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

getLowBound

public char getLowBound()
returns the characters the represents the lowest character contained in this (possibly noncontiguous) character set

getLowBound

public char getLowBound(char ch)
                 throws java.lang.Exception
returns the character that represents the lowest character contained in the contiguous range of characters that includes its parameter. For example, if the parameter's value is 'b', then the lowest character would be 'a'; if the parameter is '8', the lowest character would be '0'.
Parameters:
ch - a char specifying a character within a given range

getHighBound

public char getHighBound()
returns the characters the represents the highest character contained in this (possibly noncontiguous) character set

getHighBound

public char getHighBound(char ch)
                  throws java.lang.Exception
returns the character that represents the highest character contained in the contiguous range of characters that includes its parameter. For example, if the parameter's value is 'b', then the highest character would be 'z'; if the parameter is '8', the highest character would be '9'.
Parameters:
ch - a char specifying a character within a given range

getRange

public int getRange(char ch)
             throws java.lang.Exception
returns the size of the range of contiguous characters containing its parameter. For example, if the parameter is 'c', the range is 26; if the parameter is '8', the range is 10.
Parameters:
ch - a char representing a character within a contiguous range of characters

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

setPadBaseChar

protected char setPadBaseChar()
sets the first character in the padding character set based on the principle that padding characters must not be contained in the alphabet's character set. So for alphabetic character sets, the padding character set is '0' to '9'. For alphanumeric character sets the padding character set starts at '!'. For printable character set, the padding character set starts at ASCII 0.

getPadding

protected java.lang.String getPadding(int n)
Returns a String of n characters of padding. This is called from BlockCipher.encrypt() to bring blocks up to the blocksize. If the character set is azAZ, and 5 padding characters are needed, this method will return "55555".

isPaddingChar

public boolean isPaddingChar(char ch)
returns true iff its parameter is a member of the padding character set.

removePadding

protected java.lang.String removePadding(java.lang.String s,
                                         int blocksize)
removes the padding characters from its string parameter.
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