-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdecode.src
More file actions
345 lines (343 loc) · 14 KB
/
decode.src
File metadata and controls
345 lines (343 loc) · 14 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
AES128 = function(choice, key, text)
if typeof(key) != "string" then return null
if key.len != 16 then return null
if typeof(text) != "string" then return null
if typeof(choice) != "string" then return null
byte_key = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in key.indexes
byte_key[i] = key[i].code
end for
key = byte_key[0:]
Sbox = []
Sbox=Sbox+[99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38]
Sbox=Sbox+[54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179]
Sbox=Sbox+[41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64]
Sbox=Sbox+[143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70]
Sbox=Sbox+[238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174]
Sbox=Sbox+[8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105]
Sbox=Sbox+[217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]
Rcon = [1, 2, 4, 8, 16, 32, 64, 128, 27, 54]
Mult2 = []
Mult2=Mult2+[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90]
Mult2=Mult2+[92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158]
Mult2=Mult2+[160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,212,214,216,218,220,222,224]
Mult2=Mult2+[226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,27,25,31,29,19,17,23,21,11,9,15,13,3,1,7,5,59,57,63,61,51,49,55,53,43,41]
Mult2=Mult2+[47,45,35,33,39,37,91,89,95,93,83,81,87,85,75,73,79,77,67,65,71,69,123,121,127,125,115,113,119,117,107,105,111,109,99,97,103,101,155]
Mult2=Mult2+[153,159,157,147,145,151,149,139,137,143,141,131,129,135,133,187,185,191,189,179,177,183,181,171,169,175,173,163,161,167,165,219,217]
Mult2=Mult2+[223,221,211,209,215,213,203,201,207,205,195,193,199,197,251,249,255,253,243,241,247,245,235,233,239,237,227,225,231,229]
SubBytes = function(column)
column = column[0:]
for i in column.indexes
column[i] = Sbox[column[i]]
end for
return column
end function
WordXor = function(word1, word2)
result = [0, 0, 0, 0]
for i in result.indexes
result[i] = bitwise("^", word1[i], word2[i])
end for
return result
end function
AddRoundKey = function(state, key, roundNum)
roundKey = key[roundNum*4:roundNum*4+4]
return [WordXor(state[0], roundKey[0]), WordXor(state[1], roundKey[1]), WordXor(state[2], roundKey[2]), WordXor(state[3], roundKey[3])]
end function
ExpandKey = function(key)
W = [key[0:4], key[4:8], key[8:12], key[12:16]]
for i in range(4, 40, 4)
W = W + [[], [], [], []]
W[i] = W[i-1][1:] + [W[i-1][0]]
W[i] = SubBytes(W[i])
W[i] = WordXor(W[i-4], W[i])
W[i][0] = bitwise("^", Rcon[i/4-1], W[i][0])
for j in range(i+1, i+3)
W[j] = WordXor(W[j-4], W[j-1])
end for
end for
return W
end function
aesEncrypt = function(key, block)
Mult2 = @Mult2
key = key[0:]
State = block[0:]
ShiftRows = function(state)
state = state[0:]
tmp = state[0][1]
state[0][1] = state[1][1]
state[1][1] = state[2][1]
state[2][1] = state[3][1]
state[3][1] = tmp
for i in range(1)
tmp = state[0][2]
state[0][2] = state[1][2]
state[1][2] = state[2][2]
state[2][2] = state[3][2]
state[3][2] = tmp
end for
tmp = state[3][3]
state[3][3] = state[2][3]
state[2][3] = state[1][3]
state[1][3] = state[0][3]
state[0][3] = tmp
return state
end function
matrix = [[2, 3, 1, 1], [1, 2, 3, 1], [1, 1, 2, 3], [3, 1, 1, 2]]
MixColumns = function(state)
state = state[0:]
for column in state.indexes
endcolumn = [0, 0, 0, 0]
for i in matrix.indexes
tmp = state[column][0:]
for j in matrix[i].indexes
if matrix[i][j] == 1 then continue
tmp[j] = Mult2[state[column][j]]
if matrix[i][j] != 3 then continue
tmp[j] = bitwise("^", tmp[j], state[column][j])
end for
endcolumn[i] = bitwise("^", bitwise("^", bitwise("^", tmp[0], tmp[1]), tmp[2]), tmp[3])
end for
state[column] = endcolumn[0:]
end for
return state
end function
State = AddRoundKey(State, key, 0)
for round in range(1, 9)
State = [SubBytes(State[0]), SubBytes(State[1]), SubBytes(State[2]), SubBytes(State[3])]
State = ShiftRows(State)
State = MixColumns(State)
State = AddRoundKey(State, key, round)
end for
State = [SubBytes(State[0]), SubBytes(State[1]), SubBytes(State[2]), SubBytes(State[3])]
State = ShiftRows(State)
State = AddRoundKey(State, key, 10)
return State
end function
aesDecrypt = function(key, block)
Mult2 = @Mult2
Sbox = @Sbox
key = key[0:]
State = block[0:]
InvShiftRows = function(state)
state = state[0:]
tmp = state[3][1]
state[3][1] = state[2][1]
state[2][1] = state[1][1]
state[1][1] = state[0][1]
state[0][1] = tmp
for i in range(1)
tmp = state[3][2]
state[3][2] = state[2][2]
state[2][2] = state[1][2]
state[1][2] = state[0][2]
state[0][2] = tmp
end for
tmp = state[0][3]
state[0][3] = state[1][3]
state[1][3] = state[2][3]
state[2][3] = state[3][3]
state[3][3] = tmp
return state
end function
InvSubBytes = function(column)
column = column[0:]
for i in column.indexes
column[i] = Sbox.indexOf(column[i])
end for
return column
end function
matrix = [[14, 11, 13, 9], [9, 14, 11, 13], [13, 9, 14, 11], [11, 13, 9, 14]]
InvMixColumns = function(state)
state = state[0:]
for column in state.indexes
endcolumn = [0, 0, 0, 0]
for i in matrix.indexes
tmp = state[column][0:]
for j in matrix[i].indexes
if matrix[i][j] == 9 then
tmp[j] = bitwise("^", Mult2[Mult2[Mult2[state[column][j]]]], state[column][j])
else if matrix[i][j] == 11 then
tmp[j] = bitwise("^", Mult2[bitwise("^", Mult2[Mult2[state[column][j]]], state[column][j])], state[column][j])
else if matrix[i][j] == 13 then
tmp[j] = bitwise("^", Mult2[Mult2[bitwise("^", Mult2[state[column][j]], state[column][j])]], state[column][j])
else
tmp[j] = Mult2[bitwise("^", Mult2[bitwise("^", Mult2[state[column][j]], state[column][j])], state[column][j])]
end if
end for
endcolumn[i] = bitwise("^", bitwise("^", bitwise("^", tmp[0], tmp[1]), tmp[2]), tmp[3])
end for
state[column] = endcolumn[0:]
end for
return state
end function
State = AddRoundKey(State, key, 10)
for round in range(9, 1)
State = InvShiftRows(State)
State = [InvSubBytes(State[0]), InvSubBytes(State[1]), InvSubBytes(State[2]), InvSubBytes(State[3])]
State = AddRoundKey(State, key, round)
State = InvMixColumns(State)
end for
State = InvShiftRows(State)
State = [InvSubBytes(State[0]), InvSubBytes(State[1]), InvSubBytes(State[2]), InvSubBytes(State[3])]
State = AddRoundKey(State, key, 0)
return State
end function
IV = []
for byte in key
IV = IV + [floor(rnd(byte)*256)]
end for
key = ExpandKey(key)
b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
if choice == "encrypt" then
Blocks = [[]]
b=0
for char in text
Blocks[b] = Blocks[b] + [code(char)]
if Blocks[b].len == 16 then
Blocks = Blocks + [[]]
b=b+1
end if
end for
padNum = 16 - Blocks[-1].len
for i in range(padNum-1)
Blocks[-1] = Blocks[-1] + [padNum]
end for
for i in IV.indexes
Blocks[0][i] = bitwise("^", Blocks[0][i], IV[i])
end for
for i in Blocks.indexes
Blocks[i] = [Blocks[i][0:4], Blocks[i][4:8], Blocks[i][8:12], Blocks[i][12:16]]
end for
Blocks[0] = aesEncrypt(key, Blocks[0])
if Blocks.len > 1 then
for i in range(1, Blocks.len-1)
for column in Blocks[i].indexes
for byte in Blocks[i][column].indexes
Blocks[i][column][byte] = bitwise("^", Blocks[i][column][byte], Blocks[i-1][column][byte])
end for
end for
Blocks[i] = aesEncrypt(key, Blocks[i])
end for
end if
arr = []
for Block in Blocks
for column in Block
for byte in column
arr = arr + [byte]
end for
end for
end for
output = ""
for i in range(0, arr.len-1, 3)
buffer = arr[i]*65536
if arr.hasIndex(i+1) then
buffer = buffer+arr[i+1]*256
if arr.hasIndex(i+2) then buffer = buffer+arr[i+2]
end if
if arr.hasIndex(i+2) then
for j in range(3)
output = output + b64Table[floor(buffer/64^j)%64]
end for
else
if arr.hasIndex(i+1) then
for j in range(3,1)
output = output + b64Table[floor(buffer/64^j)%64]
end for
else
for j in range(3,2)
output = output + b64Table[floor(buffer/64^j)%64]
end for
end if
end if
end for
return output
end if
if choice == "decrypt" then
if text.len % 4 == 1 then return null
s_arr = text.values
for i in s_arr.indexes
tmp = b64Table.indexOf(s_arr[i])
if tmp == null then return null
s_arr[i] = tmp
end for
b_arr = []
for i in range(0, s_arr.len-1, 4)
buffer = s_arr[i]*262144
if s_arr.hasIndex(i+1) then
buffer = buffer+s_arr[i+1]*4096
if s_arr.hasIndex(i+2) then
buffer = buffer+s_arr[i+2]*64
if s_arr.hasIndex(i+3) then buffer = buffer+s_arr[i+3]
end if
end if
if s_arr.hasIndex(i+3) then
for j in range(2)
b_arr = b_arr + [floor(buffer/256^j)%256]
end for
else
if s_arr.hasIndex(i+2) then
for j in range(2,1)
b_arr = b_arr + [floor(buffer/256^j)%256]
end for
else
b_arr = b_arr + [floor(buffer/65526)%256]
end if
end if
end for
Blocks = [[]]
b=0
while b_arr.len > 0
if Blocks[b].len == 16 then
Blocks = Blocks + [[]]
b=b+1
end if
Blocks[b] = Blocks[b] + [b_arr.pull]
end while
if Blocks[-1].len != 16 then return null
for i in Blocks.indexes
Blocks[i] = [Blocks[i][0:4], Blocks[i][4:8], Blocks[i][8:12], Blocks[i][12:16]]
end for
NewBlocks = Blocks[0:]
for i in Blocks.indexes
NewBlocks[i] = aesDecrypt(key, Blocks[i])
end for
for column in Blocks[0].indexes
for byte in Blocks[i][column].indexes
NewBlocks[0][column][byte] = bitwise("^", NewBlocks[0][column][byte], IV[column*4+byte])
end for
end for
if Blocks.len > 1 then
for i in range(1, Blocks.len-1)
for column in Blocks[i].indexes
for byte in Blocks[i][column].indexes
NewBlocks[i][column][byte] = bitwise("^", NewBlocks[i][column][byte], Blocks[i-1][column][byte])
end for
end for
end for
end if
Blocks = NewBlocks[0:]
arr = []
for Block in Blocks
for column in Block
for byte in column
arr = arr + [byte]
end for
end for
end for
if arr[-1] > 16 or arr[-1] == 0 then return null
for i in range(arr[-1]-1)
arr.pop
end for
output = ""
for byte in arr
output = output + char(byte)
end for
return output
end if
return null
end function
Encryption_key = "1234567890123456" //Encryption key here max 16 characters
Decode = function(password)
r = AES128("decrypt", Encryption_key, password)
return str(r)
end function