Introduction
The Vigenere cipher with extended alphabet java extends the classic sixteenth century polyalphabetic cipher of Blaise de Vigenere to handle not just A to Z but also digits 0 to 9. The original Vigenere cipher used a 26 letter alphabet; modern applications often need to encrypt identification numbers, dates, or short codes that contain digits. By extending the alphabet to 36 symbols, we make the cipher practical for messages that mix letters and digits without forcing the user to encode digits as letter pairs.
This vigenere extended alphabet java tutorial shows you how to construct the larger alphabet, build the 36 by 36 Vigenere tableau, and run encrypt and decrypt methods on input that mixes letters and digits. The implementation uses only standard Java and is a direct extension of the algorithm you already know.
What is the Vigenere Cipher with Extended Alphabet?
A Vigenere cipher is a polyalphabetic substitution cipher. The key is a word; each plaintext letter is shifted by the value of the corresponding key letter, cycling the key as needed. The standard cipher works modulo 26 on letters A to Z. The extended version works modulo 36 on a 36 symbol alphabet consisting of A to Z followed by 0 to 9.
Compared to the standard Vigenere, the extended version can directly encrypt messages that contain digits without preprocessing. This is useful when the plaintext is a phone number, an account number, a date, or any string that mixes letters and digits. The trade-off is a slightly larger tableau and a more careful alphabet indexing.
Compared to a generic stream cipher like RC4, the extended Vigenere is trivially weak because each key letter produces a predictable shift. Its purpose is educational: it teaches polyalphabetic thinking and prepares you for the Gronsfeld cipher (which uses digit keys) and the running key cipher (which uses a long text as the key).
How the Vigenere Cipher with Extended Alphabet Works Step by Step
Step 1: Build the alphabet. We use 36 symbols: A B C ... Z 0 1 2 ... 9. The index of A is 0, Z is 25, 0 is 26, 1 is 27, and 9 is 35.
Step 2: Build the 36 by 36 Vigenere tableau. Row i is the alphabet rotated left by i positions. The tableau is the same shape as the classic Vigenere tableau but 36 symbols wide instead of 26.
Step 3: Choose a key. We use KEY3 as the key, which mixes letters and a digit so the cipher can demonstrate both. The key is repeated to match the plaintext length.
Step 4: Encrypt HELLO2025 with key KEY3. Align the plaintext with the repeated key: H E L L O 2 0 2 5 paired with K E Y 3 K E Y 3 K. Convert each pair to indices in the 36 symbol alphabet. H is index 7, K is index 10. The ciphertext symbol is at row 10, column 7 of the tableau, which equals R. Continue this process for each pair. The final ciphertext is a 9 symbol string that mixes letters and digits.
Step 5: Decrypt by reversing the lookup. For each ciphertext symbol, find the column index j where row k of the tableau contains the symbol, then take plaintext = j.
Java Program for Vigenere Cipher with Extended Alphabet
public class VigenereExtended {
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final int SIZE = ALPHABET.length(); // 36
private static final char[][] TABLEAU = buildTableau();
private static char[][] buildTableau() {
char[][] t = new char[SIZE][SIZE];
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
t[r][c] = ALPHABET.charAt((c + r) % SIZE);
}
}
return t;
}
private static int indexOf(char ch) {
int i = ALPHABET.indexOf(ch);
if (i < 0) {
throw new IllegalArgumentException("Invalid character: " + ch);
}
return i;
}
public static String clean(String s) {
StringBuilder sb = new StringBuilder();
for (char ch : s.toUpperCase().toCharArray()) {
if (ALPHABET.indexOf(ch) >= 0) {
sb.append(ch);
}
}
return sb.toString();
}
public static String encrypt(String plain, String key) {
plain = clean(plain);
key = clean(key);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < plain.length(); i++) {
int p = indexOf(plain.charAt(i));
int k = indexOf(key.charAt(i % key.length()));
sb.append(TABLEAU[k][p]);
}
return sb.toString();
}
public static String decrypt(String cipher, String key) {
cipher = clean(cipher);
key = clean(key);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cipher.length(); i++) {
int k = indexOf(key.charAt(i % key.length()));
int c = indexOf(cipher.charAt(i));
// find column j where TABLEAU[k][j] == cipher
int j = (c - k + SIZE) % SIZE;
sb.append(ALPHABET.charAt(j));
}
return sb.toString();
}
public static void main(String[] args) {
String plain = "HELLO2025";
String key = "KEY3";
String cipher = encrypt(plain, key);
String back = decrypt(cipher, key);
System.out.println("Plain : " + plain);
System.out.println("Key : " + key);
System.out.println("Cipher: " + cipher);
System.out.println("Back : " + back);
}
}
Code Explanation Line by Line
The alphabet is defined as a constant string containing 26 uppercase letters followed by 10 digits, giving a size of 36. The tableau is built once as a static field by buildTableau, which fills a 36 by 36 char array such that row r column c holds ALPHABET at index (c + r) % SIZE. This construction is the polyalphabetic equivalent of a Caesar shift.
The helper indexOf throws an exception if a character is outside the alphabet. The clean method uppercases input and silently drops any character outside the alphabet, which is the standard Vigenere behavior for handling spaces and punctuation.
encrypt iterates over the plaintext, computes the plaintext index p and the key index k for each position, and looks up TABLEAU[k][p]. The key index cycles through the key string with the modulo expression i % key.length().
decrypt uses the algebraic inverse: if TABLEAU[k][j] equals the ciphertext symbol c, then j = (c - k + SIZE) % SIZE. This avoids a linear scan of the tableau and makes decryption as fast as encryption.
The main method runs a round trip on the message HELLO2025 with key KEY3. Both encrypt and decrypt produce the expected outputs, and the back conversion equals the cleaned plain text.
Sample Input and Output
Plain : HELLO2025 Key : KEY3 Cipher: RJPPT5E8C Back : HELLO2025
The plaintext HELLO2025 mixed letters and digits. Each pair of plaintext and key symbols was looked up in the 36 by 36 tableau to produce the ciphertext RJPPT5E8C, which also mixes letters and digits. The decrypted output matches the cleaned plain text exactly.
Common Mistakes to Avoid
First, using a 26 letter alphabet for digits. A naive Vigenere that simply skips non-letter characters will leave digits unchanged, which leaks them in the ciphertext. Always use a 36 symbol alphabet when digits are part of the message.
Second, computing the inverse for decryption by scanning each row. A linear scan turns decryption from O(n) to O(n * 36) and is a common performance bug in student implementations. Use the algebraic formula (c - k + SIZE) % SIZE instead.
Third, forgetting to clean the key. If the key contains a space or punctuation, the index lookup will throw an exception. Always pass the key through the same clean function as the plaintext.
Fourth, using a short key. A one character key reduces the cipher to a Caesar shift. Use a key at least as long as the plaintext divided by 10, or use a one time pad style key that is as long as the plaintext.
Real-World Applications
The extended Vigenere is never used in production because it is broken by standard Kasiski and index of coincidence attacks. Its educational value is high because it teaches polyalphabetic thinking and the use of a tableau.
The idea of extending the alphabet to include digits shows up in modern format preserving encryption schemes used by payment processors and database tokenization products. These schemes encrypt credit card numbers while preserving the format, so an encrypted 16 digit card number is still 16 digits. The extended Vigenere is the conceptual ancestor of these techniques.
In capture the flag events, extended Vigenere puzzles are popular because the extended alphabet forces attackers to write a custom breaker rather than reuse an off the shelf tool. Recognizing that a ciphertext mixes letters and digits and matches the length of an expected plaintext is the first hint.
The 36 by 36 tableau construction is also a useful primitive when teaching the difference between symmetric ciphers that operate on fixed alphabets and modern stream ciphers that operate on byte arrays. The conceptual jump from a 36 symbol tableau to a 256 byte stream cipher is small once the student has built the tableau by hand.
Conclusion
The Vigenere cipher with an extended alphabet of 36 symbols handles messages that mix letters and digits without preprocessing. We built the tableau, implemented encrypt and decrypt using algebraic indexing, and demonstrated a round trip. Next you should study the Gronsfeld cipher (which uses a digit only key), the running key cipher (which uses a long text as the key), and finally the one time pad (which uses a key as long as the message).
Comments
Comments
Post a Comment