from AITMCLab.libnum import n2s, s2n flag = 'flag{Congratulation!_quiz1_passed!!!}' def nextPrime(n): n += 2 if n & 1 else 1 while not isPrime(n): n += 2 return n e = 11248112333656902878308992204660514716130692202019193081806766887380465145401754698746718075268681481388695805324253817155823465013590321091178897918430457 c1 = 5120829596353532760839054347975234579355835073413768618360492980516438193909447500996222328143719619379838946544412967584025416378147246422705451415437468 c2 = 17985907282297772406857113433926323639543183645704827789984971602150950301590677893419082 c1, c2 = n2s(c1), n2s(c2) E = nextPrime(e) f = 1 for i in range(E - 2, e, -1): f = f * inverse(i, E) % E print f f = n2s(f) def xor(s1, s2): length = min(len(s1), len(s2)) res = '' for i in range(length): res = res + chr(ord(s1[i]) ^ ord(s2[i])) return res
now_x = 0 for i in range(r): now_g = pow(g, pow(p, r - 1 - i), n) now_y = pow(y, pow(p, r - 1 - i), n) now = pow(now_g, now_x, n) for now_a in range(p + 1): if now == now_y: a[i] = now_a break now = now * pow(now_g, pow(p, i), n) % n now_x += a[i] * pow(p, i) xs.append(now_x) print xs for i in range(len(ps)): ps[i] = pow(ps[i], rs[i]) Ms = [] m = 1 for p in ps: m *= p for i in range(len(ps)): now_m = m // ps[i] now_m = now_m * invmod(now_m, ps[i]) % n Ms.append(now_m) res = 0 for i in range(len(ps)): res = (res + xs[i] * Ms[i]) % m print(n2s(res))
# Welcome to lab3, I am going to introduct some interesting functions to you! # We need to import what we want from AITMCLab.libnum import n2s, s2n, invmod from AITMCLab.Crypto.Util.number import long_to_bytes, bytes_to_long from AITMCLab.Crypto.Util.number import getPrime, isPrime # Use s2n(acronym for 'string to number') to convert string to number # and use n2s to revert s = 'I love algorithm design' n = s2n(s) # int print 'n =',n new_s = n2s(n) assert new_s == s # Feel free to replace n2s with long_to_bytes, or replace s2n with bytes_to_long # They are equavalant!! assert long_to_bytes(bytes_to_long('hahaha')) == 'hahaha' assert n2s(bytes_to_long('heiheihei')) == 'heiheihei' assert long_to_bytes(s2n('xixixi')) == 'xixixi' # x * invmod(x, n) % n == 1, as you suppose n = 23 for x in range(1, n): assert x * invmod(x, n) % n == 1 assert invmod(x, n) * x % n == 1 # Use function 'getPrime' to generate a prime RANDOMLY # namely, you will get different result every time you execute this program p = getPrime(5) q = getPrime(5) print p, q
from AITMCLab.libnum import invmod, n2s n = 3914224287795636809861368015178301501372188634077669936495180785846977608060045377653931855350462641602739528638098085015546367841148708574573569280349292403464473463818042817457266362325488737681832840764568002910598017717058689969512296099 c = 3678716732847973069496233717035619410810008982881832778108057662379358563168669345659610269104838531535596938088040391434969949131327247362326079375825722973572861874365364217648459622237827463731017181656549460372354778250000173854890274241 e = 0x10001 d = invmod(e, n - 1) print n2s(pow(c, d, n)) # flag{this_is_a_flag,hahaha}
s += i print s # 20 # You can program easier with built-in function, such as 'ord' and 'chr' # ord: str(length=1) -> int Transfer character into ascii number # chr: int -> str(length=1) Transfer ascii number into character # Want to know more? # https://www.runoob.com/python/python-func-ord.html # https://www.runoob.com/python/python-func-chr.html print ord('a') # 97 print chr(97) # 'a' # Use keyword 'import' to import other function from ctflab from AITMCLab.libnum import n2s, s2n print s2n('Happy Hacking') # 5734583677810927947475323547239 print n2s(5734583677810927947475323547239) # 'Happy Hacking' # Want to know more about AITMCLab.libnum? https://github.com/hellman/libnum # Explore more grammars, use baidu # Lab1 # Here is an encoding procedure, try to understand # what happened and findout the message message = 'flag{*************}' # You can't see the message here!! hahaha cipher = '' key = 3 for char in message: if 'a' <= char <= 'z': now_cipher = (ord(char) - ord('a') + key) % 26 cipher += chr(now_cipher + ord('a')) else:
message = padding << m**(p-1) % p + m % 2 cipher = pow(message, e, N) f.write(hex(cipher)+'\n') m /= 2""" p = 23 m = 5 #(2 * pad * pad + 0) ** e + kn = enc # #(2 * pad * pad + 0) + (kn) % e = enc % e #(2 * pad * pad + 1) ** e + kn = enc i = 0 m = 0 with open("./key.enc", "r") as f: info = f.readlines() f.close() for i in range(len(info)): x = int(info[i].strip()[:-1], 16) #print libnum.jacobi(x,N) #print x,hex(x) #raw_input() if libnum.jacobi(x, N) == 1: m += (1 << i) else: m += 0 n = N temp = random.randint(0, 2**1000)**2 print libnum.jacobi(pow(2 * temp, e, n), n) print libnum.jacobi(pow(4 * temp, e, n), n) print libnum.n2s(m)
M.append(nowm) r = 0 for i in range(10): r += M[i] * res[i] r = r % N # print r return r def solve_sig(): ls = [] with open('sig', 'rb') as f: line = f.readline().split('=')[-1] y = int(line) for i in range(95): line = f.readline().split('=')[-1].strip().split(',') ls.append((int(line[0]), int(line[1]))) res = [] for i in range(95): r, s = ls[i] res.append(1 if elgamal_sig_verify(1024, r, s, p, g, y) else 0) r = int(''.join(map(str, res[::-1])), 2) # print r return r f1 = solve_enc() f2 = solve_sig() from AITMCLab.libnum import n2s f = n2s(f1) + n2s(f2) print 'flag{' + f + '}'