-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_stream_cipher.py
More file actions
60 lines (46 loc) · 1.88 KB
/
basic_stream_cipher.py
File metadata and controls
60 lines (46 loc) · 1.88 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
import random
import utils
class StreamCipher:
def __init__(self, seed):
self.state = utils.state_init(seed)
random.setstate(self.state)
def update(self, new_state):
self.state = new_state
random.setstate(self.state)
def get_state(self):
return self.state
def encrypt(self, plaintext):
bits = utils.string_to_bits(plaintext)
key, new_state = utils.key_gen_bits(bits, self.state)
ciphertext = utils.xor_bits(bits, key)
self.update(new_state)
return ciphertext, key
def decrypt(self, ciphertext):
bits = ciphertext
key, new_state = utils.key_gen_bits(bits, self.state)
plain_bits = utils.xor_bits(ciphertext, key)
plaintext = utils.bits_to_string(plain_bits)
self.update(new_state)
return plaintext, key
if __name__ == '__main__':
plaintext = 'ILYA SMUT'
seed = 'SecreT'
state = utils.state_init(seed)
a = StreamCipher(seed)
b = StreamCipher(seed)
print('\n\nAre intial states in sync?', a.get_state() == b.get_state())
text = 'ILYA SMUT'
a_cipher, _ = a.encrypt(text)
print('\nCipher A has performed encryption of Plaintext:', text, '\nCiphertext:', a_cipher)
print('Are states in sync?', a.get_state() == b.get_state())
b_plaintext, _ = b.decrypt(a_cipher)
print('\nCipher B has performed decryption. Decrypted text:', b_plaintext)
print('Are states in sync?', a.get_state() == b.get_state())
text = 'I AM COMMUNICATING'
b_cipher, _ = b.encrypt(text)
print('\nCipher B has performed encryption:', text, '\nCiphertext:', b_cipher)
print('Are states in sync?', a.get_state() == b.get_state())
a_plaintext, _ = a.decrypt(b_cipher)
print('\nCipher A has performed decryption. Plaintext:', a_plaintext)
print('Are states in sync?', a.get_state() == b.get_state())
print()