Ceaser Cipher

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

  1. Encryption Example:
    • Plaintext: "HELLO"
    • Shift: 3
    • Ciphertext: "KHOOR"
  2. Decryption Example:
    • Ciphertext: "KHOOR"
    • Shift: 3
    • Plaintext: "HELLO"
  3. Handling Non-Alphabet Characters:
    • Plaintext: "HELLO, WORLD!"
    • Shift: 3
    • Ciphertext: "KHOOR, ZRUOG!"

Related Algorithms 

  1. ROT13:
    • A specific case of the Caesar Cipher with a shift of 13.
    • Applying ROT13 twice returns the original text.
  2. Atbash Cipher:
    • Each letter in the alphabet is mapped to its reverse (e.g., 'A' becomes 'Z').
  3. 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.
  4. 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.


Python code :

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}")




Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)