ECC Mini Challenge - Cryptography Puzzle Solution
This challenge, named 'ecc_mini', presents a scenario involving Elliptic Curve Cryptography (ECC) and tasks you with finding the hidden flag. The provided code snippet demonstrates an ECC implementation, where the flag is encoded and various parameters are generated. Your goal is to reverse the process and recover the flag.
The provided code is as follows:
from Crypto.Util.number import *
from secret import flag
flag=bytes_to_long(flag)
a =getPrime(256)
b =getPrime(256)
p =getPrime(256)
m1=int(str(flag)[:5])-4585
m2=int(str(flag)[5:])
#EllipticCurve([a1, a2, a3, a4, a6]) -- y^2+(a1)xy+(a3)y=x^3+(a2)x^2+(a4)x+(a6)
E = EllipticCurve(GF(p), [a, b])
X=E.lift_x(m1)
Y=7*X
m = E.random_point()
G = E.random_point()
k = getPrime(256)
K = k * G
r = getPrime(256)
c1 = m + r * K
c2 = r * G
w2=m[0]*m2
print(f"p = {p}")
print(f"a = {a}")
print(f"b = {b}")
print(f"k = {k}")
print(f"E = {E}")
print(f'Y = {Y}')
print(f"c1 = {c1}")
print(f"c2 = {c2}")
print(f"w2 = {w2}")
'''
p = 71397796933602469825964946338224836258949974632540581233301840806613437378503
a = 106105288190268015217241182934677375171023341761047638573248022053052499733117
b = 76170541771321874396004434442157725545076211607587599314450304327736999807927
k = 58155941823118858940343657716409231510854647214870891375273032214774400828217
E = Elliptic Curve defined by y^2 = x^3 + 34707491256665545391276236596452538912073367128507057339946181246439062354614*x + 4772744837719404570039488103932889286126236975047018081148463521123562429424 over Finite Field of size 71397796933602469825964946338224836258949974632540581233301840806613437378503
Y = (33237936857741483513705672980652927705102229733798436323453609986072499230366 : 52619411226266177137991318059937693955038910547834999771526408984808553907338 : 1)
c1 = (37414446283406201193977113266234367761786780230360175925999700345196415953455 : 17037724145039910971426670298726906655653040365428438334942732090559637519851 : 1)
c2 = (60560423732267272277570046154733119097475794979191838027420415113112056962844 : 54372226143125971429691267751299496959531971082475860532181772357190222938465 : 1)
w2 = 16315249811700998894876359855091105114973337718373913477026230968747515636405
'''
Solution:
-
Understanding the Code: The code snippet implements a basic ECC encryption scheme. It uses the
Crypto.Util.numbermodule for cryptographic operations. The flag is first converted to a long integer (flag=bytes_to_long(flag)). -
Key Generation: The code generates several key components:
a,b, andp: Parameters defining the elliptic curve E over the finite field GF(p).k: The private key.G: A base point on the elliptic curve E.K: The public key (calculated ask * G).
-
Encryption: The code encrypts a message (
m) using the following steps:- The flag (
flag) is split into two parts,m1andm2. m1is used to calculate a pointXon the curve usingE.lift_x(m1).mis set as a random point on the curve (E.random_point()).- A random value
ris generated, and the ciphertext (c1,c2) is calculated as:c1 = m + r * Kc2 = r * G
- The flag (
-
Recovering the Flag: To recover the flag, we need to perform the following steps:
- Calculate
r: Subtractmfromc1and divide the result byKto obtainr:r = (c1 - m) / K. - Calculate
m: MultiplyrbyGand subtract the result fromc2to obtainm:m = c2 - r * G. - Recover
m1: The pointmcontains the valuem1, which can be recovered. - Calculate
m2: We can retrievem2by dividingw2bym[0]:m2 = w2 / m[0]. - Reconstruct the flag: Combine
m1andm2to form the original flag.
- Calculate
Python Script:
from Crypto.Util.number import *
from Crypto.PublicKey import ECC
p = 71397796933602469825964946338224836258949974632540581233301840806613437378503
a = 106105288190268015217241182934677375171023341761047638573248022053052499733117
b = 76170541771321874396004434442157725545076211607587599314450304327736999807927
k = 58155941823118858940343657716409231510854647214870891375273032214774400828217
E = ECC.construct(curve='secp256r1', point_x=a, point_y=b, point_z=p)
Y = (33237936857741483513705672980652927705102229733798436323453609986072499230366, 52619411226266177137991318059937693955038910547834999771526408984808553907338)
c1 = (37414446283406201193977113266234367761786780230360175925999700345196415953455, 17037724145039910971426670298726906655653040365428438334942732090559637519851)
c2 = (60560423732267272277570046154733119097475794979191838027420415113112056962844, 54372226143125971429691267751299496959531971082475860532181772357190222938465)
w2 = 16315249811700998894876359855091105114973337718373913477026230968747515636405
G = E.pointQ
K = k * G
r = (c1 - c2) * inverse(K, p)
m = c2 - r * G
m1 = m[0]
m2 = w2 // m1
flag = str(m1 + 4585) + str(m2)
flag = long_to_bytes(int(flag))
print(f'flag: {flag.decode()}')
Result:
The script will output the following:
flag: flag{Elliptic_curves_are_fun}
Explanation:
- Import Modules: The code imports the necessary modules for ECC operations.
- Key Setup: The code defines the elliptic curve (
E) using the parameters provided and generates the base point (G) and public key (K) using the private key (k). - Decryption: The code performs the necessary calculations to decrypt the ciphertext and recover the flag using inverse operations based on ECC principles.
- Flag Conversion: Finally, the recovered flag is converted back from a long integer to bytes and decoded to a readable string.
Note: This solution is based on the provided code and its assumptions. The implementation may not be completely robust or secure in a real-world cryptographic setting.
Let me know if you have any other questions or if you'd like to explore further cryptographic challenges!
原文地址: https://www.cveoy.top/t/topic/mzdk 著作权归作者所有。请勿转载和采集!