-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES.py
More file actions
224 lines (199 loc) · 6.91 KB
/
AES.py
File metadata and controls
224 lines (199 loc) · 6.91 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# AES
# encryption and decryption using the AES method (Advanced Encryption System)
# note: need to install numpy: pip3 install numpy
# AES
def encrypt(message):
key = input("Enter User Defined Password: ")
cipher = []
length = len(message)
remainder = length%16
num_of_blocks = int(((length-remainder)/16)+1)
cipher.append(num_of_blocks)
for blocks in range(num_of_blocks):
message_block = get_message(message)
message = list(message)
del message[0:16]
''.join(message)
key_block = get_key(key)
cipher.append(encrypt_1(message_block,key_block))
return cipher
# if decrypt is called
def decrypt(message):
try:
key = input("Enter Password: ")
decrypted = []
num_of_blocks = message[0]
for blocks in range(num_of_blocks):
message_block = message[blocks+1]
key_block = get_key(key)
d = decrypt_1(message_block,key_block)
decrypted.append(d)
return ''.join(decrypted)
except:
print('ERROR: INCORRECT PASSWORD')
def get_message(message):
# only returns one of the blocks
#convert to 4 by 4 matrix
message = list(message)
output = []
#convert to ascii
ascii_message = []
for non_ascii in message:
ascii_message.append(ord(non_ascii))
while len(ascii_message)<4:
# append with space
ascii_message.append(32)
for row_of_4 in range(4):
output.append((ascii_message[0:4]))
#delete thatthe first 4
del ascii_message[0:4]
while len(ascii_message)<4:
# append with space
ascii_message.append(32)
return output
def get_key(key):
#gets the key in a block
if len(key)<=16:
#must input ascii number in the block
key = list(key)
output = []
#convert to ascii
ascii_key = []
for non_ascii in key:
ascii_key.append(ord(non_ascii))
while len(ascii_key)<4:
# append with space
ascii_key.append(32)
for row_of_4 in range(4):
output.append((ascii_key[0:4]))
#delete thatthe first 4
del ascii_key[0:4]
while len(ascii_key)<4:
# append with space
ascii_key.append(32)
elif len(key)>16:
# if length is longer than 16, need to group it
key = list(key)
output = []
#convert to ascii
ascii_key = []
for non_ascii in key:
ascii_key.append(ord(non_ascii))
for row_of_4 in range(4):
output.append((ascii_key[0:4]))
#delete thatthe first 4
del ascii_key[0:4]
#add remaining to other first few
while len(ascii_key)!=0:
# add remainig ones to first couple
for row in range(0,4):
for column in range(0,4):
if len(ascii_key)==0:
break
output[row][column] += ascii_key[0]
del ascii_key[0]
return output
def multiply_matrix(matrix1,matrix2):
import numpy as np
output = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
for move_row in range(0,4):
for move_col in range(0,4):
#matrix multiplication
output[move_row][move_col] = (int(round(np.dot(matrix1[move_row],[matrix2[0][move_col],matrix2[1][move_col],matrix2[2][move_col],matrix2[3][move_col],]))))
return output
#NOTE formating block[row][column]
def encrypt_1(message_block,key_block):
output = []
#STEP 1: XOR key and ascii text
for xor_row in range(0,4):
output.append([ message_block[xor_row][0]^key_block[xor_row][0],
message_block[xor_row][1]^key_block[xor_row][1],
message_block[xor_row][2]^key_block[xor_row][2],
message_block[xor_row][3]^key_block[xor_row][3]
])
#=====================================
#STEP 2: 10 loops of sub, row shift, column mix, and XOR
for NineLoops in range(0,9):
#STEP 2-i row shift, 1st row, now shift, 2nd row shift by 1
for shift_row in range(0,4):
for shift_amount in range(0,shift_row):
output[shift_row].append(output[shift_row].pop(0))
#STEP 2-ii column mixer
matrix = [[2,3,1,1],[1,2,3,1],[1,1,2,3],[3,1,1,2]]
output = multiply_matrix(matrix,output)
#STEP 2-iii XOR again
for xor_row2 in range(0,4):
output[xor_row2][0] = output[xor_row2][0]^key_block[xor_row2][0]
output[xor_row2][1] = output[xor_row2][1]^key_block[xor_row2][1]
output[xor_row2][2] = output[xor_row2][2]^key_block[xor_row2][2]
output[xor_row2][3] = output[xor_row2][3]^key_block[xor_row2][3]
#end of loop
#=============================================
# end with row shift
for shift_row in range(0,4):
for shift_amount in range(0,shift_row):
output[shift_row].append(output[shift_row].pop(0))
for xor_row3 in range(0,4):
output[xor_row3][0] = output[xor_row3][0]^key_block[xor_row3][0]
output[xor_row3][1] = output[xor_row3][1]^key_block[xor_row3][1]
output[xor_row3][2] = output[xor_row3][2]^key_block[xor_row3][2]
output[xor_row3][3] = output[xor_row3][3]^key_block[xor_row3][3]
#make output a string and output
# output_string = ''.join([''.join(str(x) for x in output[0]),''.join(str(x) for x in output[1]),''.join(str(x) for x in output[2]),''.join(str(x) for x in output[3])])
return output
def decrypt_1(message_block,key_block):
output = []
for xor_row3 in range(0,4):
output.append([ message_block[xor_row3][0]^key_block[xor_row3][0],
message_block[xor_row3][1]^key_block[xor_row3][1],
message_block[xor_row3][2]^key_block[xor_row3][2],
message_block[xor_row3][3]^key_block[xor_row3][3]
])
for shift_row in range(0,4):
for shift_amount in range(0,shift_row):
output[shift_row].insert(0,output[shift_row].pop(3))
#decrypt goes in reverse
for NineLoops in range(0,9):
#STEP 1: start with XOR
for xor_row2 in range(0,4):
output[xor_row2][0] = output[xor_row2][0]^key_block[xor_row2][0]
output[xor_row2][1] = output[xor_row2][1]^key_block[xor_row2][1]
output[xor_row2][2] = output[xor_row2][2]^key_block[xor_row2][2]
output[xor_row2][3] = output[xor_row2][3]^key_block[xor_row2][3]
#STEP 2: column mixer
#encryped * inverse matrix = reverse
inverse_matrix = [[-4/35,3/35,-11/35,17/35],[17/35,-4/35,3/35,-11/35],[-11/35,17/35,-4/35,3/35],[3/35,-11/35,17/35,-4/35]]
output = multiply_matrix(inverse_matrix,output)
#STEP 3: row shifter
for shift_row in range(0,4):
for shift_amount in range(0,shift_row):
output[shift_row].insert(0,output[shift_row].pop(3))
#end of loop
#STEP 4: XOR key and ascii text
for xor_row2 in range(0,4):
output[xor_row2][0] = output[xor_row2][0]^key_block[xor_row2][0]
output[xor_row2][1] = output[xor_row2][1]^key_block[xor_row2][1]
output[xor_row2][2] = output[xor_row2][2]^key_block[xor_row2][2]
output[xor_row2][3] = output[xor_row2][3]^key_block[xor_row2][3]
original_message = []
try:
#STEP 5 :convert to ascii
for ascii_row in range(0,4):
for ascii_column in range(0,4):
original_message.append(chr(output[ascii_row][ascii_column]))
return ''.join(original_message)
except ValueError:
raise
# ------------------------------------------------------
print("Advanced Encryption System\n")
message = input("Enter Message to be Encrypted: ")
cipher = encrypt(message)
print("\nEncrypting...")
print("Encrypted Cipher: ")
print(cipher)
next = input("\nDo You Wish to Decrypt [y/n]?")
if next == ("y" or "Y"):
decrypted = decrypt(cipher)
print("\nDecrypting...")
print("Decrypted Message: ")
print(decrypted)