|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objecthcrypto.cipher.Alphabet
public class Alphabet
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:
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.
BlockCipher
HistoricalKey
CaesarEngine
CaesarKey
PolySubstitutionEngine
PolySubstitutionKey
| 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 |
|---|
public Alphabet()
public Alphabet(char[] ranges)
throws java.lang.Exception
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.
java.lang.Exception
public Alphabet(java.lang.Character.UnicodeBlock[] blocks)
throws java.lang.Exception
blocks - An array of Unicode blocks. For example,
[Character.Unicode.HIRAGAN, Character.Unicode.KATAKANA].
java.lang.Exception
public Alphabet(char[] ranges,
int rangeId)
throws java.lang.Exception
rangeId - an integer that specifies the character set
java.lang.Exception
public Alphabet(char[] ranges,
java.lang.String rangeDesc)
throws java.lang.Exception
rangeDesc - a String giving the range descriptor. It should be
one of: az, AZ, azAZ, azAZ09, printable, ascii
java.lang.Exception| Method Detail |
|---|
public int getRangeId()
public java.lang.String getRangeDesc()
throws java.lang.Exception
java.lang.Exceptionpublic int getSize()
public int charToInt(char ch)
throws java.lang.Exception
ch - a char representing a character in the alphabet.
An Exception is thrown if ch is not in the alphabet.
java.lang.Exception
public char intToChar(int n)
throws java.lang.Exception
ch - a char representing a character in the alphabet.
An Exception is thrown if n is not in the range 0 to size - 1.
java.lang.Exceptionpublic boolean isInAlphabet(char ch)
ch - a char giving the character to be tested for validitypublic boolean equals(java.lang.Object alph2)
equals in class java.lang.Objectalph2 - an Object is assumed to be a member of Alphabetpublic java.lang.String strip(java.lang.String str)
str - is a String which may have characters not in the alphabet.public static java.lang.String removeDuplicateChars(java.lang.String str)
public java.lang.String intArrayToString(int[] arr)
arr - is an array of int values corresponding to alphabet elements.
public void createPermutation(java.lang.String str,
int[] permArr)
str - is a string with characters in the alphabet.permArr - is an int array which has been assigned memory.
public static int[] permShift(int[] permIn,
int shift)
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.
public static int[] shiftPerm(int shift,
int[] permIn)
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.public java.lang.String normedShiftString(java.lang.String shStr)
shStr - is a String of chars in the alphabet.
public java.lang.String eAlbNormedPermString(java.lang.String permStr,
java.lang.String shStr)
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.
public java.lang.String dAlbNormedPermString(java.lang.String shStr,
java.lang.String permStr)
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.
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exception
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||