1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import random import string import hashlib import sys from collections import deque import sys
count=0 assert(guess<160)
class generator: def __init__(self, key: list, iv: list, hint: bool, k=0, m=0): self.NFSR = deque() self.LFSR = deque()
for i in range(80): self.NFSR.append(key[i])
for i in range(64): self.LFSR.append(iv[i])
for i in range(64, 80): self.LFSR.append(1)
self.clock()
if hint: s = self.NFSR + self.LFSR for i in range(k, k + m): s[i] ^= 1 self.NFSR = deque(list(s)[:80]) self.LFSR = deque(list(s)[80:])
def clock(self): for i in range(160): zi = self.PRGA() self.NFSR[79] ^= zi self.LFSR[79] ^= zi
def PRGA(self): x0 = self.LFSR[3] x1 = self.LFSR[25] x2 = self.LFSR[46] x3 = self.LFSR[64] x4 = self.NFSR[63]
hx = x1 ^ x4 ^ (x0 & x3) ^ (x2 & x3) ^ (x3 & x4) ^ (x0 & x1 & x2) ^ (x0 & x2 & x3) \ ^ (x0 & x2 & x4) ^ (x1 & x2 & x4) ^ (x2 & x3 & x4)
zi = (self.NFSR[1] ^ self.NFSR[2] ^ self.NFSR[4] ^ self.NFSR[10] ^ self.NFSR[31] ^ self.NFSR[43] ^ self.NFSR[ 56]) ^ hx
fx = self.LFSR[62] ^ self.LFSR[51] ^ self.LFSR[38] ^ self.LFSR[23] ^ self.LFSR[13] ^ self.LFSR[0]
gx = self.LFSR[0] ^ self.NFSR[62] ^ self.NFSR[60] ^ self.NFSR[52] ^ self.NFSR[45] ^ self.NFSR[37] \ ^ self.NFSR[33] ^ self.NFSR[28] ^ self.NFSR[21] ^ self.NFSR[14] ^ self.NFSR[9] ^ self.NFSR[0] \ ^ (self.NFSR[63] & self.NFSR[60]) ^ (self.NFSR[37] & self.NFSR[33]) ^ (self.NFSR[15] & self.NFSR[9]) \ ^ (self.NFSR[60] & self.NFSR[52] & self.NFSR[45]) ^ (self.NFSR[33] & self.NFSR[28] & self.NFSR[21]) \ ^ (self.NFSR[63] & self.NFSR[45] & self.NFSR[28] & self.NFSR[9]) ^ ( self.NFSR[60] & self.NFSR[52] & self.NFSR[37] & self.NFSR[33]) \ ^ (self.NFSR[63] & self.NFSR[60] & self.NFSR[21] & self.NFSR[15]) ^ ( self.NFSR[63] & self.NFSR[60] & self.NFSR[52] & self.NFSR[45] & self.NFSR[37]) \ ^ (self.NFSR[33] & self.NFSR[28] & self.NFSR[21] & self.NFSR[15] & self.NFSR[9]) ^ ( self.NFSR[52] & self.NFSR[45] & self.NFSR[37] & self.NFSR[33] & self.NFSR[28] & self.NFSR[21])
self.LFSR.popleft() self.LFSR.append(fx) self.NFSR.popleft() self.NFSR.append(gx)
return zi
for round in range(len(glist)): guess = glist[round] k = guess // 2 m = guess % 10 if m == 0: m = 10 key = bin(random.getrandbits(80))[2:].zfill(80) key = list(map(int, key)) iv = bin(random.getrandbits(64))[2:].zfill(64) iv = list(map(int, iv)) a = generator(key, iv, False) k1 = [] for i in range(160): k1.append(a.PRGA()) k1 = int("".join(list(map(str, k1))), 2) b = generator(key, iv, True, k, m) k2 = [] for i in range(160): k2.append(b.PRGA()) k2 = int("".join(list(map(str, k2))), 2) print(f"round {round+1}") print("Here are some tips might help your:") print(k1) print(k2) num = int(input(">")) if num == guess: count += 1 print("you are smart!\n") else: print("wrong!\n") print(guess)
if count == 32: print(flag) sys.exit(0) else: print("you lose the game, bye!") sys.exit(0)
|