> 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/03.-breaking-rsa.md).

# 03. Breaking RSA

## Breaking RSA — Basic Attacks

RSA security depends on the difficulty of factoring large numbers from cryptography. If this assumption fails, RSA can be broken.

## 1. Factorization Attack (Classic)

### Idea

```
If n = p × q can be factored → RSA is broken
```

### Steps

```
1. Factor n → get p, q
2. Compute φ(n) = (p − 1)(q − 1)
3. Compute d ≡ e⁻¹ mod φ(n)
4. Decrypt M = C^d mod n
```

#### Given

```
n = 55
e = 3
C = 10
```

#### Solution

```
n = 5 × 11

φ(n) = 40

d = 27

M = 10^27 mod 55 = 10
```

### Python Code

```python
from sympy import factorint, mod_inverse

n = 55
e = 3
C = 10

# Factor n
factors = factorint(n)
p, q = list(factors.keys())

# Compute phi
phi = (p - 1) * (q - 1)

# Compute d
d = mod_inverse(e, phi)

# Decrypt
M = pow(C, d, n)

print("Message:", M)
```

<img src="https://github.com/user-attachments/assets/be15249e-face-4feb-84fa-0ad9e4979fe5" alt="image" height="569" width="1412">

## 2. When Does This Work?

### Works When

```
n is small
n has small factors
poor key generation
```

### Fails When

```
n is very large (2048-bit)
primes are strong and random
```

## 3. Key Insight

```
RSA is secure only because factoring n is hard
```


---

# 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/03.-breaking-rsa.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.
