Ceaser Cipher
Definition
- A substitution cipher that shifts each letter in the plaintext by a
fixed number of positions down or up the alphabet.
- Named after Julius Caesar, who used it for his private
correspondence.
- Simple and widely known encryption technique.
Application
- Historically used for military and personal communication.
- Modern usage includes educational purposes to introduce
cryptography concepts.
- Used in simple puzzles and games.
- Not suitable for securing sensitive information due to
vulnerability to brute-force attacks.
Examples
- Encryption Example:
- Plaintext: "HELLO"
- Shift: 3
- Ciphertext: "KHOOR"
- Decryption Example:
- Ciphertext: "KHOOR"
- Shift: 3
- Plaintext: "HELLO"
- Handling Non-Alphabet Characters:
- Plaintext: "HELLO, WORLD!"
- Shift: 3
- Ciphertext: "KHOOR, ZRUOG!"
Related Algorithms
- ROT13:
- A specific case of the Caesar Cipher with a shift of 13.
- Applying ROT13 twice returns the original text.
- Atbash Cipher:
- Each letter in the alphabet is mapped to its reverse (e.g., 'A'
becomes 'Z').
- Vigenère Cipher:
- Uses a keyword to apply multiple Caesar Ciphers based on the
letters of the keyword.
- Provides better security by varying the shift value.
- Affine Cipher:
- Combines the Caesar Cipher with a multiplicative step.
- Uses a mathematical function of the form E(x)=(ax+b) mod m to transform each letter.
def caesar_cipher(text, shift, mode='encrypt'):
if mode ==
'decrypt':
shift = -shift
result = ''
for char in text:
if
char.isalpha():
shift_base
= 65 if char.isupper() else 97
result +=
chr((ord(char) - shift_base + shift) % 26 + shift_base)
else:
result +=
char
return result
# Usage
message = "Hello, World!"
shift = 3
encrypted_message = caesar_cipher(message, shift, 'encrypt')
decrypted_message = caesar_cipher(encrypted_message, shift,
'decrypt')
print(f"Encrypted: {encrypted_message}")
print(f"Decrypted: {decrypted_message}")