hcrypto.engines
Class PolySubstitutionEngine
java.lang.Object
|
+--hcrypto.cipher.CipherEngine
|
+--hcrypto.cipher.BlockCipher
|
+--hcrypto.engines.PolySubstitutionEngine
- public class PolySubstitutionEngine
- extends BlockCipher
This class implements a PolySubstitution cipher algorithm (which many
authors call a Homophonic Substitution cipher). The key is represented
by a keyword, which is inserted at the beginning of the decoding alphabet.
More precisely, the cipher alphabet is likely to be considerably larger
than the plaintext alphabet with certain plaintext characters mapped into
several different ciphertext characters, that is, the encryption mapping
is one-to-many. On the other hand, each ciphertext character represents a
unique plaintext character. The keyword will be expanded to a string with
length equal to the size of the cipher alphabet with all characters in the
plaintext alphabet. The kth character of the cipher alphabet will be decoded
into the kth character of the keyword. This map also uniquely determines
the set of characters that each plaintext character can be mapped into.
The encoding algorithm maintains a variable that, for each character in the
plaintext message, stores the position of that character. The value of that
variable modulo the number of ciphertext characters that plaintext character
can be mapped into determines which ciphertext character it is mapped into.
For example, suppose that the plaintext alphabet is a..z of size 26 and
the cipher alphabet is a..zA..Z of size 52. Suppose the user chooses the
keyword: "nowisthetimetolearncryptography" of length 31. The 10 characters
"bdfjkquvxz" are not contained in this keyword so add them to the end of it.
Now add the first 11 characters of the resulting keyword to the end of itself
to get a new keyword of length 52. Thus we have the following table for
decoding:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -- The cipher alphabet
nowisthetimetolearncryptographybdfjkquvxznowisthetim -- The decoding alphabet
Thus for encryption, for example, "a" can be mapped into "q" or "B"
and "t" can be mapped into any of the letters of "fimxUX". If the plaintext
message starts with the word "that", the first "t" of "that" would be encoded
as "f", the second would be encoded as "i", and so on. Since there are six
different encodings for "t", after six encodings, the seventh occurrence of
't' would be encoded as "f".
The PolySubstution cipher is one that requires that two different
alphabets be used for the plaintext and ciphertext alphabets
respectively. As just explained, the ciphertext alphabet must
be larger than the plaintext alphabet. The following code segment
provides an example of how to create and use a Polysubstitution cipher:
Cipher cipher = Cipher.getInstance("PolySubstitution");
HistoricalKey key = HistoricalKey.getInstance("PolySubstitution", cipher.getProvider());
key.init("nowisthetimetolearncryptography/az/azAZ");
cipher.init(key);
String c1encrypt = cipher.encrypt("this is a secret message");
In this case, the plaintext alphabet consists of the letters a..z, and
the ciphertext alphabet consists of the letters a..zA..Z. The decoding
alphabet would be constructed from the keyphrase as described above.
See also:
PolySubstitutionKey
Alphabet
Constructor Summary |
PolySubstitutionEngine()
Creates a PolySubstitutionEngine and sets the alphabetRangeOptions
instance variable to "111111", which translates to all six alphabet options. |
Method Summary |
java.lang.String |
engineDecode(java.lang.String s)
Returns an decoded String for the specified encode String. |
java.lang.String |
engineEncode(java.lang.String s)
Returns an encoded String for the specified String. |
protected void |
engineInit(HistoricalKey hKey)
Initializes the PolySubstitutionEngine with the specified hKey. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PolySubstitutionEngine
public PolySubstitutionEngine()
- Creates a PolySubstitutionEngine and sets the alphabetRangeOptions
instance variable to "111111", which translates to all six alphabet options.
engineInit
protected void engineInit(HistoricalKey hKey)
throws java.lang.Exception
- Initializes the PolySubstitutionEngine with the specified hKey.
- Overrides:
engineInit
in class BlockCipher
- Parameters:
hKey
- a PolySubstitutionKey.
engineEncode
public java.lang.String engineEncode(java.lang.String s)
throws java.lang.Exception
- Returns an encoded String for the specified String. Characters which are
not part of the chosen alphabet set are ignored.
- Overrides:
engineEncode
in class BlockCipher
- Parameters:
s
- the String to be encoded
engineDecode
public java.lang.String engineDecode(java.lang.String s)
throws java.lang.Exception
- Returns an decoded String for the specified encode String.
Characters which are not part of the chosen alphabet set are ignored.
- Overrides:
engineDecode
in class BlockCipher
- Parameters:
s
- the String to be decoded.