-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask1.py
More file actions
94 lines (76 loc) · 2.75 KB
/
task1.py
File metadata and controls
94 lines (76 loc) · 2.75 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
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
import os
from bitarray._bitarray import bitarray
from bitarray.util import *
def right_shift(array, n):
res = bitarray(len(array))
for i in range(len(array)):
res[i] = array[i]
for j in range(n):
for i in reversed(range(1, len(array))):
res[i] = array[i - 1]
res[0] = 0
array = res
return res
def left_shift(array, n):
res = bitarray(len(array))
for i in range(len(array)):
res[i] = array[i]
for j in range(n):
for i in range(len(array) - 1):
res[i] = array[i + 1]
res[-1] = 0
array = res
return res
def bytestring2bitarray(input_string):
bits_input_string = bitarray()
for char in input_string:
aa = ''.join(['0'] * (8 - len(bin(char)[2:])))
bits_input_string += aa + bin(char)[2:]
return bits_input_string
def F(left, key):
return left_shift(left, 9) ^ (~(right_shift(key, 11) & left))
def get_K_i(K, i):
return right_shift(K, i * 8)[:32]
def encrypt(plaintext, key, n=2, size_block=64):
for i in range(n):
res = bitarray()
k_i = get_K_i(key, i)
for index_of_block in range(len(plaintext) // size_block):
block = plaintext[size_block * index_of_block: size_block * (index_of_block + 1)]
left = block[0:size_block // 2]
right = block[size_block // 2: size_block]
temp = F(left, k_i) ^ right
if i == n - 1:
new_block = left + temp
else:
new_block = temp + left
res += new_block
plaintext = res
return plaintext
def decrypt(plaintext, key, n=2, size_block=64):
for i in range(n):
res = bitarray()
k_i = get_K_i(key, n - i - 1)
for index_of_block in range(len(plaintext) // size_block):
block = plaintext[size_block * index_of_block: size_block * (index_of_block + 1)]
left = block[0:size_block // 2]
right = block[size_block // 2: size_block]
temp = F(left, k_i) ^ right
if i == n - 1:
new_block = left + temp
else:
new_block = temp + left
res += new_block
plaintext = res
return plaintext
input_string = b'Very very secret text'
input_string = bytes([0] * (8 - len(input_string) % 8)) + input_string
binput_string = bytestring2bitarray(input_string)
key = bytearray(os.urandom(8))
bkey = bytestring2bitarray(key)
n = 10
print('исходн:', binput_string)
print('_ключ_:', bkey)
print('зашфрв:', encrypt(binput_string, bkey, n))
print('расшфр:', decrypt(encrypt(binput_string, bkey, n), bkey, n))
print('текст: ', ba2int(decrypt(encrypt(binput_string, bkey, n), bkey, n)).to_bytes(21, 'big').decode())