This repository was archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.py
More file actions
99 lines (79 loc) · 2.37 KB
/
css.py
File metadata and controls
99 lines (79 loc) · 2.37 KB
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
import itertools
import random
def LFSR(s, v, l):
if max(v) > len(s):
raise Exception("tap position greater than length of seed")
if min(v) < 0:
raise Exception("tap position must be positive")
result = []
for i in range (0, l):
result.append(s[-1])
b = s[ -1 - v[0] ]
for j in v[1:]:
b ^= s[-1 - j]
s = [ b ] + s[:-1]
return (result, s)
def listToInt(_list):
return int("".join(map(lambda x: str(x), _list)) , 2)
def intToList(_int, pad = 0):
_list = list(map(lambda x: int(x), "{0:b}".format(_int)))
if (pad == 0 or len(_list) >= pad):
return _list
_list = [0] * (pad - len(_list) ) +_list
return _list
def carry(int1, int2):
if (int1 + int2) > 255:
return 1
return 0
def CSS(s, l):
v17 = [14, 0]
v25 = [12, 4, 3, 0]
if len(s) != 40:
raise Exception("Seed must be of length 40.")
s1 = s[:16]
s2 = s[16:]
s2.insert(0, 1)
s1.insert(0, 1)
c = 0
z = []
for i in range(0, l):
xi, s1 = LFSR(s1, v17, 8)
yi, s2 = LFSR(s2, v25, 8)
xi = listToInt(xi)
yi = listToInt(yi)
z += intToList( (xi + yi + c) % 256, pad = 8)
c = carry(xi, yi)
return z
def nBitGenerator(n):
result = []
for b in itertools.product("01", repeat = n):
result.append(list(map(lambda x: int(x), ''.join(b))))
return result
def createS():
return [random.randint(0,1) for i in range(40)]
s_test = [0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0]
def deCSS():
v17 = [14, 0]
v25 = [12, 4, 3, 0]
z = CSS(s_test, 100)
z_atk = z[16:24] + z[8:16] + z[:8] # 2**16 z3 + 2**8 z2 + z1
for s1 in nBitGenerator(16):
s2 = []
x = LFSR([1] + s1, v17, 8*3)[0]
x = x[16:24] + x[8:16] + x[:8] # 2**16 x3 + 2**8 x2 + x1
y = listToInt(z_atk) - listToInt(x)
y = y % (2**24)
y = intToList(y, pad = 24) # y = 2**16 y3 + 2**8 y2 + y1
y1 = y[16:24];
y2 = y[8:16];
y3 = y[:8];
s2 = y1 + y2 + y3
s2.reverse()
s = s1 + s2
print ("Trying seed: " + "".join(map(lambda x: str(x), s)) )
zp = CSS(s, 100)
if z == zp:
return "Found seed: {}.".format(s)
return "Error, seed was not found."
print(deCSS())