Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify iv, fixes #4 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/aeslua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ end
-- 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
-- iv - initialization vector to use
--
-- mode and keyLength must be the same for encryption and decryption.
--
function public.encrypt(password, data, keyLength, mode)
function public.encrypt(password, data, keyLength, mode, iv)
assert(password ~= nil, "Empty password.");
assert(password ~= nil, "Empty data.");

Expand All @@ -63,13 +64,13 @@ function public.encrypt(password, data, keyLength, mode)
local paddedData = util.padByteString(data);

if (mode == public.ECBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB);
return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB, iv);
elseif (mode == public.CBCMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC);
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC, iv);
elseif (mode == public.OFBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB);
return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB, iv);
elseif (mode == public.CFBMODE) then
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB);
return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB, iv);
else
return nil;
end
Expand All @@ -84,24 +85,25 @@ end
-- data - string to encrypt
-- keyLength - length of aes key: 128(default), 192 or 256 Bit
-- mode - mode of decryption: ecb, cbc(default), ofb, cfb
-- iv - initialization vector to use
--
-- mode and keyLength must be the same for encryption and decryption.
--
function public.decrypt(password, data, keyLength, mode)
function public.decrypt(password, data, keyLength, mode, iv)
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);
plain = ciphermode.decryptString(key, data, ciphermode.decryptECB, iv);
elseif (mode == public.CBCMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC);
plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC, iv);
elseif (mode == public.OFBMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB);
plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB, iv);
elseif (mode == public.CFBMODE) then
plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB);
plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB, iv);
end

result = util.unpadByteString(plain);
Expand Down
6 changes: 4 additions & 2 deletions src/aeslua/ciphermode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ aeslua.ciphermode = public;
-- key - byte array with key
-- string - string to encrypt
-- modefunction - function for cipher mode to use
-- iv - initialization vector to use
--
function public.encryptString(key, data, modeFunction)
function public.encryptString(key, data, modeFunction, iv)
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();
Expand Down Expand Up @@ -71,8 +72,9 @@ end
-- key - byte array with key
-- string - string to decrypt
-- modefunction - function for cipher mode to use
-- iv - initialization vector to use
--
function public.decryptString(key, data, modeFunction)
function public.decryptString(key, data, modeFunction, iv)
local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

local keySched;
Expand Down