File size: 3,461 Bytes
d7c5875 | 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 | local private = {};
local public = {};
aeslua = public;
local ciphermode = require("aeslua.ciphermode");
local util = require("aeslua.util");
--
-- Simple API for encrypting strings.
--
public.AES128 = 16;
public.AES192 = 24;
public.AES256 = 32;
public.ECBMODE = 1;
public.CBCMODE = 2;
public.OFBMODE = 3;
public.CFBMODE = 4;
function private.pwToKey(password, keyLength)
local padLength = keyLength;
if (keyLength == public.AES192) then
padLength = 32;
end
if (padLength > #password) then
local postfix = "";
for i = 1,padLength - #password do
postfix = postfix .. string.char(0);
end
password = password .. postfix;
else
password = string.sub(password, 1, padLength);
end
local pwBytes = {string.byte(password,1,#password)};
password = ciphermode.encryptString(pwBytes, password, ciphermode.encryptCBC);
password = string.sub(password, 1, keyLength);
return {string.byte(password,1,#password)};
end
--
-- Encrypts string data with password password.
-- password - the encryption key is generated from this string
-- data - string to encrypt (must not be too large)
-- keyLength - length of aes key: 128(default), 192 or 256 Bit
-- mode - mode of encryption: ecb, cbc(default), ofb, cfb
--
-- mode and keyLength must be the same for encryption and decryption.
--
function public.encrypt(password, data, keyLength, mode)
assert(password ~= nil, "Empty password.");
assert(password ~= nil, "Empty data.");
local mode = mode or public.CBCMODE;
local keyLength = keyLength or public.AES128;
local key = private.pwToKey(password, keyLength);
local paddedData = util.padByteString(data);
if (mode == public.ECBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB);
elseif (mode == public.CBCMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC);
elseif (mode == public.OFBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB);
elseif (mode == public.CFBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB);
else
return nil;
end
end
--
-- Decrypts string data with password password.
-- password - the decryption key is generated from this string
-- data - string to encrypt
-- keyLength - length of aes key: 128(default), 192 or 256 Bit
-- mode - mode of decryption: ecb, cbc(default), ofb, cfb
--
-- mode and keyLength must be the same for encryption and decryption.
--
function public.decrypt(password, data, keyLength, mode)
local mode = mode or public.CBCMODE;
local keyLength = keyLength or public.AES128;
local key = private.pwToKey(password, keyLength);
local plain;
if (mode == public.ECBMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptECB);
elseif (mode == public.CBCMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC);
elseif (mode == public.OFBMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB);
elseif (mode == public.CFBMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB);
end
result = util.unpadByteString(plain);
if (result == nil) then
return nil;
end
return result;
end
|