Introduction
The running key cipher java is a polyalphabetic substitution cipher that uses a long stream of text as the key. Instead of repeating a short keyword like the Vigenere cipher, the running key cipher takes its key from a book, a newspaper article, a constitution, or any other long text the sender and receiver share. Because the key is as long as the message, the cipher resists the Kasiski examination and the index of coincidence attack that break the Vigenere cipher. The running key cipher java tutorial in this post walks through the construction, the encryption and decryption algorithms, and a complete Java implementation.
The running key cipher is sometimes confused with the one time pad because both use a long key. The crucial difference is entropy: the one time pad uses a truly random key, while the running key cipher uses natural language text that has high redundancy. This redundancy is the weakness that lets a patient cryptanalyst break the cipher even without the key.
What is the Running Key Cipher?
The running key cipher is a polyalphabetic substitution cipher that uses a long key text instead of a short repeating keyword. Each plaintext letter is shifted by the value of the corresponding key letter, exactly like the Vigenere cipher, but the key never repeats because it is as long as the plaintext.
Compared to Vigenere, the running key cipher defeats the Kasiski examination because repeated plaintext fragments align with different key letters and produce different ciphertext fragments. Compared to the one time pad, the running key cipher is much weaker because natural language text is highly redundant: English has about 1.5 bits of entropy per letter, while a truly random key has about 4.7 bits per letter.
The running key cipher is structurally identical to the Vigenere cipher except that the key is not cycled. The mathematical operation is the same: ciphertext = (plaintext + key) mod 26. The security comes entirely from the length and unpredictability of the key text.
How the Running Key Cipher Works Step by Step
Step 1: Choose a key text. The sender and the receiver agree on a book, a chapter, and a starting position. For this tutorial we use the opening of the United States Constitution as the running key: WE THE PEOPLE OF THE UNITED STATES IN ORDER TO FORM A MORE PERFECT UNION.
Step 2: Clean both texts. Remove all non-letter characters from the plaintext and the key text, and convert both to uppercase. The plaintext is ATTACK AT DAWN. The cleaned key text is WETHEPEOPLEOFTHEUNITEDSTATESINORDERTOFORMAMOREPERFECTUNION.
Step 3: Align the plaintext with the key. The first 12 letters of the cleaned key text are WETHEPEOPLEO. The plaintext ATTACKATDAWN pairs letter by letter: A-W, T-E, T-T, A-H, C-E, K-P, A-E, T-O, D-P, A-L, W-E, N-O.
Step 4: Encrypt each pair. A shifted by W (22 positions) becomes W. T shifted by E (4 positions) becomes X. T shifted by T (19 positions) becomes M. A shifted by H (7 positions) becomes H. C shifted by E (4 positions) becomes G. K shifted by P (15 positions) becomes Z. A shifted by E (4 positions) becomes E. T shifted by O (14 positions) becomes H. D shifted by P (15 positions) becomes S. A shifted by L (11 positions) becomes L. W shifted by E (4 positions) becomes A. N shifted by O (14 positions) becomes B. The ciphertext is WXMHG ZEHSL AB.
Step 5: Decrypt by subtracting the key letter from each ciphertext letter, modulo 26. W minus W is A. X minus E is T. And so on.
Java Program for Running Key Cipher
public class RunningKeyCipher {
private static String clean(String s) {
StringBuilder sb = new StringBuilder();
for (char ch : s.toUpperCase().toCharArray()) {
if (ch >= 'A' && ch <= 'Z') {
sb.append(ch);
}
}
return sb.toString();
}
public static String encrypt(String plain, String keyText) {
plain = clean(plain);
keyText = clean(keyText);
if (keyText.length() < plain.length()) {
throw new IllegalArgumentException(
"Key text must be at least as long as plaintext.");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < plain.length(); i++) {
int p = plain.charAt(i) - 'A';
int k = keyText.charAt(i) - 'A';
sb.append((char) ('A' + (p + k) % 26));
}
return sb.toString();
}
public static String decrypt(String cipher, String keyText) {
cipher = clean(cipher);
keyText = clean(keyText);
if (keyText.length() < cipher.length()) {
throw new IllegalArgumentException(
"Key text must be at least as long as ciphertext.");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cipher.length(); i++) {
int c = cipher.charAt(i) - 'A';
int k = keyText.charAt(i) - 'A';
sb.append((char) ('A' + (c - k + 26) % 26));
}
return sb.toString();
}
public static void main(String[] args) {
String plain = "ATTACK AT DAWN";
String keyText = "WE THE PEOPLE OF THE UNITED STATES IN ORDER TO FORM";
String cipher = encrypt(plain, keyText);
String back = decrypt(cipher, keyText);
System.out.println("Plain : " + plain);
System.out.println("Cipher: " + cipher);
System.out.println("Back : " + back);
}
}
Code Explanation Line by Line
The clean method uppercases the input and drops all non-letter characters. This is the standard Vigenere style of handling spaces and punctuation, and it keeps the key text and the plaintext aligned by letter position rather than by raw character position.
encrypt first checks that the key text is at least as long as the plaintext. This is the defining property of the running key cipher: the key never repeats. If the key text is shorter than the plaintext, the cipher silently degenerates to a Vigenere cipher, which is a security failure. We throw an exception to make this failure visible.
The encrypt loop walks each plaintext letter, adds the corresponding key letter value, and takes the result modulo 26. The key index is the same as the plaintext index, not cycled, which is the difference between the running key cipher and the Vigenere cipher.
decrypt uses the algebraic inverse formula (c - k + 26) % 26. The +26 guards against negative results, which would otherwise produce incorrect ciphertext when Java's % operator returns a negative value.
The main method runs a round trip with the message ATTACK AT DAWN and the opening of the United States Constitution as the key text. The cleaned key text is much longer than the plaintext, so only the first 12 letters are used.
Sample Input and Output
Plain : ATTACK AT DAWN Key : WE THE PEOPLE OF THE UNITED STATES... Cipher: WXMHGZEHSLAB Back : ATTACKATDAWN
The plaintext ATTACK AT DAWN becomes WXMHGZEHSLAB with key text starting WE THE PEOPLE. The decrypted output matches the cleaned plaintext exactly. The cleaned key text is much longer than the plaintext, so only the first 12 letters participate in the encryption.
Common Mistakes to Avoid
First, using a key text shorter than the plaintext. The running key cipher degenerates to a Vigenere cipher if the key text repeats, and the Vigenere cipher is broken by the Kasiski examination. Always ensure the key text is at least as long as the plaintext.
Second, using a key text that the attacker can guess. Common mistakes include using the Bible, the Constitution, or a popular poem as the key text. Cryptanalysts have databases of these texts and routinely try them as candidate keys. Use a less famous text or, better, use a truly random one time pad.
Third, leaving spaces in the ciphertext. Spaces reveal word boundaries and give the attacker free information about the plaintext structure. Strip spaces during encryption and reinsert them after decryption if needed.
Fourth, advancing the key index for non-letter characters in the plaintext. If the plaintext has a space and you advance the key index, the key alignment is broken. Either clean both texts first, or only advance the key index when you encounter a letter.
Real-World Applications
The running key cipher is not used in production today. Its weakness is that natural language text has low entropy, which means the cipher can be broken by finding the most likely plaintext and key text pair that produces the observed ciphertext. The classical attack is to look for runs where the ciphertext can be decomposed into two readable English texts, one of which is the plaintext.
The running key cipher was used historically by Soviet intelligence during World War II and the early Cold War. The Venona project, run by the U.S. Army's Signal Intelligence Service, successfully broke many of these ciphers by exploiting the redundancy of natural language key text. The breaks led to the identification of several spies, including Julius and Ethel Rosenberg.
In modern systems, the principle of using a long non-repeating key lives on in the one time pad and in stream ciphers such as ChaCha20 and AES-CTR. These modern constructions replace the low entropy natural language key text with a high entropy pseudorandom stream, which removes the principal weakness of the running key cipher.
The running key cipher is also a useful teaching step between the Vigenere cipher and the one time pad. It demonstrates that the security of a polyalphabetic cipher depends on both the length and the entropy of the key. Once you understand why the running key cipher is breakable despite its long key, you understand why the one time pad requires a truly random key.
In capture the flag events, running key puzzles are popular because the attacker must find the source text used as the key. Online book databases such as Project Gutenberg are a common source of candidate key texts, and automated tools can try thousands of books per second.
Conclusion
The running key cipher is a polyalphabetic cipher with a key text as long as the plaintext. We implemented encrypt and decrypt using algebraic addition and subtraction, validated that the key text is long enough, and ran a round trip. Next you should study the one time pad (which uses a truly random key), the autokey cipher (which uses the plaintext itself as part of the key), and the stream cipher (which generates the key stream from a short seed).
Comments
Comments
Post a Comment