-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriviumStreamCipher.py
97 lines (79 loc) · 2.53 KB
/
TriviumStreamCipher.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
# Functions
def _hex_to_bytes(s):
return [_allbytes[s[i:i+2].upper()] for i in range(0, len(s), 2)]
def _bytes_to_hex(s):
return ''.join(format(X, '02X') for X in s)
def bits_to_hex(b):
return "".join(["%02X" % sum([b[i + j] << j for j in range(8)]) for i in range(0, len(b), 8)])
def hex_to_bits(s):
return [(b >> i) & 1 for b in _hex_to_bytes(s) for i in range(8)]
_allbytes = dict([("%02X" % i, i) for i in range(256)])
def add_head(t, s):
s.insert(0, t)
s.pop()
def rotate(s1, s2, s3):
t1 = s1[65] ^ s1[92]
t2 = s2[68] ^ s2[83]
t3 = s3[65] ^ s3[110]
z = t1 ^ t2 ^ t3
t1 = t1 ^ (s1[90] & s1[91]) ^ s2[77]
t2 = t2 ^ (s2[81] & s2[82]) ^ s3[86]
t3 = t3 ^ (s3[108] & s3[109]) ^ s1[68]
add_head(t3, s1)
add_head(t1, s2)
add_head(t2, s3)
return z
def init_state(s1, s2, s3):
for i in range(4 * 288):
rotate(s1, s2, s3)
def key_stream(s1, s2, s3):
for i in range(2**64):
yield rotate(s1, s2, s3)
# __main__
plaintext = "Hanoi University of Science and Technology"
key_string = "0F62B5085BAE0154A7FA"
iv_string = "288FF65DC42B92F960C7"
print("Key: " + key_string)
print("IV: " + iv_string)
# parse KEY and IV from string to 80bits
KEY = hex_to_bits(key_string)[::-1]
IV = hex_to_bits(iv_string)[::-1]
# initializal internal state
s1_s93 = KEY
s94_s177 = IV
s178_s288 = []
for i in range(13):
s1_s93.append(0)
s94_s177 = s94_s177 + [0, 0, 0, 0]
for i in range(108):
s178_s288.append(0)
s178_s288 = s178_s288 + [1, 1, 1]
init_state(s1_s93, s94_s177, s178_s288)
# key_stream
next_key_bit = key_stream(s1_s93, s94_s177, s178_s288)
keystream = []
for j in range(8*len(plaintext)):
keystream.append(next(next_key_bit))
# encrypto
print("====================Encrypto====================")
print("Plaintext: " + "'" + plaintext + "'")
plaintext_dec = []
for i in range(len(plaintext)):
plaintext_dec.append((ord(plaintext[i])))
print("Key stream: " + bits_to_hex(keystream))
key_stream_dec = _hex_to_bytes(bits_to_hex(keystream))
ciphertext = ''
ciphertext_dec = []
for i in range(len(plaintext)):
cipher = plaintext_dec[i] ^ key_stream_dec[i]
ciphertext_dec.append(cipher)
ciphertext += chr(cipher)
print("Ciphertext: " + "'" + ciphertext + "'")
print("Ciphertext Hexa: " + (_bytes_to_hex(ciphertext_dec)).upper())
# decrypto
print("====================Decrypto====================")
plain_text = ''
for i in range(len(ciphertext)):
plain = ciphertext_dec[i] ^ key_stream_dec[i]
plain_text += chr(plain)
print("Plaintext: " + "'" + plain_text + "'")