-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBC.py
147 lines (104 loc) · 5.25 KB
/
CBC.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
import AES as aes # Import AES to implement CBC
import random # It is used to generate random integer
WORD_LENGTH = 4 # Length of matrix
SIZE = 16 # Length of the texts
# It generates a random initialization vector with size 16
def generate_initialization_vector():
in_vector = [0 for x in range(SIZE)]
for i in range(SIZE):
in_vector[i] = random.randint(1, 101)
return in_vector
# It splits the text 16 bytes by 16 bytes
# It adds 'X' to the text if it is not multiple of 16
def split_plain_text(string):
plain_text_arr = [string[i:i + SIZE] for i in range(0, len(string), SIZE)]
size_of_last_part = len(plain_text_arr[len(plain_text_arr) - 1])
added_item_count = SIZE - size_of_last_part
last_item_index = len(plain_text_arr) - 1
for i in range(added_item_count):
plain_text_arr[last_item_index] = plain_text_arr[last_item_index] + 'X'
return plain_text_arr, added_item_count
# It takes a hex string matrix and converts it to a hex string array
def reverse_matrix_to_hex_string_arr(matrix):
arr = ['' for x in range(SIZE)]
index = 0
for i in range(WORD_LENGTH):
for j in range(WORD_LENGTH):
arr[index] = matrix[j][i]
index = index + 1
return arr
# It converts the string array to string
# It remove the 'X's if it was added
def merge_strings(str_arr, remove_count):
text = ""
if remove_count == 0:
for string in str_arr:
text = text + string
else:
final = str_arr.pop(len(str_arr) - 1)
for string in str_arr:
text = text + string
for i in range(SIZE - remove_count):
text = text + final[i]
return text
# It converts the string array to string
# It does not remove the 'X's
def merge_strings_without_remove_item(str_arr):
text = ""
for string in str_arr:
text = text + string
return text
# It is encryption operation for the CBC
# It takes the main key, message and the initialization vector
# It returns count of added 'X's and encrypted message as hex string 2d array
def CBC_encryption(key_val, text_val, iv_enc_val):
plain_text_arr, added_item_count = split_plain_text(text_val)
key_in_hex = aes.translate_string_into_hex_str(key_val)
all_round_key = aes.find_all_round_keys(key_in_hex)
cipher_text_arr = []
hex_plain_text = aes.translate_string_into_hex_str(plain_text_arr[0])
hex_plain_text_matrix = aes.generate_4x4_matrix(hex_plain_text)
hex_iv_enc = aes.make_int_arr_to_hex(iv_enc_val)
hex_iv_enc_matrix = aes.generate_4x4_matrix(hex_iv_enc)
current_input = aes.add_round_key(hex_iv_enc_matrix, hex_plain_text_matrix)
current_input = reverse_matrix_to_hex_string_arr(current_input)
cipher_text = aes.encrypt(current_input, all_round_key)
cipher_text_arr.append(cipher_text)
for i in range(1, len(plain_text_arr)):
plain_text = plain_text_arr[i]
hex_plain_text = aes.translate_string_into_hex_str(plain_text)
hex_plain_text_matrix = aes.generate_4x4_matrix(hex_plain_text)
cipher_text_matrix = aes.generate_4x4_matrix(cipher_text)
current_input = aes.add_round_key(cipher_text_matrix, hex_plain_text_matrix)
current_input = reverse_matrix_to_hex_string_arr(current_input)
cipher_text = aes.encrypt(current_input, all_round_key)
cipher_text_arr.append(cipher_text)
return cipher_text_arr, added_item_count
# It is decryption operation for the CBC
# It takes the main key, encrypted message, initialization vector and count of added 'X's to remove
# It returns count of added 'X's and encrypted message as hex string 2d array
def CBC_decryption(key_val, cipher_text_arr, iv_dec_val, remove_count):
key_in_hex = aes.translate_string_into_hex_str(key_val)
all_round_key = aes.find_all_round_keys(key_in_hex)
plain_text_arr = []
cipher_text = cipher_text_arr[0]
plain_text_hex = aes.decryption(cipher_text, all_round_key)
plain_text_hex_matrix = aes.generate_4x4_matrix(plain_text_hex)
hex_iv_dec = aes.make_int_arr_to_hex(iv_dec_val)
hex_iv_dec_matrix = aes.generate_4x4_matrix(hex_iv_dec)
plain_text_hex_matrix = aes.add_round_key(hex_iv_dec_matrix, plain_text_hex_matrix)
plain_text_hex = reverse_matrix_to_hex_string_arr(plain_text_hex_matrix)
plain_text_str = aes.translate_hex_into_str(plain_text_hex)
plain_text_arr.append(plain_text_str)
for i in range(1, len(cipher_text_arr)):
cipher_text_1 = cipher_text_arr[i]
cipher_text_2 = cipher_text_arr[i - 1]
plain_text_hex = aes.decryption(cipher_text_1, all_round_key)
plain_text_hex_matrix = aes.generate_4x4_matrix(plain_text_hex)
cipher_text_matrix = aes.generate_4x4_matrix(cipher_text_2)
plain_text_hex_matrix = aes.add_round_key(cipher_text_matrix, plain_text_hex_matrix)
plain_text_hex = reverse_matrix_to_hex_string_arr(plain_text_hex_matrix)
plain_text_str = aes.translate_hex_into_str(plain_text_hex)
plain_text_arr.append(plain_text_str)
resolved_text_val = merge_strings(plain_text_arr, remove_count)
return resolved_text_val