-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFSR.py
61 lines (43 loc) · 1.43 KB
/
LFSR.py
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
import random
class LFSR():
def __init__(self, degree, taps):
print("Initializing LFSR {} degree".format(degree))
cur = random.randrange(0, degree, 3)
self.register = ([0]*cur) + ([1]*(degree-cur))
random.shuffle(self.register)
self.taps = taps
def setRegister(self, register):
self.register = register
def getRegister(self):
return self.register
def getTaps(self):
return self.taps
def shift(self, input_list):
cuurr_list = [[input_list[i - j] for i in range(len(input_list))] for j in range(len(input_list))]
return cuurr_list[1]
def successor(self, input_list):
tap_list = self.taps
tap_list.reverse()
new_reg = self.shift(input_list)
first_index = new_reg[0]
for tl in tap_list:
first_index = first_index ^ input_list[tl]
new_reg[0] = first_index
return new_reg
def decrypt(self, input_list):
rare = input_list[0]
new_list = input_list[1:]
tap_list = self.taps
tap_list.reverse()
for tl in tap_list:
rare ^= new_list[tl-1]
new_list.append(rare)
return new_list
def lfsr(self):
cur_list = self.register
new_list = cur_list
while True:
print(new_list)
new_list = self.successor(new_list)
if new_list == cur_list:
break