32 lines
654 B
Python
32 lines
654 B
Python
#!/usr/bin/env python3
|
|
|
|
from Cryptodome.Util.number import getPrime, inverse, bytes_to_long, long_to_bytes, GCD
|
|
from secret import flag
|
|
|
|
assert len(flag) == 23
|
|
|
|
e = 3
|
|
|
|
# n will be 8 * (100 + 100) = 1600 bits strong which is pretty good
|
|
while True:
|
|
#getPrime is 100 bits not 100 Bytes
|
|
p = getPrime(100)
|
|
q = getPrime(100)
|
|
phi = (p - 1) * (q - 1)
|
|
d = inverse(e, phi)
|
|
if d != -1 and GCD(e, phi) == 1:
|
|
break
|
|
|
|
n = p * q
|
|
|
|
pt = bytes_to_long(flag)
|
|
ct = pow(pt, e, n)
|
|
|
|
print(f"n = {n}")
|
|
print(f"e = {e}")
|
|
print(f"ct = {ct}")
|
|
|
|
pt = pow(ct, d, n)
|
|
decrypted = long_to_bytes(pt)
|
|
assert decrypted == flag
|