-
Notifications
You must be signed in to change notification settings - Fork 0
/
CriptoBlock.py
57 lines (47 loc) · 1.98 KB
/
CriptoBlock.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
#!/usr/bin/env python
def Xor(buf):
cccam = "CCcam"
for i in range(0, 8):
buf[8 + i] = 0xff & (i * buf[i])
if i < 5:
buf[i] ^= ord(cccam[i])
return buf
class CryptographicBlock(object):
def __init__(self):
self._keytable = [0] * 256
self._state = 0
self._counter = 0
self._sum = 0
def Init(self, key, len):
for i in range(0, 256):
self._keytable[i] = i
j = 0
for i in range(0, 256):
j = 0xff & (j + key[i % len] + self._keytable[i])
self._keytable[i], self._keytable[j] = self._keytable[j], self._keytable[i]
self._state = key[0]
self._counter = 0
self._sum = 0
def Decrypt(self, data, len):
for i in range(0, len):
self._counter = 0xff & (self._counter + 1)
self._sum = self._sum + self._keytable[self._counter]
#Swap keytable[counter] with keytable[sum]
self._keytable[self._counter], self._keytable[self._sum & 0xFF] = \
self._keytable[self._sum & 0xFF], self._keytable[self._counter]
z = data[i]
data[i] = z ^ self._keytable[(self._keytable[self._counter] + \
self._keytable[self._sum & 0xFF]) & 0xFF] ^ self._state
z = data[i]
self._state = 0xff & (self._state ^ z)
def Encrypt(self, data, len):
for i in range(0, len):
self._counter = 0xff & (self._counter + 1)
self._sum = self._sum + self._keytable[self._counter]
#Swap keytable[counter] with keytable[sum]
self._keytable[self._counter], self._keytable[self._sum & 0xFF] = \
self._keytable[self._sum & 0xFF], self._keytable[self._counter]
z = data[i]
data[i] = z ^ self._keytable[(self._keytable[self._counter & 0xFF] + \
self._keytable[self._sum & 0xFF]) & 0xff] ^ self._state
self._state = 0xff & (self._state ^ z)