ALL the CTFS of Crypto2025 finally
This commit is contained in:
56
crypto-asimmetric/RSA-2/attack.py
Normal file
56
crypto-asimmetric/RSA-2/attack.py
Normal file
@ -0,0 +1,56 @@
|
||||
from Cryptodome.Util.number import bytes_to_long
|
||||
from Cryptodome.Util.number import isPrime
|
||||
from Cryptodome.Util.number import getPrime
|
||||
from Cryptodome.Util.number import getRandomInteger
|
||||
|
||||
from gmpy2 import next_prime
|
||||
from gmpy2 import isqrt
|
||||
# p = getPrime(512)
|
||||
# q = next_prime(p)
|
||||
# n = p*q
|
||||
# c = print(pow(m, e, n))
|
||||
def egcd(a, b):
|
||||
if (a == 0):
|
||||
return (b, 0, 1)
|
||||
else:
|
||||
g, y, x = egcd(b%a, a)
|
||||
return (g, x - (b//a) * y, y)
|
||||
|
||||
e = 65537
|
||||
n = 60509355275518728792864353034381323203712352065221533863094540755630035742080855136016830887120470658395455751858380183285852786807229077435165810022519265154399424311072791755790585544921699474779996198610853766677088209156457859301755313246598035577293799853256065979074343370064111263698164125580000165237
|
||||
c = 44695558076372490838321125335259117268430036823123326565653896322404966549742986308988778274388721345811255801305658387179978736924822440382730114598169989281210266972874387657989210875921956705640740514819089546339431934001119998309992280196600672180116219966257003764871670107271245284636072817194316693323
|
||||
|
||||
|
||||
|
||||
a = b = isqrt(n)
|
||||
b2 = pow(a,2) - n
|
||||
|
||||
i = 0
|
||||
|
||||
while True:
|
||||
print("Iteration # ="+str(i))
|
||||
if b2 == pow(b,2):
|
||||
print("solution found")
|
||||
break
|
||||
else:
|
||||
a+=1
|
||||
b2 = pow(a,2) - n
|
||||
b = isqrt(b2)
|
||||
print("a = " + str(a))
|
||||
print("b = " + str(b))
|
||||
print("b2 = " + str(b2))
|
||||
print("delta-->"+str(pow(b,2)-b2 % n))
|
||||
i+=1
|
||||
p = a+b
|
||||
q = a-b
|
||||
|
||||
print(f"P={p}")
|
||||
print(f"Q={q}")
|
||||
|
||||
phi = (p-1)*(q-1)
|
||||
res = egcd(e, phi)
|
||||
u = res[1]
|
||||
|
||||
decrypted = pow(c,u,n)
|
||||
|
||||
print(decrypted.to_bytes(decrypted.bit_length()//8+1,byteorder='big').decode())
|
||||
20
crypto-asimmetric/RSA-2/chall.py
Normal file
20
crypto-asimmetric/RSA-2/chall.py
Normal file
@ -0,0 +1,20 @@
|
||||
from Cryptodome.Util.number import bytes_to_long, getPrime, isPrime
|
||||
from secret import flag
|
||||
|
||||
|
||||
def next_prime(p):
|
||||
while True:
|
||||
p = p+1
|
||||
if isPrime(p):
|
||||
return p
|
||||
|
||||
#Close numbers so n = pq = (a^2 - b^2)
|
||||
p = getPrime(512)
|
||||
q = next_prime(p)
|
||||
n = p*q
|
||||
e = 65537
|
||||
print(n)
|
||||
m = bytes_to_long(flag.encode())
|
||||
print(pow(m, e, n))
|
||||
# Output: 6050935527551872879286435303438132320371235206522153386309454075563003574208085513601683088712047065839545575185838018328585278680722907743516581002251926515439942431107279175579058554492169947477999619861085376667708820915645785930175531324659803557729379985325606597907434337006411126369816412558000016523744695558076372490838321125335259117268430036823123326565653896322404966549742986308988778274388721345811255801305658387179978736924822440382730114598169989281210266972874387657989210875921956705640740514819089546339431934001119998309992280196600672180116219966257003764871670107271245284636072817194316693323
|
||||
#
|
||||
Reference in New Issue
Block a user