> 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/rsa/12.-short-pad-attack.md).

# Short Pad Attack (Low Exponent + Small Padding)

This attack applies when RSA uses a **small exponent (usually `e = 3`)** and messages are padded with a **small random value**.

```
M1 = m + r1
M2 = m + r2
```

where:

```
m  = original message (same)
r1, r2 = small padding values
```

Encryption:

```
C1 = (m + r1)^e mod n
C2 = (m + r2)^e mod n
```

Goal:

```
Recover m
```

## 2. Core Idea

The padding is **too small**, so:

```
Difference between messages is small
```

This allows us to:

```
Treat it like a related message attack
```

But instead of known relation:

```
M2 = M1 + k   (known k)
```

we have:

```
M2 = M1 + (r2 − r1)   (unknown but small)
```

## 3. Mathematical Setup

Let:

```
x = m
```

Then:

```
C1 = (x + r1)^e mod n
C2 = (x + r2)^e mod n
```

Define:

```
Δ = r2 − r1   (small)
```

So:

```
(x + r2) = (x + r1 + Δ)
```

## 4. Key Insight

```
Δ is small → brute-force possible
```

Once Δ is guessed:

```
M2 = M1 + Δ
```

Now it becomes:

```
Franklin–Reiter attack
```

## 5. Attack Strategy

1. Loop over small Δ
2. Assume:

```
M2 = M1 + Δ
```

3. Build:

```
f(x) = x^e − C1
g(x) = (x + Δ)^e − C2
```

4. Compute:

```
gcd(f, g)
```

5. If valid:

```
Recover x = m
```

## 6. Python (SageMath) Implementation

```python
# SageMath required

n = ...
e = 3

C1 = ...
C2 = ...

R.<x> = PolynomialRing(Zmod(n))

for delta in range(1, 1000):  # small padding search
    f = x^e - C1
    g = (x + delta)^e - C2

    h = f.gcd(g)

    if h.degree() == 1:
        m = -h[0]
        print("Found m:", m)
        break
```

## 7. Why This Works

```
Small padding → small Δ
Small Δ → brute-force feasible
After guess → becomes algebraic (Franklin-Reiter)
```

## 8. When to Use

Look for:

```
Same message encrypted twice
Small random padding
Same n and e
e = 3 (very common)
```

Hints like:

```
"short padding"
"random suffix"
"tiny noise added"
```

## 9. Important Notes

```
Works only if padding difference is small
Requires polynomial GCD → use Sage
Fails if padding is large or randomized properly
```

## 10. Example (Concept)

```
m = 100
r1 = 2 → M1 = 102
r2 = 5 → M2 = 105
```

```
Δ = 3
```

We brute Δ:

```
Try Δ = 3 → success
```

## 11. Complexity

```
O(small_padding_range)
```

Usually:

```
< 2^16 or smaller
```

## 12. Summary

```
Same message + small padding
→ guess Δ
→ reduce to Franklin-Reiter
→ recover m
```

## 13. Practice Problem

```
n = 18721
e = 3

C1 = 10652
C2 = 16021

Message padded with small values (difference < 50)
```

## Task

```
Recover original message m
```


---

# 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/rsa/12.-short-pad-attack.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.
