> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/ciphers/caeser-cipher.md).

# Caesar Cipher – Complete Notes

## 1. What is Caesar Cipher?

The **Caesar Cipher** is a **substitution cipher** where each letter in the plaintext is shifted by a fixed number of positions in the alphabet.

It is named after Julius Caesar, who used it to send secret messages.

## 2. Basic Idea

* Choose a number → called the **key (shift)**
* Shift each letter forward by that number

### Example (Key = 3)

```
Plaintext:  HELLO
Ciphertext: KHOOR
```

Because:

```
H → K (shift +3)
E → H
L → O
L → O
O → R
```

## 3. Alphabet Mapping

Alphabet is treated as circular:

```
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 shift = 3:

```
A → D
B → E
C → F
...
X → A
Y → B
Z → C
```

***

## 4. Mathematical Representation

Convert letters to numbers:

```
A = 0, B = 1, C = 2, ..., Z = 25
```

***

### Encryption Formula

```
C = (P + k) mod 26
```

Where:

* `C` = ciphertext letter
* `P` = plaintext letter (number)
* `k` = key (shift)
* `mod 26` = wrap around alphabet

***

### Decryption Formula

```
P = (C - k) mod 26
```

***

## 5. Example with Math

Encrypt **HELLO** with key = 3:

```
H = 7 → (7 + 3) mod 26 = 10 → K
E = 4 → (4 + 3) = 7 → H
L = 11 → (11 + 3) = 14 → O
O = 14 → (14 + 3) = 17 → R
```

***

## 6. Encryption (Step-by-Step)

1. Choose key `k`
2. Convert each letter to number
3. Apply formula: `(P + k) mod 26`
4. Convert back to letters

***

## 7. Decryption (Step-by-Step)

1. Take ciphertext
2. Convert letters to numbers
3. Apply: `(C - k) mod 26`
4. Convert back to plaintext

***

## 8. Brute Force Attack

Since only **26 possible keys**, attacker can try all shifts:

```
Key 1 → text
Key 2 → text
...
Key 25 → text
```

This is why Caesar Cipher is **not secure**.

***

## 9. Frequency Analysis

In English:

* Most common letter = **E**

If ciphertext has many `K`, maybe:

```
K → E → shift = 6
```

This helps guess the key.

***

## 10. Python Implementation

### Encryption

```python
def encrypt(text, k):
    result = ""
    for char in text:
        if char.isalpha():
            shift = ord('A') if char.isupper() else ord('a')
            result += chr((ord(char) - shift + k) % 26 + shift)
        else:
            result += char
    return result
```

***

### Decryption

```python
def decrypt(text, k):
    return encrypt(text, -k)
```

***

## 11. Brute Force Script

```python
def brute_force(text):
    for k in range(26):
        print(f"Key {k}: {encrypt(text, -k)}")
```

***

## 12. Key Points for CTFs

* If text looks like: `uryyb` → likely Caesar/ROT13
* Try:

```bash
tr 'a-zA-Z' 'n-za-mN-ZA-M'
```

* Use tools:
  * CyberChef
  * dcode.fr
* Look for readable English output

***

## 13. Limitations

* Very weak encryption
* Only 26 keys
* Easily broken with brute force or frequency analysis

***

## 14. Variants

* **ROT13** → special case where key = 13
* **Shift cipher** → general form of Caesar cipher

***

## Final Summary

* Caesar Cipher = shift letters
* Uses modular arithmetic (mod 26)
* Easy to implement, easy to break
* Important for understanding basic cryptography concepts

***

If you want, next I can give you:

* **Vigenère Cipher (next level)**
* or **how to detect Caesar instantly in CTFs (pro tricks)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/ciphers/caeser-cipher.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
