Caesar Cipher

Nerd Cafe

The Caesar Cipher is one of the simplest and most widely known encryption techniques. It shifts each letter in the plaintext by a fixed number of places down the alphabet. Let’s go step by step:

Step 1: Understand the Concept

  • Encryption: Each letter in the plaintext is shifted by a fixed number (key). For example, with a shift of 3, A becomes D, B becomes E, and so on.

  • Decryption: Reverse the process by shifting letters back by the same key.

Step 2: Choose the Key

The key is the number of positions each letter is shifted. For instance, a key of 3 means each letter in the plaintext will move three steps forward.

Step 3: Define the Alphabet

The standard alphabet is used:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

If you encounter lowercase letters, treat them similarly:

abcdefghijklmnopqrstuvwxyz

Step 4: Encryption Formula

Use the formula:

Ci=(Pi+K)β€…β€Šβ€…β€Šmodβ€…β€Šβ€…β€Š26C_{i}=(P_{i}+K)\;\;mod\;\;26

Where:

  • 𝐢𝑖: Ciphertext letter (encrypted letter)

  • 𝑃𝑖: Plaintext letter (original letter)

  • 𝐾: Key (shift value)

  • 26: Total letters in the English alphabet

Step 5: Decryption Formula

Reverse the encryption formula:

Pi=(Ciβˆ’K)β€…β€Šβ€…β€Šmodβ€…β€Šβ€…β€Š26P_{i}=(C_{i}-K)\;\;mod\;\;26

Step 6: Practical Example

  • Plaintext: "HELLO"

  • Key: 3

Step 6.1: Encryption

Take each letter and shift it by 3:

  • H β†’ K

  • E β†’ H

  • L β†’ O

  • L β†’ O

  • O β†’ R

Ciphertext: "KHOOR"

Step 6.2: Decryption

Take each letter in the ciphertext and shift it back by 3:

  • K β†’ H

  • H β†’ E

  • O β†’ L

  • O β†’ L

  • R β†’ O

Decrypted Text: "HELLO"

Step 7: Implementation in Python

Output

Notes

  • Non-alphabetic characters are typically not encrypted but can be handled based on specific requirements.

  • The Caesar Cipher is not secure for modern cryptography but is excellent for understanding the basics of encryption.

Last updated