#NIST P-384 #p = 2^384 – 2^128 – 2^96 + 2^32 – 1 p = 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319 a = -3 b = 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575
#NIST P-521 p = 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151 a = -3 b = 1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984
import random """ 这里使用NIST P-256 """ """ 已知Alice私钥为62082900092842610053287908404599315101005270141167316143733368271632829538584 公钥为(107641879144396245698735275506803265840358797864132319680190373847271858164980,47355535520015758232672121387250557693006806715731251672455727730722279072073) """ p = 115792089210356248762697446949407573530086143415290314195533631308867097853951 a = 115792089210356248762697446949407573530086143415290314195533631308867097853948 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 E = EllipticCurve(GF(p),[a,b]) M = E(80764032034929976879602863302323059647882062252124869895215418422992624743795,4964654783828069942602279691168356721024126126864424301508238062949726916347)
G = E.gen(0) # 获取生成元 n = E.order() Pa = E(107641879144396245698735275506803265840358797864132319680190373847271858164980,47355535520015758232672121387250557693006806715731251672455727730722279072073)
k = random.randint(1,p-1) C1 = k*G K = k*Pa C2 = M + K
p = 115792089210356248762697446949407573530086143415290314195533631308867097853951 a = 115792089210356248762697446949407573530086143415290314195533631308867097853948 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 E = EllipticCurve(GF(p),[a,b])
C1 = E(57728498705822675870223033275964016497503135785449483276975549209363755426632,100207880026633678257826613394394896220264123242435071203173012352108326553272) C2 = E(37815699639437978342774585294689161021328418818072948538182050516188487137302,98438576994494511703874400677395107315311376509871600325640423991402748907811) na = 62082900092842610053287908404599315101005270141167316143733368271632829538584
from secret import flag from Crypto.Util.number import * assert(flag[:5]=='flag{') flag = flag[5:-1] num1 = bytes_to_long(flag[:7]) num2 = bytes_to_long(flag[7:14]) num3 = bytes_to_long(flag[14:])
defECC1(num): p = 146808027458411567 A = 46056180 B = 2316783294673 E = EllipticCurve(GF(p),[A,B]) P = E.random_point() Q = num*P print E print'P:',P print'Q:',Q
defECC2(num): p = 1256438680873352167711863680253958927079458741172412327087203 #import random #A = random.randrange(389718923781273978681723687163812) #B = random.randrange(816378675675716537126387613131232121431231) A = 377999945830334462584412960368612 B = 604811648267717218711247799143415167229480 E = EllipticCurve(GF(p),[A,B]) P = E.random_point() Q = num*P print E print'P:',P print'Q:',Q factors, exponents = zip(*factor(E.order())) primes = [factors[i] ^ exponents[i] for i inrange(len(factors))][:-1] print primes dlogs = [] for fac in primes: t = int(int(P.order()) / int(fac)) dlog = discrete_log(t*Q,t*P,operation="+") dlogs += [dlog] print("factor: "+str(fac)+", Discrete Log: "+str(dlog)) #calculates discrete logarithm for each prime order print num print crt(dlogs,primes)
defECC3(num): p = 0xd3ceec4c84af8fa5f3e9af91e00cabacaaaecec3da619400e29a25abececfdc9bd678e2708a58acb1bd15370acc39c596807dab6229dca11fd3a217510258d1b A = 0x95fc77eb3119991a0022168c83eee7178e6c3eeaf75e0fdf1853b8ef4cb97a9058c271ee193b8b27938a07052f918c35eccb027b0b168b4e2566b247b91dc07 B = 0x926b0e42376d112ca971569a8d3b3eda12172dfb4929aea13da7f10fb81f3b96bf1e28b4a396a1fcf38d80b463582e45d06a548e0dc0d567fc668bd119c346b2 E = EllipticCurve(GF(p),[A,B]) P = E.random_point() Q = num*P print E print'P:',P print'Q:',Q
defsolve(P,Q,E): factors, exponents = zip(*factor(E.order())) primes = [factors[i] ^ exponents[i] for i inrange(len(factors))][:-1] print(primes) dlogs = [] for fac in primes: t = int(int(P.order()) // int(fac)) dlog = discrete_log(t*Q,t*P,operation="+") dlogs += [dlog] print("factor: "+str(fac)+", Discrete Log: "+str(dlog)) #calculates discrete logarithm for each prime order m = CRT_list(dlogs,primes) return m
from Crypto.Util.number import bytes_to_long from secret import flag
p = 64464091191308356774703439660771627086045800299627641179047457478059588557461 a = 31926335967105564755113987930261069322507794703287741857397622356704769886356 b = 34835808070187351680507689900273321615070127680320357724483770400791707112940 Gx = 2053202552422630348010474635096983783565667661786369125783579647572276572403 Gy = 51320753844844801021362329076409450910659564359017581255542897537756778371539
from Crypto.Util.number import * from tqdm import *
p = 64464091191308356774703439660771627086045800299627641179047457478059588557461 a = 31926335967105564755113987930261069322507794703287741857397622356704769886356 b = 34835808070187351680507689900273321615070127680320357724483770400791707112940 Gx = 2053202552422630348010474635096983783565667661786369125783579647572276572403 Gy = 51320753844844801021362329076409450910659564359017581255542897537756778371539
K = EC(31981799071949968743482831587417174146463993877255771340814476669214408840460,15144025062588325012239455117890516531350002058200271280110877844265896081387)
defsolve(P,Q,E): factors, exponents = zip(*factor(E.order())) primes = [factors[i] ^ exponents[i] for i inrange(len(factors))][:-1] print(primes) dlogs = [] for fac in primes: t = int(int(P.order()) // int(fac)) dlog = discrete_log(t*Q,t*P,operation="+") dlogs += [dlog] print("factor: "+str(fac)+", Discrete Log: "+str(dlog)) #calculates discrete logarithm for each prime order m = CRT_list(dlogs,primes) n = prod(primes) return m,n
#sage from sage.allimport * from sage.all_cmdline import * from Crypto.Util.number import * from secret import flag
Nbits = 512 x = bytes_to_long(flag) f = open('./output', 'w')
defgen_pubkey(Nbits): p = getPrime(Nbits // 2) q = getPrime(Nbits // 2) n = p*q whileTrue: a = getRandomInteger(Nbits // 2) b = getRandomInteger(Nbits // 2) if gcd(4*a**3 + 27*b**2, n) == 1: break E = EllipticCurve(Zmod(n), [a, b]) e = getPrime(64) f.write(f"p={p}\nq={q}\n") return n, E, e
n, E, e = gen_pubkey(Nbits) pt = E.lift_x(Integer(x)) ct = pt * e f.write(f"n = {n}\na = {E.a4()}\nb = {E.a6()}\ne = {e}\n") f.write(f"ciphertext = {long_to_bytes(int(ct.xy()[0]))}\n")
已知$Ct = Pt×e$
给出$Ct$的横坐标$x$
想利用E.lift_x(Integer(x))来求$Ct$的纵坐标$y$,发现算不出来
改用
1 2 3 4
#sage R.<y> = Zmod(n)[] f = x^3 + a*x + b - y^2 print(f.roots())
p = 115192265954802311941399019598810724669437369433680905425676691661793518967453 q = 109900879774346908739236130854229171067533592200824652124389936543716603840487 n = 12659731371633323406361071735480743870942884407511647144758055911931321534333057725377899993936046070028289182446615763391740446071787318153462098556669611 a = 34573016245861396068378040882622992245754693028152290874131112955018884485688 b = 103282137133820948206682036569671566996381438254897510344289164039717355513886 e = 11415307674045871669 ciphertext = b'f\xb1\xae\x08`\xe8\xeb\x14\x8a\x87\xd6\x18\x82\xaf1q\xe4\x84\xf0\x87\xde\xedF\x99\xe0\xf7\xdcH\x9ai\x04[\x8b\xbbHR\xd6\xa0\xa2B\x0e\xd4\xdbr\xcc\xad\x1e\xa6\xba\xad\xe9L\xde\x94\xa4\xffKP\xcc\x00\x907\xf3\xea'