-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
58 lines (39 loc) · 1.49 KB
/
preprocessing.py
File metadata and controls
58 lines (39 loc) · 1.49 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
class Preprocessing_Algorithm:
def __init__(self, plain_text):
self.plain_text = plain_text
def binary(self, ascii_conversion : int) -> str:
output = ""
while ascii_conversion:
output += str(ascii_conversion % 2)
ascii_conversion //= 2
output = output[::-1]
length = len(output)
return output if length == 8 else (8 - length) * "0" + output
def get_binary_len(self, pad_length : int) -> str:
output = ""
while pad_length:
output += str(pad_length % 2)
pad_length //= 2
output = output[::-1]
length = len(output)
return output if length == 64 else (64 - length) * "0" + output
def get_padding(self) -> str:
padded_text = ""
for p in self.plain_text:
padded_text += self.binary(ord(p))
padded_text += "1"
length = len(padded_text) # You can use it to see the length of padded text
while length % 512 != 448:
padded_text += "0"
length += 1
original_length = len(self.plain_text) * 8
binary_length = self.get_binary_len(original_length)
padded_text += binary_length
return padded_text
def get_parsed_data(self) -> list:
padded_text = self.get_padding()
length = len(padded_text)
parsed_data = []
for k in range(0, length, 512):
parsed_data.append(padded_text[k : k + 512])
return parsed_data