Skip to content

Commit

Permalink
Update 20240618_0.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Pochi-Liberluna authored Jun 19, 2024
1 parent f2faf89 commit 190ec57
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/pages/blog/posts/20240618_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,56 @@ thumbnail: "https://github.com/Liberluna/liberluna.github.io/tree/main/public/as
| Tag | Beginner |
| Desc | Using a safe prime makes RSA secure, doesn't it? |
| 配布ファイル | Safe_Prime.tar.gz 774280c0d7d278ed01f537b13014fa15b4dc1d3a |
ソースコード
```python
import os
from Crypto.Util.number import getPrime, isPrime

$n = p \cdot (2 p + 1)$ で構成されています。
FLAG = os.getenv("FLAG", "ctf4b{*** REDACTED ***}").encode()
m = int.from_bytes(FLAG, 'big')

while True:
p = getPrime(512)
q = 2 * p + 1
if isPrime(q):
break

n = p * q
e = 65537
c = pow(m, e, n)

print(f"{n = }")
print(f"{c = }")
```
$n = p \cdot (2 p + 1)$ で表されます。
</br>
$n$ が $p$ のみで決定するので,二分探索します・
$n$ から $p$ を求めることができるのでsolverを書きます。
</br>
※PyCryptoDomeを使っても解けます。
```python
from sympy import nextprime, isprime
from math import isqrt

n = 292927367433510948901751902057717800692038691293351366163009654796102787183601223853665784238601655926920628800436003079044921928983307813012149143680956641439800408783429996002829316421340550469318295239640149707659994033143360850517185860496309968947622345912323183329662031340775767654881876683235701491291
c = 40791470236110804733312817275921324892019927976655404478966109115157033048751614414177683787333122984170869148886461684367352872341935843163852393126653174874958667177632653833127408726094823976937236033974500273341920433616691535827765625224845089258529412235827313525710616060854484132337663369013424587861

p = isqrt(n // 2)
while True:
q = 2 * p + 1
if n % p == 0 and n % q == 0 and n == p * q:
break
p = nextprime(p)

q = 2 * p + 1
phi = (p - 1) * (q - 1)
e = 65537
d = pow(e, -1, phi)

m = pow(c, d, n)
flag = m.to_bytes((m.bit_length() + 7) // 8, 'big').decode()

print("FLAG:", flag)
```
```
ctf4b{R3l4ted_pr1m3s_4re_vuLner4ble_n0_maTt3r_h0W_l4rGe_p_1s}
```

0 comments on commit 190ec57

Please sign in to comment.