4.5.6.10 - Encryption, Caesar and Vernam ciphers (Java)
Learn how Caesar substitution and Vernam XOR transform a message, recover the original message and offer very different kinds of security.
This is Paper 2 written practice. Java examples and code answers have no Run. Java route status: incomplete: runnable support pending.
Java implementation exercises are formative companion applications. Paper 2 requires understanding and applying the cipher representations; it does not require writing these Java methods.
Plaintext, ciphertext and keys
Encryption changes a readable message into a form that an unauthorised reader should not be able to understand.
Encryption
The transformation of plaintext into ciphertext using a cipher, usually controlled by a key.
Plaintext is the original message. Ciphertext is the transformed message. A cipher is the algorithm specifying the transformation; a key supplies the value that selects a particular transformation. Decryption recovers the plaintext using the appropriate algorithm and key. A message can still be represented by letters after encryption: the important distinction is what those symbols mean, not whether they look like binary.
Suppose a cipher shifts each alphabet letter three places forwards. The cipher is “shift letters”; the key is 3. CAT becomes FDW. The receiver shifts each letter three places backwards to recover CAT. An attacker may know the algorithm; keeping the key secret should provide the protection. A fixed public encoding such as ASCII is a representation, not an encryption system with a secret key.
Encryption aims to protect confidentiality. It does not by itself show who sent a message or guarantee that the ciphertext has not been changed. A receiver also needs a secure way to obtain and protect the key. Sending an unprotected key beside its ciphertext defeats the intended secrecy.
Caesar substitution
A Caesar cipher replaces every alphabet letter with the letter a fixed number of positions further around the alphabet.
Use positions A = 0, B = 1, …, Z = 25. With a key from 0 to 25, encryption adds and wraps around after 25; decryption subtracts and wraps around below zero.
Here and are letter positions. Adding 26 in this decryption expression keeps the dividend non-negative for the stated key range.
For key 4, ZOO encrypts to DSS: Z has position 25, and , which is D. O has position 14 and becomes S, position 18. To decrypt D, calculate , giving Z. Wraparound matters in both directions.
This Java program handles uppercase A–Z, preserving all other characters unchanged. Subtracting 'A' obtains the zero-based letter position. Adding 'A' converts a position back to a character code; the cast to char is required for the resulting integer expression.
public class Main {
static String encrypt(String text, int key) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
int position = ch - 'A';
result.append((char) ('A' + (position + key) % 26));
} else {
result.append(ch);
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(encrypt("ZOO!", 4));
}
}
Trace before reading on: the three letters append D, S and S, and the exclamation mark follows the else branch. The output is DSS!. On paper, amend the expression to (position - key + 26) % 26 and predict its result for ciphertext DSS!, key 4: ZOO!. Java % is a remainder operator; a negative dividend can give a negative remainder, so omitting + 26 is unsafe here.
A Caesar cipher has only 26 possible shifts including the unchanged message. An attacker can try every key quickly and look for meaningful text. It also preserves repeated-letter patterns and the frequency pattern of the original language, merely relabelling the letters. A longer secret message is therefore not automatically safer.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account