Repository navigation
Expand file tree
/
Copy pathciphermode.lua
More file actions
147 lines (111 loc) · 3.52 KB
/
Copy pathciphermode.lua
File metadata and controls
147 lines (111 loc) · 3.52 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
local aes = require("aeslua.aes");
local util = require("aeslua.util");
local buffer = require("aeslua.buffer");
local public = {};
aeslua.ciphermode = public;
--
-- Encrypt strings
-- key - byte array with key
-- string - string to encrypt
-- modefunction - function for cipher mode to use
--
function public.encryptString(key, data, modeFunction)
local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
local keySched = aes.expandEncryptionKey(key);
local encryptedData = buffer.new();
for i = 1, #data/16 do
local offset = (i-1)*16 + 1;
local byteData = {string.byte(data,offset,offset +15)};
modeFunction(keySched, byteData, iv);
buffer.addString(encryptedData, string.char(unpack(byteData)));
end
return buffer.toString(encryptedData);
end
--
-- the following 4 functions can be used as
-- modefunction for encryptString
--
-- Electronic code book mode encrypt function
function public.encryptECB(keySched, byteData, iv)
aes.encrypt(keySched, byteData, 1, byteData, 1);
end
-- Cipher block chaining mode encrypt function
function public.encryptCBC(keySched, byteData, iv)
util.xorIV(byteData, iv);
aes.encrypt(keySched, byteData, 1, byteData, 1);
for j = 1,16 do
iv[j] = byteData[j];
end
end
-- Output feedback mode encrypt function
function public.encryptOFB(keySched, byteData, iv)
aes.encrypt(keySched, iv, 1, iv, 1);
util.xorIV(byteData, iv);
end
-- Cipher feedback mode encrypt function
function public.encryptCFB(keySched, byteData, iv)
aes.encrypt(keySched, iv, 1, iv, 1);
util.xorIV(byteData, iv);
for j = 1,16 do
iv[j] = byteData[j];
end
end
--
-- Decrypt strings
-- key - byte array with key
-- string - string to decrypt
-- modefunction - function for cipher mode to use
--
function public.decryptString(key, data, modeFunction)
local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
local keySched;
if (modeFunction == public.decryptOFB or modeFunction == public.decryptCFB) then
keySched = aes.expandEncryptionKey(key);
else
keySched = aes.expandDecryptionKey(key);
end
local decryptedData = buffer.new();
for i = 1, #data/16 do
local offset = (i-1)*16 + 1;
local byteData = {string.byte(data,offset,offset +15)};
iv = modeFunction(keySched, byteData, iv);
buffer.addString(decryptedData, string.char(unpack(byteData)));
end
return buffer.toString(decryptedData);
end
--
-- the following 4 functions can be used as
-- modefunction for decryptString
--
-- Electronic code book mode decrypt function
function public.decryptECB(keySched, byteData, iv)
aes.decrypt(keySched, byteData, 1, byteData, 1);
return iv;
end
-- Cipher block chaining mode decrypt function
function public.decryptCBC(keySched, byteData, iv)
local nextIV = {};
for j = 1,16 do
nextIV[j] = byteData[j];
end
aes.decrypt(keySched, byteData, 1, byteData, 1);
util.xorIV(byteData, iv);
return nextIV;
end
-- Output feedback mode decrypt function
function public.decryptOFB(keySched, byteData, iv)
aes.encrypt(keySched, iv, 1, iv, 1);
util.xorIV(byteData, iv);
return iv;
end
-- Cipher feedback mode decrypt function
function public.decryptCFB(keySched, byteData, iv)
local nextIV = {};
for j = 1,16 do
nextIV[j] = byteData[j];
end
aes.encrypt(keySched, iv, 1, iv, 1);
util.xorIV(byteData, iv);
return nextIV;
end
return public;