> For the complete documentation index, see [llms.txt](https://wiki.ainzzorl.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.ainzzorl.lol/tech/algorithms/rsa.md).

# RSA

* [The original paper.](http://people.csail.mit.edu/rivest/Rsapaper.pdf)
* <https://en.wikipedia.org/wiki/RSA_(cryptosystem)>

### Method

* Choose random semi-prime n = p \* q
* Choose random e: gcd(e, (p-1)(q-1)) = 1
* d: ed = 1 mod (p-1)(q-1)

Public key: (e, n)

Private key: (d, n)

Encrypt: E(M) = M^e mod n

Decrypt: D(C) = C^d mod n

### Math

D(E(M)) = M is proved based on Fermat's little theorem or Euler's theorem.

(p-1)(q-1) is the Euler's totient number for n (number of integers less than n co-prime with it).

Knowing p and q, it's trivial to compute d from e. Not knowing: supposedly hard.

### Attacks and Stuff

* There are many ways to choose weak keys.
  * <https://github.com/Ganapati/RsaCtfTool>
  * <https://www.sjoerdlangkemper.nl/2019/06/19/attacking-rsa/>
* Needs strong random number generator, otherwise can be factorized.
* Needs good padding.
  * <https://en.wikipedia.org/wiki/Semantic_security>
  * <https://en.wikipedia.org/wiki/Padding_(cryptography)>

### RSA Problem

* ["If we consider the problem of finding the private exponent 𝑑 then it is proven by Miller to be computationally equivalent to factoring 𝑛;"](https://crypto.stackexchange.com/questions/89883/is-it-proven-that-breaking-rsa-is-equivalent-to-factoring-as-of-2021)
  * [Breaking RSA Generically is Equivalent to Factoring.](https://eprint.iacr.org/2008/260.pdf)
* Decrypting a single cyphertext: not necessarily as difficult as factoring. It probably isn't.

### Format

DER: binary format; modulo concatenated with exponent.

PEM: base64-encoded DER.

<https://tls.mbed.org/kb/cryptography/asn1-key-structures-in-der-and-pem>


---

# 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://wiki.ainzzorl.lol/tech/algorithms/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.
