katanya sih ini salah satu challenge paling common di crypto
- chall.py
from Crypto.Util.number import getPrime, isPrime, bytes_to_long
from secret import flag
p = getPrime(512)
q = p+pow(10, 10)
while not isPrime(q):
q+=1
n = p*q
e = 65537
c = pow(bytes_to_long(flag.encode()), e, n)
w = open('yupps.txt', 'w')
w.write('''n :{}
e :{}
c :{}
'''.format(n, e, c))
w.close()
- yupps.txt
n :49395104140655658514748571842080326914292796936916928698864042986374411298172218017902749917436512276989151397882527274877230879505860589222329619862809739982766514159792938698753211409320684569464486446070544232700671329040497669816428170571531694983615346689735470529160952805280642456928633093385042290903
e :65537
c :30781133348334449003346660743488576896189374900675641186739706292512857823016060656024278245283188842883530944647636039309667044428404383073597120995244881437966593912379874459080502668399875256503185287402082539930387628744251784107740046626292203599846152655564451412867273342609548028692795413644948191855
Di dalam file txt tersebut, disediakan nilai n, e, dan c. Untuk melakukan faktorisasi n, saya menggunakan fungsi factorint() dari library sympy.ntheory. Berikut kode lengkapnya
from Crypto.Util.number import *
from Crypto.PublicKey import RSA
from sympy.ntheory import factorint
c = 30781133348334449003346660743488576896189374900675641186739706292512857823016060656024278245283188842883530944647636039309667044428404383073597120995244881437966593912379874459080502668399875256503185287402082539930387628744251784107740046626292203599846152655564451412867273342609548028692795413644948191855
n = 49395104140655658514748571842080326914292796936916928698864042986374411298172218017902749917436512276989151397882527274877230879505860589222329619862809739982766514159792938698753211409320684569464486446070544232700671329040497669816428170571531694983615346689735470529160952805280642456928633093385042290903
e = 65537
# factorization
pq = factorint(n)
p, q = pq.keys()
phi = (p-1)*(q-1) #find phi
d = inverse(e,phi) #find d
msg = pow(c,d,n) #decrypt c -> msg = (c to the d) mod n
print(long_to_bytes(msg)) #convert to byte string and print output
hackfest0x5{nev3r_use_2_cl0se_pr1me_in_rSa}
🏷️tags: RSA, close prime