Week 2: Caesar Cipher - M199205zn/IAS-CS4 GitHub Wiki
The Caesar Cipher is one of the oldest and simplest forms of encryption techniques, named after Julius Caesar, who is said to have used it to protect his private messages. It is a type of substitution cipher, where each letter in the plaintext is shifted a certain number of places down or up the alphabet.
How the Caesar Cipher Works:
-
Choose a shift value (key): A number is chosen that determines how many positions each letter in the plaintext will be shifted in the alphabet. For example, a shift of 3 means each letter will be replaced by the letter that is three positions ahead of it in the alphabet.
-
Encrypt the plaintext: For each letter in the plaintext, replace it with the letter that comes after it by the number of positions specified by the shift value. If the shift goes beyond 'Z', it wraps around to the beginning of the alphabet.
-
Form the ciphertext: The result of applying the shift to each letter is the ciphertext.
Example:
Let's consider a shift of 3 and the plaintext "HELLO".
- The letters of the alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
- With a shift of 3, 'A' becomes 'D', 'B' becomes 'E', and so on.
For the message "HELLO":
- H -> K
- E -> H
- L -> O
- L -> O
- O -> R
The ciphertext becomes "KHORR".
Decryption:
To decrypt the message, you perform the opposite operation by shifting each letter back by the same number of positions used for encryption.
For the ciphertext "KHORR" and a shift of 3:
- K -> H
- H -> E
- O -> L
- O -> L
- R -> O
The original plaintext is "HELLO".
Generalized Steps for Caesar Cipher:
-
Encryption:
- Convert each letter in the plaintext to its corresponding numerical value (A = 0, B = 1, ..., Z = 25).
- Apply the shift: ( \text{Ciphertext letter} = (\text{Plaintext letter number} + \text{shift}) \mod 26 ).
- Convert the resulting numbers back to letters.
-
Decryption:
- Convert each letter in the ciphertext to its numerical value.
- Apply the reverse shift: ( \text{Plaintext letter} = (\text{Ciphertext letter number} - \text{shift}) \mod 26 ).
- Convert the numbers back to letters.
Example with Key = 3 (Shift):
- Plaintext: "HELLO"
- Ciphertext: "KHORR"
- Shift: 3
Strength and Weaknesses:
-
Strength:
- The Caesar cipher is simple to implement and understand, making it a good educational tool for teaching basic encryption concepts.
-
Weaknesses:
- The cipher is easily broken because there are only 25 possible shifts (ignoring shifts of 0 and 26, which would leave the message unchanged). This makes it vulnerable to a brute-force attack, where all possible shifts are tried.
- The cipher also does not change the frequency distribution of letters in the text, so frequency analysis can easily break it, especially for longer texts.
Variations:
- Affine Cipher: This is a generalization of the Caesar cipher where each letter is encrypted using a mathematical function like ( (Ax + B) \mod 26 ), where ( A ) and ( B ) are integers and ( A ) is coprime with 26.
- Rot13: A variant of the Caesar cipher where the shift is exactly 13 positions, often used in forums or online to obscure text temporarily.
Despite its simplicity, the Caesar Cipher has been historically significant and forms the basis for more complex encryption techniques.