def solve(card_pk, door_pk): base = 7 m = 20201227 card_loop_size = discrete_log(m, card_pk, base) door_loop_size = discrete_log(m, door_pk, base) return ( pow(door_pk, card_loop_size, m), pow(card_pk, door_loop_size, m) )
def part1(keys): pubKDoor = int(keys[0]) pubKCard = int(keys[1]) # Polard Rho key = discrete_log(p, pubKDoor, subjectNumber) # Brute Force # key = 0 # while(transformSubjectNumber(subjectNumber, key) != pubKDoor): # key += 1 return transformSubjectNumber(pubKCard, key)
def part_1(card_public_key: int, door_public_key: int) -> int: card_loop_size = discrete_log(20201227, card_public_key, 7) card_encryption_key = pow(door_public_key, card_loop_size, 20201227) return card_encryption_key
def handshake(data, g, p): """SymPy discrete_log on raw data.""" a, b = data x = discrete_log(p, a, g) return pow(b, x, p)
def crack2(pubkey): return discrete_log(n, pubkey, 7)
def get_loop_sz(pub_key): return discrete_log(20201227, pub_key, 7)
print() ''' # BruteForce # A = 2^a mod p # c = b^e mod m for i in range(0,p): # not in the first 1 billion - took a few minutes to calculate x = pow(2,i,p) if(x==A): print("a:",i) elif(x==B): print("b:",i) ''' # 2. Find an integer x in [1,p-1] s.t. g^{x} = A mod p and g^{x} = B mod p. # Note that the complexity of this computation is exponential as it is the discrete logarithm problem. x = discrete_log(p, A, 2) y = discrete_log(p, B, 2) print("x:",x) print("y:",y) if (pow(2,x,p) == A): print("a:",x) a = x if (pow(2,y,p) == B): print("b:",y) b = y print() # 3. Compute the secret common key. (to verify correct keys)
#!/usr/bin/python3 from sympy.ntheory.residue_ntheory import discrete_log M = 491988559103692092263984889813697016406 P = 232042342203461569340683568996607232345 B = 5 A = discrete_log(M, P, B) message = 12259991521844666821961395299843462461536060465691388049371797540470 bobkey = 76405255723702450233149901853450417505 text = bytes.fromhex(hex(pow(bobkey, A, M) ^ message)[2:]).decode('ASCII') print(text)
first = 4500*3256+1829 quo, rest = fill_corpus(first,resp_first) do_number(first) iteration = 1 while not check_corpus(a_quo_sfactors,a_rest_sfactors,b_quo_sfactors,b_rest_sfactors) and iteration < NB_REQUESTS: i = 1 prime = next(primes_N) print("test",prime) while prime in Number(): prime = next(primes_N) while (prime * (i)) < p: try: power = discrete_log(p,prime*i,first) break except Exception as e: i += 1 continue if power: iteration += 1 print("iteration :",iteration,"for",prime,end=" | ") do_number(first,power) print("nb corpus elts :",len(Number.corpus_i),"nb iteration :",iteration) print("P :",p," Challenge :",A,"dans",B,"?") if check_corpus(a_quo_sfactors,a_rest_sfactors,b_quo_sfactors,b_rest_sfactors): A = Number.get_number(a_quo_sfactors,a_rest_sfactors) B = Number.get_number(b_quo_sfactors,b_rest_sfactors)
def part_one(code: Tuple[int, int]) -> None: subject_number = 7 cycle = 20201227 result = pow(code[1], discrete_log(cycle, code[0], subject_number), cycle) print(f"Result for part one is {result}")