from Crypto.Util.number import getPrime, bytes_to_long with open("flag.txt", "rb") as f: flag = f.read() p, q = getPrime(1024), getPrime(1024) N = p * q M = 2 * p + q e = 0x10001 def encrypt(plaintext: bytes) -> int: plaintext = bytes_to_long(plaintext) c = pow(plaintext, e, N) return c if __name__ == "__main__": c = encrypt(flag) print(f"{N = }") print(f"{M = }") print(f"{e = }") print(f"{c = }")
がもらえる
と考えて phiがわかっているときの素因数分解 をやる
with open("output.txt") as f: N = int(f.readline().strip().split(" = ")[1]) M = int(f.readline().strip().split(" = ")[1]) e = int(f.readline().strip().split(" = ")[1]) c = int(f.readline().strip().split(" = ")[1]) from gmpy2 import isqrt N = 2*N p = int((M + isqrt(M**2 - 4*N)) // 2) q = int((M - isqrt(M**2 - 4*N)) // 2) N = N // 2 p = p // 2 d = pow(e, -1, (p-1)*(q-1)) m = pow(c, d, N) print(m.to_bytes(100, "big"))