from PIL import Image import numpy as np from Crypto.Util.number import * import random from gmpy2 import lcm from secret import flag defgetMyPrime(nbits): defgenProduct(nbits): p = 1 while p.bit_length() < nbits: p *= random.choice(sieve_base) return p P = genProduct(nbits) whilenot isPrime(P-1): P = genProduct(nbits) return (P-1) classHomo: def__init__(self): p, q = getMyPrime(512), getMyPrime(512) n = p*q g = random.randint(1, n*n) while GCD(self.L(pow(g, lcm(p-1, q-1), n*n), n), n) != 1: g = random.randint(1, n*n) self.g, self.n = g, n print("n =", n) print("g =", g) defenc(self, m): n = self.n return (pow(self.g, int(m), n*n)*pow(random.randint(1, n), n, n*n)) % (n*n) defL(self, u, n): return (u-1)//n defencrypt_img(homo, img_array): cip_list = [] for i inrange(len(img_array)): for j inrange(len(img_array[i])): cip_list.append(homo.enc(img_array[i][j])) return cip_list defencrypt_flag(homo, flag): flag_bin = bin(flag)[2:] cip_list = [homo.enc(int(i) << 8) for i in flag_bin] return cip_list if __name__ == "__main__": homo = Homo() img = Image.open("./img.png").convert("L") # 56x56 img_array = np.array(img) img_enc = encrypt_img(homo, img_array) flag = bytes_to_long(flag.encode()) flag_enc = encrypt_flag(homo, flag)
assertlen(img_enc) > len(flag_enc) withopen("data", "w") as f: for i inrange(len(img_enc)): if i < len(flag_enc): enc = (flag_enc[i]*img_enc[i]) % (homo.n**2) f.write(hex(enc)[2:]+"\n") else: f.write(hex(img_enc[i])[2:]+"\n") # n = 18434491463536053807355381425234564739214857081161321309756933006496704225386021314592003940717664842835425875706674652882353813796736161102105318696602774157554627232038142092845118667433422811370679163251506433284182749390704212211677891535882993540750379419384629735499044085104591893176847835387567644762636961 # g = 99894228586367782940715460732971967417359410558715186789679488951080212107512884192976002563404881263875114900183845944243751294600634946131559701908524899495387780188074842981190381617301097312646907480816373003121403029154865843313001145153263200356271270964096284006748227606839491672635131818273934109984977288621523498782962389115299664149676881349445940131040928322172748228670542470966453917916224551852329336572423059849239115479150176538160893340622774682474615303826972971312087884483400100816655408278649954266707268236152380355955111697822333005733513834283677509165970313043388621472231706074701389916165894223877
from Crypto.Util.number import * from given import n,p,q,g defL(x): return (x-1)//(n) #-----MAIN BELOW-----# f=open("data.txt","r") b=f.read() f.close() C,M=[],[] s="" for i inrange(len(b)): if b[i]=='\n': C.append(int(s,16)) s="" else: s=s+b[i] lm=(p-1)*(q-1)//GCD(p-1,q-1) for i inrange(len(C)): if(i%100==0): print(i) m=L(pow(C[i],lm,n*n))*inverse(L(pow(g,lm,n*n)),n)%n M.append(m) print(M)
from Crypto.Util.number import * from gmpy2 import * from E import e import libnum withopen('flag.enc','rb') as f: cipher = f.read() c = int.from_bytes(cipher, byteorder='little') print(iroot(c,e)[0]) for i inrange(4): input()
3.[INSHack2019]Yet Another RSA Challenge - Part 1
1 2 3 4 5 6 7 8
import subprocess p = subprocess.check_output('openssl prime -generate -bits 2048 -hex') q = subprocess.check_output('openssl prime -generate -bits 2048 -hex') flag = int('INSA{REDACTED}'.encode('hex'), 16) N = int(p,16) * int(q,16) print N print'0x'+p.replace('9F','FC') printpow(flag,65537,N)
import random flag = "flag{" + ''.join(str(random.getrandbits(32)) for _ inrange(4)) + "}" withopen('output.txt', 'w') as f: for i inrange(1000): f.write(str(random.getrandbits(32)) + "\n") print(flag)
from random import Random # right shift inverse definverse_right(res,shift,mask=0xffffffff,bits=32): tmp = res for i inrange(bits//shift): tmp = res ^ tmp>>shift & mask return tmp # left shift inverse definverse_left(res,shift,mask=0xffffffff,bits=32): tmp = res for i inrange(bits//shift): tmp = res ^ tmp << shift & mask return tmp defbacktrace(cur): high = 0x80000000 low = 0x7fffffff mask = 0x9908b0df state = cur for i inrange(3,-1,-1): tmp = state[i+624]^state[i+397] # recover Y,tmp = Y if tmp & high == high: tmp ^= mask tmp <<= 1 tmp |= 1 else: tmp <<=1 # recover highest bit res = tmp&high # recover other 31 bits,when i =0,it just use the method again it so beautiful!!!! tmp = state[i-1+624]^state[i+396] # recover Y,tmp = Y if tmp & high == high: tmp ^= mask tmp <<= 1 tmp |= 1 else: tmp <<=1 res |= (tmp)&low state[i] = res return state defrecover_state(out): state = [] for i in out: i = inverse_right(i,18) i = inverse_left(i,15,0xefc60000) i = inverse_left(i,7,0x9d2c5680) i = inverse_right(i,11) state.append(i) return state f = open("output.txt","r").readlines() c = [] for i inrange(1000): c.append(int(f[i].strip())) partS = recover_state(c) state = backtrace([0]*4+partS)[:624] # print(state) prng = Random() prng.setstate((3,tuple(state+[0]),None)) flag = "flag{" + ''.join(str(prng.getrandbits(32)) for _ inrange(4)) + "}" print(flag)