-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes128.py
98 lines (77 loc) · 3.21 KB
/
aes128.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
# TODO: Possibly add the option to do Cipher Block Chaining (CBC) mode?
import hashlib
import encrypt
import decrypt
def prompt_user():
choice = input("Would you like to:\n\t[1] encrypt a message\n\t[2] decrypt a message\nEnter here: ")
while choice not in ["1", "2"]:
choice = input("Please input either 1 or 2.\n\t[1] to encrypt a message\n\t[2] to decrypt a message.\nEnter "
"here: ")
if choice == "1":
choice = "encrypt"
message = input("Input the message to encrypt: ")
while not message:
message = input("Please enter the message you'd like to encrypt: ")
message = message.encode("utf-8").hex()
# I am aware this is probably not secure or what happens in practice. Choosing the key is not in the
# specification, so I thought to make the key this because it was the first thing that came to mind and was
# easy to implement.
key = input("Input the key you'd like to use to encrypt the message with: ")
while not key:
key = input("Please enter the key you'd like to use to encrypt the message with: ")
key = hashlib.sha256(key.encode("utf-8")).hexdigest()[:32]
return choice, message, key
elif choice == "2":
print("Decrypt not implemented yet :(")
exit(1)
def tokenize(string, n):
return [string[i:i + n] for i in range(0, len(string), n)]
def hexify(tokenized_arr):
msg_length = len(tokenized_arr)
pad_length = 0 if msg_length % 16 == 0 else ((msg_length // 16) + 1) * 16 - msg_length
hex_arr = []
for token in tokenized_arr:
hex_arr.append(int(token, 16))
for i in range(pad_length):
hex_arr.append(0)
return hex_arr
def blockify_and_matrix_msg(tokenized_msg):
blockified_msg = []
for i in range(len(tokenized_msg) // 16):
blockified_msg.append([])
for j in range(4):
start_index = 4 * (4 * i + j)
end_index = 4 * (4 * i + j + 1)
blockified_msg[i].append(tokenized_msg[start_index:end_index])
return blockified_msg
def main():
"""
message = [
[
["3F", "EE", "C0", "E3"], ["93", "29", "87", "A8"],
["F7", "78", "65", "87"], ["D4", "AE", "0B", "EF"]
]
]
key = [
["42", "69", "54", "21"], ["F5", "79", "C9", "FE"],
["8D", "65", "52", "41"], ["96", "9F", "75", "05"]
]
"""
choice, message, key = prompt_user()
# TODO: For decryption (whenever that time comes), bytes.fromhex(message).decode('utf-8') converts back to msg
print("message: {}".format(message))
tokenized_msg = tokenize(message, 2)
print("tokenized message: {}".format(tokenized_msg))
hexified_msg = hexify(tokenized_msg)
print("hexified message: {}".format(hexified_msg))
blockified_msg = blockify_and_matrix_msg(hexified_msg)
print("blockified message: {}".format(blockified_msg))
print(key)
tokenized_key = tokenize(key, 2)
hexified_key = hexify(tokenized_key)
blockified_key = blockify_and_matrix_msg(hexified_key)[0]
print(blockified_key)
print("-------------------------")
encrypt.encrypt(blockified_msg, blockified_key)
if __name__ == "__main__":
main()