hcrypto.cipher
Class Alphabet

java.lang.Object
  extended by hcrypto.cipher.Alphabet

public class Alphabet
extends java.lang.Object

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 the encryption and decryption operations 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 be defined 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/2003 to make constructors public and to add an equals() method to override the equals() from the Object class.


Constructor Summary
Alphabet()
          The default constructor sets the range to 'a' to 'z'.
Alphabet(char[] ranges)
          Creates an alphabet using an array of char values.
Alphabet(char[] ranges, int rangeId)
          This constructor lets the user set the range using one of the static constants.
Alphabet(char[] ranges, java.lang.String rangeDesc)
          This constructor lets the user set the range using one of a set of descriptors.
Alphabet(java.lang.Character.UnicodeBlock[] blocks)
          This constructor creates an alphabet composed of characters form one or more sets of Unicode blocks.
 
Method Summary
 int charToInt(char ch)
          Returns the position of the parameter ch in the alphabet in the range 0 to size - 1.
 void createPermutation(java.lang.String str, int[] permArr)
          createPermutation(str,permArr) writes a permutation to the array permArr which extends the partial mapping described by the string str.
 java.lang.String dAlbNormedPermString(java.lang.String shStr, java.lang.String permStr)
          dAlbNormedPermString(shStr, permStr) returns a String which defines the first shift defined in shStr followed by the permutation of permStr.
 java.lang.String eAlbNormedPermString(java.lang.String permStr, java.lang.String shStr)
          eAlbNormedPermString(permStr, shStr) returns a String which defines the permutation of permStr followed by the first shift defined in shStr.
 boolean equals(java.lang.Object alph2)
          Overrides the equals method of the Object class alph1.equals(alph2) returns true iff the arrays alph1.ranges and alph2.ranges have the same lengths and alph1.ranges[k] == alph2.ranges[k]for all k.
 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.
 java.lang.String intArrayToString(int[] arr)
          Returns the string of characters which have the positions in the alphabet of the array elements of arr.
 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)
           
 java.lang.String normedShiftString(java.lang.String shStr)
          normedShiftString(shStr) returns a String which defines shifts like those defined by shStr only with the first shift subtracted from all of the shifts.
static int[] permShift(int[] permIn, int shift)
          permShift(permIn, shift) returns a permutation which is the composite of permIn followed by shift, that is, k -> shift(permIn(k)).
static java.lang.String removeDuplicateChars(java.lang.String str)
           
static int[] shiftPerm(int shift, int[] permIn)
          shiftPerm(shift, permIn) returns a permutation which is the composite of shift followed by permIn, that is, k -> permIn(shift(k)).
 java.lang.String strip(java.lang.String str)
          alph.strip(str) removes characters in the String str and returns the resulting String.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Alphabet

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


Alphabet

public Alphabet(char[] ranges)
         throws java.lang.Exception
Creates an alphabet using an array of char values.

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 specify the alphanumeric ascii characters.
Throws:
java.lang.Exception

Alphabet

public 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].
Throws:
java.lang.Exception

Alphabet

public 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
Throws:
java.lang.Exception

Alphabet

public 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
Throws:
java.lang.Exception
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

Throws:
java.lang.Exception

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.
Throws:
java.lang.Exception

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.
Throws:
java.lang.Exception

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

equals

public boolean equals(java.lang.Object alph2)
Overrides the equals method of the Object class alph1.equals(alph2) returns true iff the arrays alph1.ranges and alph2.ranges have the same lengths and alph1.ranges[k] == alph2.ranges[k]for all k.

Overrides:
equals in class java.lang.Object
Parameters:
alph2 - an Object is assumed to be a member of Alphabet

strip

public java.lang.String strip(java.lang.String str)
alph.strip(str) removes characters in the String str and returns the resulting String.

Parameters:
str - is a String which may have characters not in the alphabet.

removeDuplicateChars

public static java.lang.String removeDuplicateChars(java.lang.String str)

intArrayToString

public java.lang.String intArrayToString(int[] arr)
Returns the string of characters which have the positions in the alphabet of the array elements of arr.

Parameters:
arr - is an array of int values corresponding to alphabet elements.

createPermutation

public void createPermutation(java.lang.String str,
                              int[] permArr)
createPermutation(str,permArr) writes a permutation to the array permArr which extends the partial mapping described by the string str. So k -> charToInt(str.charAt(k)) for 0 < k < str.length(). It is assumed that the number of the integers being permuted is defined by the amount of memory assigned to permArr.

Parameters:
str - is a string with characters in the alphabet.
permArr - is an int array which has been assigned memory.

permShift

public static int[] permShift(int[] permIn,
                              int shift)
permShift(permIn, shift) returns a permutation which is the composite of permIn followed by shift, that is, k -> shift(permIn(k)). It is assumed that permIn defines a valid permutation and that shift is a valid shift.

Parameters:
permIn - is an int array which defines a valid permutation.
shift - is an int shift between 0 and 1 less than the size of permIn.

shiftPerm

public static int[] shiftPerm(int shift,
                              int[] permIn)
shiftPerm(shift, permIn) returns a permutation which is the composite of shift followed by permIn, that is, k -> permIn(shift(k)). It is assumed that permIn defines a valid permutation and that shift is a valid shift.

Parameters:
shift - is an int shift between 0 and 1 less than the size of permIn.
permIn - is an int array which defines a valid permutation.

normedShiftString

public java.lang.String normedShiftString(java.lang.String shStr)
normedShiftString(shStr) returns a String which defines shifts like those defined by shStr only with the first shift subtracted from all of the shifts. Thus for alphabet "az", normedShiftString("xyz")=="abc"

Parameters:
shStr - is a String of chars in the alphabet.

eAlbNormedPermString

public java.lang.String eAlbNormedPermString(java.lang.String permStr,
                                             java.lang.String shStr)
eAlbNormedPermString(permStr, shStr) returns a String which defines the permutation of permStr followed by the first shift defined in shStr. This permutation and the normed shift string of shStr are keys that give precisely the same easy Alberti cipher as the keys permStr and shStr.

Parameters:
permStr - is a String of chars in the alphabet defining a permutation.
shStr - is a String of chars in the alphabet defining a sequence of shifts.

dAlbNormedPermString

public java.lang.String dAlbNormedPermString(java.lang.String shStr,
                                             java.lang.String permStr)
dAlbNormedPermString(shStr, permStr) returns a String which defines the first shift defined in shStr followed by the permutation of permStr. This permutation and the normed shift string of shStr are keys that give precisely the same difficult Alberti cipher as the keys shStr and permStr.

Parameters:
shStr - is a String of chars in the alphabet defining a sequence of shifts.
permStr - is a String of chars in the alphabet defining a permutation.

main

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