from Crypto.Util.number import * from key import FLAG
defkeygen(size): q = getPrime(80) k = getPrime(944) whileTrue: p = q * k + 1 if isPrime(p): break k += 1 g = 2 whileTrue: ifpow(g, q, p) == 1: break g += 1 A = getRandomInteger(size) % q B = getRandomInteger(size) % q x = getRandomInteger(size) % q h = pow(g, x, p) return (g, h, A, B, p, q), (x)
defrand(A, B, q): global rand_state rand_state, ret = (A * rand_state + B) % q, rand_state return ret
defencrypt(pubkey, m): g, h, A, B, p, q = pubkey assert0 < m <= p r = rand(A, B, q) c1 = pow(g, r, p) c2 = (m * pow(h, r, p)) % p return (c1, c2)
assert ( A * s0 + B ) % q == s1 assert ( A * s1 + B ) % q == s2 assert ( A * s2 + B ) % q == s3 assert ( A * s3 + B ) % q == s4
p = 65211247300401312530078141569304950676358489059623557848188896752173856845051471066071652073612337629832155846984721797768267868868902023383604553319793550396610085424563231688918357710337401138108050205457200940158475922063279384491022916790549837379548978141370347556053597178221402425212594060342213485311 g = 27642593390439430783453736408814717946185190497201679721975757020767271070510268596627490205095779429964809833535285315202625851326460572368018875381603399143376574281200028337681552876140857556460885848491160812604549770668188783258592940823128376128198726254875984002214053523752696104568469730021811399216 h = 54585833166051670245656045196940486576634589000609010947618047461787934106392112227019662788387352615714332234871251868259282522817042504587428441746855906297390193418159792477477443129333707197013251839952389651332058368911829464978546505729530760951698134101053626585254469108630886768357270544236516534904
import time import random from Crypto.Util.number import long_to_bytes import gmpy2
deffactor_to_get_q(temp): # 对 temp 进行因式分解 factors = factor(temp) for i in factors: if i[0].nbits() == 80: # 80, 刚好 80 bits return gmpy2.mpz(i[0]) raise Exception("fail to get q")
defAMM(o, r, q): """ pow(x, r, q) = o --> return x """ start = time.time() # print('\n----------------------------------------------------------------------------------') # print('Start to run Adleman-Manders-Miller Root Extraction Method') # print('Try to find one {:#x}th root of {} modulo {}'.format(r, o, q)) g = GF(q) o = g(o) p = g(random.randint(1, q)) while p ^ ((q-1) // r) == 1: p = g(random.randint(1, q)) # print('[+] Find p:{}'.format(p)) t = 0 s = q - 1 while s % r == 0: t += 1 s = s // r # print('[+] Find s:{}, t:{}'.format(s, t)) k = 1 while (k * s + 1) % r != 0: k += 1 alp = (k * s + 1) // r # print('[+] Find alp:{}'.format(alp)) a = p ^ (r**(t-1) * s) b = o ^ (r*alp - 1) c = p ^ s h = 1 for i inrange(1, t): d = b ^ (r^(t-1-i)) if d == 1: j = 0 else: # print('[+] Calculating DLP...') j = - dicreat_log(a, d) # print('[+] Finish DLP...') b = b * (c^r)^j h = h * c^j c = c ^ r result = o^alp * h # end = time.time() # print("Finished in {} seconds.".format(end - start)) # print('Find one solution: {}'.format(result)) return result
defexgcd(a, b): if a == 0: return (b, 0, 1) else: g, x, y = exgcd(b % a, a) return (g, y - (b // a) * x, x)
p = gmpy2.mpz(65211247300401312530078141569304950676358489059623557848188896752173856845051471066071652073612337629832155846984721797768267868868902023383604553319793550396610085424563231688918357710337401138108050205457200940158475922063279384491022916790549837379548978141370347556053597178221402425212594060342213485311) g = gmpy2.mpz(27642593390439430783453736408814717946185190497201679721975757020767271070510268596627490205095779429964809833535285315202625851326460572368018875381603399143376574281200028337681552876140857556460885848491160812604549770668188783258592940823128376128198726254875984002214053523752696104568469730021811399216) h = gmpy2.mpz(54585833166051670245656045196940486576634589000609010947618047461787934106392112227019662788387352615714332234871251868259282522817042504587428441746855906297390193418159792477477443129333707197013251839952389651332058368911829464978546505729530760951698134101053626585254469108630886768357270544236516534904)