forked from ZhangFengze/NeoXResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotor.py
192 lines (154 loc) · 5.23 KB
/
rotor.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import ctypes
import math
import sys
PYTHON3 = sys.version_info >= (3, 0)
def to_int(x, d=0):
if not PYTHON3:
return int(math.trunc(x))
else:
return int(x)
class Rotor(object):
def __init__(self, key):
self.key = [0, 0, 0, 0, 0]
self.seed = [0, 0, 0]
self.size = 256
self.rotors = 6
self.e_rotor = [[0] * self.size] * (self.rotors)
self.d_rotor = [[0] * self.size] * (self.rotors)
self._positions = [0] * (self.rotors)
self._advances = [0] * (self.rotors)
self.set_key(key)
self.set_seed()
self.positions()
self.advances()
self.e_rotors()
self.d_rotors()
for i in range(self.rotors):
self._positions[i] = ctypes.c_uint8(self.r_rand(
ctypes.c_short(self.size).value)).value
self._advances[i] = (
1+(2*(self.r_rand(ctypes.c_short(int(self.size/2.0)).value))))
(e_rotor, d_rotor) = self.RTR_permute_rotor(
self.e_rotor[i][:], self.d_rotor[i][:])
self.e_rotor[i] = e_rotor
self.d_rotor[i] = d_rotor
def RTR_make_id_rotor(self, rtr):
for j in range(self.size):
rtr[j] = ctypes.c_uint8(j).value
return rtr
def RTR_permute_rotor(self, e, d):
i = self.size
self.RTR_make_id_rotor(e)
while 2 <= i:
q = self.r_rand(i)
i -= 1
j = e[q]
e[q] = ctypes.c_uint8(e[i]).value
e[i] = ctypes.c_uint8(j).value
d[j] = ctypes.c_uint8(i).value
e[0] = ctypes.c_uint8(e[0]).value
d[e[0]] = 0
return (e, d)
def d_rotors(self):
for i in range(self.rotors):
for j in range(self.size):
self.d_rotor[i][j] = ctypes.c_uint8(j).value
pass
def e_rotors(self):
for i in range(self.rotors):
self.e_rotor[i] = self.RTR_make_id_rotor(self.e_rotor[i])
def positions(self):
for i in range(self.rotors):
self._positions[i] = 1
def advances(self):
for i in range(self.rotors):
self._advances[i] = 1
def set_seed(self):
self.seed[0] = self.key[0]
self.seed[1] = self.key[1]
self.seed[2] = self.key[2]
def set_key(self, key):
k1 = 995
k2 = 576
k3 = 767
k4 = 671
k5 = 463
for ki in key:
ki = ord(ki)
k1 = (((k1 << 3 | k1 >> 13) + ki) & 0xFFFF)
k2 = (((k2 << 3 | k2 >> 13) ^ ki) & 0xFFFF)
k3 = (((k3 << 3 | k3 >> 13) - ki) & 0xFFFF)
k4 = ((ki - (k4 << 3 | k4 >> 13)) & 0xFFFF)
k5 = (((k5 << 3 | k5 >> 13) ^ (~ki & 0xFFFF)) & 65535)
self.key[0] = ctypes.c_short(k1).value
self.key[1] = ctypes.c_short(k2 | 1).value
self.key[2] = ctypes.c_short(k3).value
self.key[3] = ctypes.c_short(k4).value
self.key[4] = ctypes.c_short(k5).value
def RTR_advance(self):
i = 0
temp = 0
while (i < self.rotors):
temp = self._positions[i] + self._advances[i]
self._positions[i] = ctypes.c_uint8(
to_int(math.fmod(temp, self.size))).value
if (temp >= self.size) and (i < (self.rotors - 1)):
self._positions[i+1] = ctypes.c_uint8(1 +
self._positions[i+1]).value
i += 1
def RTR_d_char(self, c):
i = self.rotors - 1
tc = c
while 0 <= i:
# print(i, tc, self.d_rotor[i][tc])
tc = ctypes.c_uint8((self._positions[i] ^
to_int(math.fmod(self.d_rotor[i][tc], self.size)))).value
i -= 1
self.RTR_advance()
return tc
def decrypt(self, data):
data_put = bytearray()
for i in range(len(data)):
d = data[i]
if type(d) is str:
d = ord(d)
# print(self.RTR_d_char(d))
# return
data_put.append(self.RTR_d_char(d))
return bytes(data_put)
def r_random(self):
x = self.seed[0]
y = self.seed[1]
z = self.seed[2]
# oy = y
# ox = x
x = 171 * to_int(math.fmod(x, 177)
) - 2 * to_int(x/177.0)
y = 172 * to_int(math.fmod(y, 176)
) - 35 * to_int(y/176.0)
z = 170 * to_int(math.fmod(z, 178)
) - 63 * to_int(z/178.0)
# print(x, ox, 171 * to_int(math.fmod(ox, 177.0)),
# int(ox/177.0),
# 2 * to_int(ox/177.0))
# asdjasdklajsldjalsjdlka
# X: 25928, Y: -11943, Z: 4464
if x < 0:
x = x + 30269
if y < 0:
y = y + 30307
if z < 0:
z = z + 30323
self.seed[0] = x
self.seed[1] = y
self.seed[2] = z
term = (x/30269.0) + (y/30307.0) + (z/30323.0)
val = term - to_int(term)
if val >= 1.0:
val = 0.0
return val
def r_rand(self, s):
n = ctypes.c_short(int(self.r_random() * float(s))).value
return ctypes.c_short(int(math.fmod(n, s))).value
def newrotor(key):
return Rotor(key)