4.5.6.10 - Encryption, Caesar and Vernam ciphers (Python)

4.5.6.10 - Encryption, Caesar and Vernam ciphers (Python)

Encryption changes a readable message into ciphertext using a cipher and a key. Learn to apply Caesar and Vernam ciphers and explain their very different security for Paper 2. The Python exploration and source-writing tasks are formative companion applications; the written exam responses use the actual cipher representations.

Plaintext, ciphertext and keys

Encryption protects the meaning of a message while it is stored or transmitted, even when someone can obtain its encrypted form.

Encryption

Encryption transforms plaintext, the original readable data, into ciphertext, an encrypted representation, using a cipher and a key. Decryption reverses that transformation with the appropriate key.

A cipher is the algorithm or rule. The key selects a particular transformation within that rule. For example, “shift every letter around the alphabet” describes a cipher; “shift by three places” supplies its key. A recipient must know the cipher and the appropriate decryption key to recover the plaintext. Knowing the algorithm alone should not make a strong cipher insecure: the secret normally lies in the key.

Encryption is not the same as changing a character into its public character code. A public encoding such as ASCII represents data without concealing its meaning. Encryption is reversible with the correct key; a hash is not a decryption method. Neither an unreadable appearance nor a complicated-looking algorithm establishes security.

When a question supplies an unfamiliar cipher, follow its stated transformation and inverse. Do not assume that every cipher uses an alphabet shift. You may be asked to apply a new rule of similar complexity, with its details given.

Caesar substitution

A Caesar cipher substitutes each letter with the letter a fixed number of places further around the alphabet, wrapping after Z.

For this lesson use uppercase A–Z, numbered A = 0 through Z = 25. Leave other characters unchanged. With shift 3, Z becomes C, O becomes R, and the plaintext ZOO becomes CRR. To decrypt CRR, move back three places: C becomes Z and R becomes O.

For letter number pp, ciphertext number cc and shift ss:

c=(p+s) mod 26,p=(c−s) mod 26.c=(p+s)\bmod 26,\qquad p=(c-s)\bmod 26.

Modulo 26 brings the result back into the range 0–25, including when subtraction gives a negative intermediate value.

In Python, ord(ch) - ord("A") converts an uppercase letter to its number. chr(number + ord("A")) converts a number back to a letter. % 26 supplies the wrap. The explicit range check below preserves spaces, digits and punctuation rather than accidentally treating them as letters.

Observe wrapping and reverse the shift

{"code":"def caesar(text, shift):\n result = ""\n for ch in text:\n if "A" <= ch <= "Z":\n number = ord(ch) - ord("A")\n result += chr((number + shift) % 26 + ord("A"))\n else:\n result += ch\n return result\n\nmessage = "ZOO, AT 6!"\nshift = 3\nciphertext = caesar(message, shift)\nprint(ciphertext)\nprint(caesar(ciphertext, -shift))\n","instructions":"Predict both output lines, including the wrap at Z. Run the program. Then change shift from 3 to 1, predict the new ciphertext, and run again. Explain why the second line still recovers the original message.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}

Caesar encryption is easily cracked. There are only 26 distinct shifts, including the identity shift, or 25 non-zero shifts. An attacker can try each and recognise readable language. The same plaintext letter always produces the same ciphertext letter, so letter frequencies and repeated patterns are also retained. A longer message does not create a larger key space.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account