> For the complete documentation index, see [llms.txt](https://sayonara.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sayonara.gitbook.io/writeups/ctf/nitectf2023/crypto/babyrsa.md).

# babyRSA

<div align="left"><figure><img src="/files/NoT14DYbI3PQBfUksMqK" alt=""><figcaption></figcaption></figure></div>

### Challenge Files :&#x20;

{% file src="/files/TTUoYUh3jFL5v20gu4t6" %}

{% file src="/files/AYPuadvuGLxJQpKN9Xzs" %}

### <mark style="color:red;">RSA ATTACK :</mark>&#x20;

same public exponent `(e=37` is used on the same message M during encryption, then CRT `(Chinese Remainder Theorem)` can be used to obtain M^e using each combination.

### <mark style="color:red;">Python Script :</mark>&#x20;

i took the script from MxRy and modified it to work for me you can get the original script from here <https://github.com/MxRy/rsa-attacks/blob/master/hastad-attack.py>

```python
import math
import functools
import gmpy2

# Get the Flag
def ReverseX(x, e) :
	m = gmpy2.iroot(x, e)[0]
	size = int(math.log(m, 10) + 1)
	print("Flag : \n"+"".join([chr((m >> j) & 0xff) for j in reversed(range(0, size << 3, 8))]))
	return 1	


# Step-by-step CRT implementation (cf. demo precedente)
def CrtComputation(C_i, N_i) :
	mu = list()
	nu = list()
	e = list()
	(x_i, N) = (0, 1)
	"""
	Calcul mu_i = PI_(j=1,j!=i)^k(n_j)
	"""
	N = functools.reduce(lambda a, b: a*b, N_i)
	for n_i in N_i :
		mu.append(N//n_i)
	"""
	Calcul nu_i inverse modulo n_i des mu_i
	useful link :
	https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
	"""
	for mu_i, n_i in zip(mu, N_i) :
		nu.append(gmpy2.invert(mu_i, n_i))
	"""
	Calcul e_i = nu_i * mu_i
	"""
	for mu_i, nu_i in zip(mu, nu) :
		e.append(mu_i*nu_i)
	"""
	Calcul et retour de x
	"""	
	for e_i, a_i in zip(e, C_i) :	
		x_i += e_i*a_i
	x = x_i % N
	return x 

def HastadAttack(C, N, e):
	x = CrtComputation(C, N)
	return ReverseX(x, e)

def main():	
    e = 37
    C = [... 37 element ] # put here the c array from the output.txt file
    N = [... 37 element ] # put here the n array from the output.txt file

    HastadAttack(C, N, e)

if __name__ == "__main__":
    main()
```

running the script we get the flag

<div align="left"><figure><img src="/files/zkZo0ME1t0HYlPoDl6yH" alt=""><figcaption></figcaption></figure></div>

### <mark style="color:red;">Flag :</mark>&#x20;

```
nite{y0u_C@n_N3v3r_Gu3s5!!!}
```


---

# 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://sayonara.gitbook.io/writeups/ctf/nitectf2023/crypto/babyrsa.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.
