flag = b"flag{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}"
p = getStrongPrime(2048) for i in range(1, (p+1)//2): s += pow(i, p-2, p) s = s % p q = gmpy2.next_prime(s) n = p*q e = 0x10001 c = pow(bytes_to_long(flag), e, n) print(p) print(c)
#!/usr/bin/env python2.7 from libnum import n2s, s2n, xgcd from random import getrandbits from Crypto.Util.number import getPrime from Crypto.Cipher import AES from hashlib import sha256 import SocketServer #from secret import flag flag=b"vanish"
pad = lambda x: x + '$' * (16 - len(x) % 16) if len(x) % 16else x nbits = 128 MASK = 2 ** nbits - 1
defgenerate_key(nbits): e1 = 65537 e2 = 92431 p = getPrime(nbits // 2) q = getPrime(nbits // 2) while abs(p - q) < p >> 2: p = getPrime(nbits // 2) q = getPrime(nbits // 2) n = p * q phi = (p - 1) * (q - 1) d1 = xgcd(e1, phi)[0] % phi d2 = xgcd(e2, phi)[0] % phi return n, d1, d2
classEncHandler(SocketServer.BaseRequestHandler): defhandle(self): primate_key = generate_k() secret_key = generate_key(2048) n = secret_key[0] public_key = n, 65537, 92431
self.request.sendall("Welcome to our OT system\n") self.request.sendall("Now you can choose what you wanna do\n") self.request.sendall("1. get a message from the two\n2. encrypt flag with your own data\n3. encrypt in another way\n")
self.request.sendall("Your pubkey is: " + hex(n) + "\n") for _ in range(580): self.request.sendall('Input your choice\n') self.request.sendall("choice>") choice = self.request.recv(16).strip() if choice == '1': m = [] for _ in range(8): cur = getrandbits(32) m.append(cur) m0 = (m[0] << 96) ^ (m[1] << 64) ^ (m[2] << 32) ^ m[3] m1 = (m[4] << 96) ^ (m[5] << 64) ^ (m[6] << 32) ^ m[7] self.request.sendall('If you wanna get mi, encrypt your key with ei\n') enc_key = int(self.request.recv(1024).strip(), 16) print(enc_key) t0, t1 = decrypt(enc_key, secret_key) ms = t0 ^ m0, t1 ^ m1 self.request.sendall('Your message is ' + str(ms) + '\n') self.request.sendall("Don't worry, I don't know which message you have got!\n") elif choice == '2': self.request.sendall('Input your data.\n') data = int(self.request.recv(1024).strip(), 16) t = verify(data, public_key) cur_rand = getrandbits(128) cur_k = t[cur_rand & 1] ^ cur_rand key = sha256(n2s(add(primate_key, cur_k))).digest()[:16] aes = AES.new(key, AES.MODE_ECB) cipher = aes.encrypt(pad(flag)).encode('hex') self.request.sendall("Your cipher is: " + cipher + '\n') elif choice == '3': self.request.sendall('Input your data.\n') data = int(self.request.recv(1024).strip(), 16) t = verify(data, public_key) cur_rand = getrandbits(128) cur_k = t[cur_rand & 1] ^ cur_rand key = sha256(n2s(xor(primate_key, cur_k))).digest()[:16] aes = AES.new(key, AES.MODE_ECB) cipher = aes.encrypt(pad(flag)).encode('hex') self.request.sendall("Your cipher is: " + cipher + '\n')
if __name__ == "__main__": HOST, PORT = "0.0.0.0", 9999 server = ThreadedTCPServer((HOST, PORT), EncHandler) server.serve_forever()
import hashlib import socketserver import os,random,string from hashlib import sha256 from Crypto.Util.number import *
#from SECRET import FLAG FLAG = b"vanish" p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f a = 0 b = 7 xG = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 yG = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 h = 1 zero = (0,0) G = (xG, yG) kbits = 8
defadd(p1, p2): if p1 == zero: return p2 if p2 == zero: return p1 (p1x,p1y),(p2x,p2y) = p1,p2 if p1x == p2x and (p1y != p2y or p1y == 0): return zero if p1x == p2x: tmp = (3 * p1x * p1x + a) * inverse(2 * p1y , p) % p else: tmp = (p2y - p1y) * inverse(p2x - p1x , p) % p x = (tmp * tmp - p1x - p2x) % p y = (tmp * (p1x - x) - p1y) % p return (int(x),int(y))
defmul(n, p): r = zero tmp = p while0 < n: if n & 1 == 1: r = add(r,tmp) n, tmp = n >> 1, add(tmp,tmp) return r
defnext(self): nextdata = (self.init << 1) & self.lengthmask i = self.init & self.mask & self.lengthmask output = 0 while i != 0: output ^= (i & 1) i = i >> 1 nextdata ^= output self.init = nextdata return output
defmy_filter(c1,c2): if random.random()>0.1: return str(c1) else: return str(c2)
N=64 mask1=random.getrandbits(N) mask2=random.getrandbits(N) print(mask1) print(mask2) l1=lfsr(init1,mask1,N) l2=lfsr(init2,mask2,N) output='' for i in range(2048): output+=my_filter(l1.next(),l2.next()) print(output)
defnext(self): nextdata = (self.init << 1) & self.lengthmask i = self.init & self.mask & self.lengthmask output = 0 while i != 0: output ^^= (i & 1) i = i >> 1 nextdata ^^= output self.init = nextdata return output
defdiff(a,b): flag = 0 for (i,j) in zip(a,b): if i == j: flag+=1 return float(flag / len(a))
# construct transfer matrix1 a=[] for i in range(63): b=[] for j in range(64): if j==i+1: b.append(1) else: b.append(0) a.append(b) a.append(mask1.bits()[::-1]) M = Matrix(GF(2),a)
# find the continuous 64bits of l1 and recover the init1 for i in tqdm(range(len(output)-64)): try: block = output[i:i+64] init1 = M^(i+64) \ vector(GF(2),[int(each) for each in list(output[i:i+64])]) init1 = int(''.join(str(i) for i in init1),2) tryl1 = lfsr(init1,mask1,N) tryoutput = ''.join(str(tryl1.next()) for _ in range(2048)) match = diff(tryoutput,output) if match > 0.9: print("yes",init1,match) break except Exception as e: print(str(e))
我们的用状态矩阵M 乘以 初始向量init($M \cdot init$),那么就是一次状态转移,会获得一个输出(状态)。新的state向量(状态向量)再左乘一下状态矩阵($M \cdot state = M \cdot M \cdot init$),则又是一次状态转移,再获得一个新的输出(状态)那么这里如果我们先将两个矩阵相乘,我们如下形式的矩阵
init1 = 15401137114601469828 l1 = lfsr(init1,mask1,N) lioutput = ''.join(str(l1.next()) for _ in range(2048)) diff_idx = [] # choose bits of l2 for idx in range(2048): if lioutput[idx] != output[idx]: diff_idx.append(idx)
mask2= # construct transfer matrix2 a=[] for i in range(63): b=[] for j in range(64): if j==i+1: b.append(1) else: b.append(0) a.append(b) a.append(mask2.bits()[::-1]) M2 = Matrix(GF(2),a) a2=[] b2=[]
# construct matrix to solve Equations for idx in diff_idx: a2.append((M2^(idx+1))[-1]) b2.append(output[idx]) M3 = Matrix(GF(2),a2) b2 = vector(GF(2),b2)
init2 = M3 \ b2 init2 = int(''.join(str(i) for i in init2),2)