from random import getrandbits, randint from Crypto.Util.number import getPrime from functools import reduce from secret import flag import sys import signal
signal.alarm(90)
N = 512 defegcd(a, b): if0 == b: return1, 0, a x, y, q = egcd(b, a % b) x, y = y, (x - a // b * y) return x, y, q
defchinese_remainder(pairs): mod_list, remainder_list = [p[0] for p in pairs], [p[1] for p in pairs] mod_product = reduce(lambda x, y: x * y, mod_list) mi_list = [mod_product//x for x in mod_list] mi_inverse = [egcd(mi_list[i], mod_list[i])[0] for i inrange(len(mi_list))] x = 0 for i inrange(len(remainder_list)): x += mi_list[i] * mi_inverse[i] * remainder_list[i] x %= mod_product return x
defkeygen(): U = [getrandbits(N)for i inrange(N)] V = [] for i inrange(N): v = U[i] - pow(2, N-i-1) V.append(v)
s1 = sum(U) whileTrue: p = getPrime(s1.bit_length() + 1) if p > s1: break s1 = 0 s2 = 0 for i in V: if i < 0: s2 += i else: s1 += i
A = [] for i, j inzip(U, V): A.append(chinese_remainder([(p, i), (q, j)])) return A, U, V, p, q
defcheck(m, n):#比较高位 mbin = bin(m)[2:] nbin = bin(n)[2:] count = 0 for i, j inzip(mbin, nbin): if i == j: count += 1 return count
defencrypt(msg, pub):#背包加密 s = 0 for i, j inzip(msg, pub): s += i * j return s
A, U, V, p, q = keygen()
n = p * q print("your pubkey:") print(A) print(U[0] + V[0]) print(U[0] * V[0]) Menu = ''' 1.hint 2.get flag''' for i inrange(500): print(Menu) op = int(input(">").strip()) if op == 1: m = int(input(">").strip()) print(check(m, n)) elif op == 2: msg = [randint(0, 1) for i inrange(N)] ct = encrypt(msg, A) print("secret:") print(ct) secret = int(input(">").strip()) ans = int("".join(list(map(str, msg))), 2) if ans == secret: print(flag) else: print("wrong") sys.exit(0) else: sys.exit(0)
defhandle(self): signal.alarm(200) ifnot self.proof_of_work(): return nounce = os.urandom(8) self.dosend("Welcome to the Auth System.") self.dosend('If you are admin, I will give you the flag.\n') adminpass = 'Iamthesuperadmin' adminhash = self.myhash(nounce,adminpass) for i inrange(5): self.dosend('Choice:\n') choice = int(self.dorecv(8)) if choice == 0: self.dosend('I can hash for you') user = self.dorecv(32) iflen(user)!=16: self.request.close() return if user == adminpass: self.request.close() return userhash = self.myhash(nounce,user) self.dosend(userhash+'\n') elif choice == 1: self.dosend('Are you admin?') userhash = self.dorecv(48) if userhash == adminhash: self.dosend(flag+'\n') self.request.close() return else: self.dosend('You are not admin!\n') self.request.close() return else: pass self.request.close()
defencrypt_ecb(self,msg,key): l = len(msg) if l%4 !=0: msg = msg+'\x00'*(4-(l%4)) cipher = '' for i inrange(0,len(msg),4): cipher += self.encrypt(msg[i:i+4],key) return cipher
defencrypt(self,msg,key): subkeys = unpack('>4H',key) left,right = unpack('>2H',msg) right = right^subkeys[3] for i inrange(3): tmp = left^self.f(subkeys[i]^right) left = right right = tmp left = right^left return pack('>2H', left, right)
defhandle(self): signal.alarm(200) ifnot self.proof_of_work(): return key = os.urandom(8) self.dosend('Encrypted flag is:') self.dosend(self.encrypt_ecb(flag,key)+'\n') self.dosend('Here is your chance:') data = self.dorecv(160) self.dosend(self.encrypt_ecb(data,key))