|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--hcrypto.cipher.Alphabet
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 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 = 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 |
protected int rangeId
public char padBaseChar
Constructor Detail |
protected Alphabet()
protected Alphabet(char[] ranges)
protected Alphabet(java.lang.Character.UnicodeBlock[] blocks) throws java.lang.Exception
rangeId
- an integer that specifies the character setprotected Alphabet(char[] ranges, int rangeId) throws java.lang.Exception
rangeId
- an integer that specifies the character setprotected 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, asciiMethod Detail |
public int getRangeId()
public java.lang.String getRangeDesc() throws java.lang.Exception
public char getLowBound()
public char getLowBound(char ch) throws java.lang.Exception
ch
- a char specifying a character within a given rangepublic char getHighBound()
public char getHighBound(char ch) throws java.lang.Exception
ch
- a char specifying a character within a given rangepublic int getRange(char ch) throws java.lang.Exception
ch
- a char representing a character within a contiguous range of characterspublic boolean isInAlphabet(char ch)
ch
- a char giving the character to be tested for validityprotected char setPadBaseChar()
protected java.lang.String getPadding(int n)
public boolean isPaddingChar(char ch)
protected java.lang.String removePadding(java.lang.String s, int blocksize)
s
- a String giving a block of characters from which padding should be removedblocksize
- an int giving the blocksize for a particular cipher
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |