Introduction
The bifid cipher was invented by the French cryptographer Felix Marie Delastelle in 1901. Delastelle was a pioneer of fractionation ciphers, which split each plaintext letter into multiple symbols (typically digits) and then rearrange those symbols before recombining them. The bifid cipher is one of his most elegant constructions, combining the Polybius square substitution with a transposition of the resulting digits. In this bifid cipher java tutorial we will implement the full algorithm and see why fractionation makes frequency analysis harder.
The bifid cipher matters historically as one of the strongest practical ciphers of the early 20th century. It is significantly harder to break than the Playfair cipher because it operates on individual letters rather than digraphs, but the fractionation step smears the letter frequencies in a way that defeats naive frequency analysis. The cipher was used for some French military communications and is considered a milestone in the development of fractionation ciphers like the ADFGVX.
What is the Bifid Cipher?
The bifid cipher is a fractionation cipher that combines a Polybius square substitution with a transposition. The algorithm works in three steps: first, each plaintext letter is converted to a pair of digits using a Polybius square. Second, the digit pairs are written in two rows (the row digits on top, the column digits on the bottom) and read off as a single sequence. Third, the sequence is grouped into new pairs and each pair is converted back to a letter using the same Polybius square.
Compared to the Polybius square alone, the bifid cipher adds a transposition step that mixes the digit positions. This breaks the simple one-to-one mapping between plaintext letters and ciphertext letters, making frequency analysis much harder. Compared to the Playfair cipher, the bifid cipher operates on single letters rather than digraphs, which makes the implementation simpler but the cryptanalysis slightly different.
The bifid cipher uses a 5 by 5 Polybius square that can be keyed by a keyword, just like the Playfair grid. Without a keyword, the standard square (with I and J merged) is used. The key space is large because any keyword produces a different square.
How the Bifid Cipher Works Step by Step
Suppose our plaintext is HELLO and we use the standard Polybius square (no keyword). 1 2 3 4 5 1: A B C D E 2: F G H I K 3: L M N O P 4: Q R S T U 5: V W X Y Z
Step 1: Convert each plaintext letter to a pair of digits (row, column). H = (2, 3) E = (1, 5) L = (3, 1) L = (3, 1) O = (3, 4)
Step 2: Write the row digits on the top row and the column digits on the bottom row. Row digits: 2 1 3 3 3 Column digits: 3 5 1 1 4
Step 3: Read the digits off as a single sequence, row by row. The sequence is 2 1 3 3 3 3 5 1 1 4.
Step 4: Group the sequence into new pairs of digits: (2,1), (3,3), (3,3), (5,1), (1,4).
Step 5: Convert each pair back to a letter using the same Polybius square. (2,1) = F (3,3) = N (3,3) = N (5,1) = V (1,4) = D
So HELLO encrypts to FNNVD. To decrypt, reverse the process: convert each ciphertext letter to a digit pair, write the digits as a single sequence, split into two halves of equal length, and use the first half as row digits and the second half as column digits.
Java Program for Bifid Cipher
import java.util.*;
public class BifidCipher {
private static final char[][] grid = {
{'A', 'B', 'C', 'D', 'E'},
{'F', 'G', 'H', 'I', 'K'},
{'L', 'M', 'N', 'O', 'P'},
{'Q', 'R', 'S', 'T', 'U'},
{'V', 'W', 'X', 'Y', 'Z'}
};
private static final Map<Character, int[]> pos = new HashMap<>();
static {
for (int r = 0; r < 5; r++) {
for (int c = 0; c < 5; c++) {
pos.put(grid[r][c], new int[]{r, c});
if (grid[r][c] == 'I') pos.put('J', new int[]{r, c});
}
}
}
public static String encrypt(String text) {
text = text.toUpperCase().replaceAll("[^A-Z]", "").replace("J", "I");
int n = text.length();
int[] rows = new int[n];
int[] cols = new int[n];
for (int i = 0; i < n; i++) {
int[] p = pos.get(text.charAt(i));
rows[i] = p[0];
cols[i] = p[1];
}
int[] combined = new int[2 * n];
for (int i = 0; i < n; i++) combined[i] = rows[i];
for (int i = 0; i < n; i++) combined[n + i] = cols[i];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2 * n; i += 2) {
int r = combined[i];
int c = combined[i + 1];
sb.append(grid[r][c]);
}
return sb.toString();
}
public static String decrypt(String text) {
text = text.toUpperCase().replaceAll("[^A-Z]", "");
int n = text.length();
int[] combined = new int[2 * n];
for (int i = 0; i < n; i++) {
int[] p = pos.get(text.charAt(i));
combined[2 * i] = p[0];
combined[2 * i + 1] = p[1];
}
int[] rows = new int[n];
int[] cols = new int[n];
for (int i = 0; i < n; i++) rows[i] = combined[i];
for (int i = 0; i < n; i++) cols[i] = combined[n + i];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(grid[rows[i]][cols[i]]);
}
return sb.toString();
}
public static void main(String[] args) {
String plain = "HELLO";
String cipher = encrypt(plain);
String recovered = decrypt(cipher);
System.out.println("Plain : " + plain);
System.out.println("Cipher : " + cipher);
System.out.println("Decoded : " + recovered);
}
}
Code Explanation Line by Line
The grid and pos fields are the standard Polybius square plus a position lookup map. We build pos in a static initializer, mapping each letter to its (row, column) coordinates. The letter J is mapped to the same coordinates as I, so that J in the input is treated as I.
The encrypt method first cleans the input. It then creates two arrays, rows and cols, each of length n. For each plaintext letter, we look up its position and store the row in rows and the column in cols. This is the fractionation step.
Next, we create a combined array of length 2n. We copy rows into the first half of combined and cols into the second half. This is the transposition step: the row digits and column digits are interleaved in a specific pattern that breaks the one-to-one letter mapping.
Finally, we walk through the combined array in pairs (combined[0], combined[1]), (combined[2], combined[3]), and so on. Each pair is interpreted as (row, column) coordinates in the Polybius square, and we look up the corresponding letter. This is the recombination step.
The decrypt method reverses the process. We convert each ciphertext letter to a pair of digits and write them into the combined array interleaved (combined[0] = row of letter 0, combined[1] = col of letter 0, combined[2] = row of letter 1, and so on). Then we split the combined array in half: the first n entries are the row digits, and the last n entries are the column digits. Finally, we use (rows[i], cols[i]) to look up the original plaintext letter.
The trick is in the indexing. During encryption, we combine rows and cols linearly (rows first, then cols). During decryption, we interleave them (alternating row and col). The two operations are inverses because the dimensions work out: 2n digits rearranged as n pairs gives back n pairs of (row, column) coordinates.
Sample Input and Output
Plain : HELLO Cipher : FNNVD Decoded : HELLO
The output confirms our manual calculation. HELLO encrypts to FNNVD using the standard bifid cipher, and decrypting FNNVD recovers HELLO.
Common Mistakes to Avoid
1. Forgetting to merge I and J. The Polybius square has 25 cells for 26 letters, so I and J must share a cell. Forgetting this leads to a missing letter in the grid and a NullPointerException when J appears in the input.
2. Mixing up the row and column digits. The encrypt method writes rows first then cols in the combined array. The decrypt method interleaves them. Swapping the order produces wrong ciphertext that does not decrypt to the original.
3. Not handling odd-length plaintext. The bifid cipher works cleanly for any length, but if you accidentally round up the array sizes, you might end up with extra digits that confuse decryption.
4. Using a non-square grid. The bifid cipher requires a square Polybius grid (5 by 5) for the math to work. A non-square grid would not have the same number of rows and columns, and the combined array would not split evenly.
5. Forgetting to clean non-letters from the input. Punctuation and spaces in the plaintext would cause a NullPointerException when the pos map is queried for a non-letter character.
Real-World Applications
The bifid cipher has no real-world security use today. It was broken in the early 20th century using statistical attacks that exploit the structure of the digit transposition. However, the principle of fractionation (splitting each letter into multiple symbols and rearranging them) lives on in modern cryptography as the basis of certain diffusion strategies.
The AES MixColumns step is conceptually similar to fractionation. It splits each byte of the state matrix into bits and mixes them across the matrix in a way that spreads local changes globally. The exact mechanism is different (matrix multiplication over GF(2^8) rather than digit transposition), but the underlying goal of diffusion is the same.
In CTF competitions, the bifid cipher occasionally appears as a medium-difficulty challenge. The standard attack is to use frequency analysis on the digit pairs rather than single letters, which requires more ciphertext than a simple substitution cipher but is still feasible for messages of a few hundred characters.
The bifid cipher also has historical significance as the predecessor of the ADFGVX cipher, which used a similar fractionation approach with a 6 by 6 grid (for 36 symbols including digits) and a columnar transposition. The ADFGVX cipher was used by Germany in World War I and was broken by French cryptanalyst Georges Painvin in 1918, in one of the great achievements of classical cryptanalysis.
The bifid cipher is also pedagogically valuable because it teaches the concept of fractionation, which is one of the key ideas in modern cryptography. Understanding how splitting and recombining symbols can break statistical patterns is essential for understanding why modern block ciphers use multiple rounds of substitution and permutation.
The cipher also demonstrates an important design principle: a cipher does not have to be mathematically complex to be cryptographically strong. The bifid cipher uses only basic array manipulation, but the right combination of substitution and transposition produces a cipher that resisted serious attacks for years.
Conclusion
In this bifid cipher java tutorial we built a complete fractionation cipher combining a Polybius square with a digit transposition, walked through every line of code, and explained the math behind the encrypt and decrypt methods. You now understand fractionation, the role of transposition in breaking frequency patterns, and the historical context of Delastelle's contribution. Next, study the four square cipher, which uses two keyed Polybius squares to encrypt digraphs.
Comments
Comments
Post a Comment