-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path8-getflag.py
42 lines (34 loc) · 2.27 KB
/
8-getflag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
from Crypto.PublicKey import RSA
from Crypto.Util import asn1
import binascii
# Given
p = 153143042272527868798412612417204434156935146874282990942386694020462861918068684561281763577034706600608387699148071015194725533394126069826857182428660427818277378724977554365910231524827258160904493774748749088477328204812171935987088715261127321911849092207070653272176072509933245978935455542420691737433
c = 2887512232927570212720289007617218056059430602904994311768865248937186092346045634461113653200410008027930318598899961967398819457005578177750855122616645816143754424929892030705859313377266089203195245015545475756780591749438383925678961618918887123573182347018588636929367129348683955602230289203565347486263264786442219851846011532640387672302559499309153498019746553031047474066734422816867560702848158992070366148525244203275301204388654920924623403169692782505561391649873520950447510664771965559666379810356768249845746489087039846408953680555639864400302957371546072298362982071553184164689418758473539792781
e = 65537
n = 23952937352643527451379227516428377705004894508566304313177880191662177061878993798938496818120987817049538365206671401938265663712351239785237507341311858383628932183083145614696585411921662992078376103990806989257289472590902167457302888198293135333083734504191910953238278860923153746261500759411620299864395158783509535039259714359526738924736952759753503357614939203434092075676169179112452620687731670534906069845965633455748606649062394293289967059348143206600765820021392608270528856238306849191113241355842396325210132358046616312901337987464473799040762271876389031455051640937681745409057246190498795697239
# Calculate
q = n // p
phi = (p-1) * (q-1)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
g, y, x = egcd(b%a,a)
return (g, x - (b//a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('No modular inverse')
return x%m
d = modinv(e, phi)
# get plaintext
priv_key = RSA.construct((n, e, d))
m = priv_key.decrypt(c)
print(m)
# If you convert the last plaintext to a hex number, then ascii, you'll find what you're searching for ;)
def hex_pair(x):
return ('0' * (len(x) % 2)) + x
m_hex = '{:x}'.format(m)
m_hex = hex_pair(m_hex)
msg = binascii.unhexlify(m_hex)
print(msg.decode())