· 6 years ago · Sep 15, 2019, 07:28 PM
1-- Simple API for encrypting strings.
2--
3AES128 = 16
4AES192 = 24
5AES256 = 32
6
7ECBMODE = 1
8CBCMODE = 2
9OFBMODE = 3
10CFBMODE = 4
11CTRMODE = 4
12
13local aeslua = {}
14
15local function pwToKey(password, keyLength, iv)
16 local padLength = keyLength
17 if (keyLength == AES192) then
18 padLength = 32
19 end
20
21 if (padLength > #password) then
22 local postfix = ""
23 for i = 1,padLength - #password do
24 postfix = postfix .. string.char(0)
25 end
26 password = password .. postfix
27 else
28 password = string.sub(password, 1, padLength)
29 end
30
31 local pwBytes = {string.byte(password,1,#password)}
32 password = ciphermode.encryptString(pwBytes, password, ciphermode.encryptCBC, iv)
33
34 password = string.sub(password, 1, keyLength)
35
36 return {string.byte(password,1,#password)}
37end
38
39--
40-- Encrypts string data with password password.
41-- password - the encryption key is generated from this string
42-- data - string to encrypt (must not be too large)
43-- keyLength - length of aes key: 128(default), 192 or 256 Bit
44-- mode - mode of encryption: ecb, cbc(default), ofb, cfb
45--
46-- mode and keyLength must be the same for encryption and decryption.
47--
48function aeslua.encrypt(password, data, keyLength, mode, iv)
49 assert(password ~= nil, "Empty password.")
50 assert(password ~= nil, "Empty data.")
51
52 local mode = mode or CBCMODE
53 local keyLength = keyLength or AES128
54
55 local key = pwToKey(password, keyLength, iv)
56
57 local paddedData = util.padByteString(data)
58
59 if mode == ECBMODE then
60 return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB, iv)
61 elseif mode == CBCMODE then
62 return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC, iv)
63 elseif mode == OFBMODE then
64 return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB, iv)
65 elseif mode == CFBMODE then
66 return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB, iv)
67 elseif mode == CTRMODE then
68 return ciphermode.encryptString(key, paddedData, ciphermode.encryptCTR, iv)
69 else
70 error("Unknown mode", 2)
71 end
72end
73
74
75
76
77--
78-- Decrypts string data with password password.
79-- password - the decryption key is generated from this string
80-- data - string to encrypt
81-- keyLength - length of aes key: 128(default), 192 or 256 Bit
82-- mode - mode of decryption: ecb, cbc(default), ofb, cfb
83--
84-- mode and keyLength must be the same for encryption and decryption.
85--
86function aeslua.decrypt(password, data, keyLength, mode, iv)
87 local mode = mode or CBCMODE
88 local keyLength = keyLength or AES128
89
90 local key = pwToKey(password, keyLength, iv)
91
92 local plain
93 if mode == ECBMODE then
94 plain = ciphermode.decryptString(key, data, ciphermode.decryptECB, iv)
95 elseif mode == CBCMODE then
96 plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC, iv)
97 elseif mode == OFBMODE then
98 plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB, iv)
99 elseif mode == CFBMODE then
100 plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB, iv)
101 elseif mode == CTRMODE then
102 plain = ciphermode.decryptString(key, data, ciphermode.decryptCTR, iv)
103 else
104 error("Unknown mode", 2)
105 end
106
107 result = util.unpadByteString(plain)
108
109 if (result == nil) then
110 return nil
111 end
112
113 return result
114end
115
116return aeslua