Skip to content
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
13 changes: 8 additions & 5 deletions AES.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

require'Class'
BaseFun_AES = Class("AES");

function BaseFun_AES:ctor()
Expand Down Expand Up @@ -262,17 +263,17 @@ function BaseFun_AES:encrypt(plain , pPos , cipher , cPos)
self:shift_sub_rows(s1);
self:copy_and_key(cipher , cPos , s1 , 1 , self.key_sched , 1 + (self.round * self.N_BLOCK));
end
function BaseFun_AES:decrypt(plain , pPos , cipher , cPos)
function BaseFun_AES:decrypt(cipher , pPos , plain , cPos)
local r , s1 , s2;
s1 = {};
s2 = {};
self:copy_and_key(s1 , 1 , plain , pPos , self.key_sched , 1 + (self.round * self.N_BLOCK));
self:copy_and_key(s1 , 1 , cipher , pPos , self.key_sched , 1 + (self.round * self.N_BLOCK));
self:inv_shift_sub_rows(s1);
for r = self.round - 1 , 1 , -1 do
self:copy_and_key(s2 , 1 , s1 , 1 , self.key_sched , 1 + (r * self.N_BLOCK));
self:inv_mix_sub_columns(s1 , s2);
end
self:copy_and_key(cipher , cPos , s1 , 1 , self.key_sched , 1);
self:copy_and_key(plain , cPos , s1 , 1 , self.key_sched , 1);
end
function BaseFun_AES.GetStringList(strBytes)
local i , List;
Expand All @@ -297,7 +298,7 @@ function BaseFun_AES.GetStringKey(strKey)
if i <= nLen then
List[i] = string.byte(strKey , i);
else
List[i] = 0; -- �����
List[i] = 0; -- 零填充
end
end
return List;
Expand All @@ -315,7 +316,7 @@ function BaseFun_AES:GetBlockList(strData)
if i <= nLen then
List[i] = string.byte(strData , i);
else
List[i] = 0; -- �����
List[i] = 0; -- 零填充
end
end
return List;
Expand Down Expand Up @@ -400,3 +401,5 @@ function BaseFun_AES:cbc_EncryptDecrypt(strData , strKey , strIV , bEncrypt)
return table.concat(txtList);
-- return string.char(table.unpack(reList));
end

return BaseFun_AES
14 changes: 7 additions & 7 deletions Class.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

-- ��Ĭ�ϳ�ʼ������ ctor ��Ϊ���ʼ����ʱʹ�ã��Ҵ���ʱ��Ȼ�ᱻ���ã�ctor�����������ڴ�������ʱ����Ĭ�ϱ���
-- �������ʼ��ʱ������ͨ������ǰ��˫�»���"__"��ʽ���ø�����󲢵������е��Զ����ʼ�����������ݲ���
-- ���� ������ �������б�
-- 类默认初始化函数 ctor 仅为类初始对象时使用,且创建时必然会被调用,ctor函数可用来在创建对象时构建默认变量
-- 在子类初始化时,可以通过类名前加双下划线"__"显式引用父类对象并调用其中的自定义初始化函数来传递参数
-- 参数 :类名 ,父类列表
function Class(className, ...)
if type(className) ~= "string" then
error("class name must string type");
Expand All @@ -11,7 +11,7 @@ function Class(className, ...)
end
function cls:__ctor() -- On Init
self["__" .. className] = cls;
-- �����ʼ��
-- 父类初始化
if cls.__supers ~= nil then
local k,super;
for k,super in pairs(cls.__supers) do
Expand All @@ -20,14 +20,14 @@ function Class(className, ...)
end
end
end
-- ��ʼ��
-- 初始化
cls.ctor(self);
end
local supers = {...};
local k,super;
for k,super in ipairs(supers) do
if type(super) == "table" then
-- ���Ӹ��б�
-- 附加父列表
if super.__isClass and type(super.__className) == "string" then
cls.__supers = cls.__supers or {};
cls.__supers[super.__className] = super;
Expand Down Expand Up @@ -58,7 +58,7 @@ function Class(className, ...)
local instance = obj or {};
setmetatable(instance, {__index = self});
instance.__class = self;
instance:__ctor(); -- ��ʼ������
instance:__ctor(); -- 初始化调用
return instance;
end
return cls;
Expand Down
93 changes: 2 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,94 +1,5 @@
# lua-AES

测试代码:
纯Lua实现,使用Lua 5.3,支持128/192/256bit、EBC/CBC、NonePadding,AES。

AES_obj = AES:new();

local i , buf , buf2 , bComplete;

-- 使用 Crypto++ Library 8.2 的部分用例
AES_Key = {0x2B , 0x7E , 0x15 , 0x16 , 0x28 , 0xAE , 0xD2 , 0xA6 , 0xAB , 0xF7 , 0x15 , 0x88 , 0x09 , 0xCF , 0x4F , 0x3C};
AES_EData = {0x6B , 0xC1 , 0xBE , 0xE2 , 0x2E , 0x40 , 0x9F , 0x96 , 0xE9 , 0x3D , 0x7E , 0x11 , 0x73 , 0x93 , 0x17 , 0x2A , 0xAE , 0x2D , 0x8A , 0x57 , 0x1E , 0x03 , 0xAC , 0x9C , 0x9E , 0xB7 , 0x6F , 0xAC , 0x45 , 0xAF , 0x8E , 0x51 , 0x30 , 0xC8 , 0x1C , 0x46 , 0xA3 , 0x5C , 0xE4 , 0x11 , 0xE5 , 0xFB , 0xC1 , 0x19 , 0x1A , 0x0A , 0x52 , 0xEF , 0xF6 , 0x9F , 0x24 , 0x45 , 0xDF , 0x4F , 0x9B , 0x17 , 0xAD , 0x2B , 0x41 , 0x7B , 0xE6 , 0x6C , 0x37 , 0x10};
AES_DData = {0x3A , 0xD7 , 0x7B , 0xB4 , 0x0D , 0x7A , 0x36 , 0x60 , 0xA8 , 0x9E , 0xCA , 0xF3 , 0x24 , 0x66 , 0xEF , 0x97 , 0xF5 , 0xD3 , 0xD5 , 0x85 , 0x03 , 0xB9 , 0x69 , 0x9D , 0xE7 , 0x85 , 0x89 , 0x5A , 0x96 , 0xFD , 0xBA , 0xAF , 0x43 , 0xB1 , 0xCD , 0x7F , 0x59 , 0x8E , 0xCE , 0x23 , 0x88 , 0x1B , 0x00 , 0xE3 , 0xED , 0x03 , 0x06 , 0x88 , 0x7B , 0x0C , 0x78 , 0x5E , 0x27 , 0xE8 , 0xAD , 0x3F , 0x82 , 0x23 , 0x20 , 0x71 , 0x04 , 0x72 , 0x5D , 0xD4};

Msg("Test original:");
AES_obj:set_key(AES_Key , 16);
buf = {};
for i = 1 , 64 , 16 do
AES_obj:encrypt(AES_EData , i , buf , i);
end
-- Msg(string.format("dl:%d bl:%d" , #AES_EData , #buf));
bComplete = true;
for i = 1 , 64 do
if AES_DData[i] ~= buf[i] then
bComplete = false;
Msg(string.format("encrypt err: %d , %02x , %02x" , i , AES_DData[i] , buf[i]));
break;
end
end
if bComplete then
Msg("encrypt complet");
else
Msg("encrypt Data:" .. sys.Convert(buf , T_Convert.HexText));
end
buf2 = {};
for i = 1 , 64 , 16 do
AES_obj:decrypt(AES_DData , i , buf2 , i);
end
bComplete = true;
for i = 1 , 64 do
if AES_EData[i] ~= buf2[i] then
bComplete = false;
Msg(string.format("decrypt err: %d , %02x , %02x" , i , AES_EData[i] , buf2[i]));
break;
end
end
if bComplete then
Msg("decrypt complet");
else
Msg("decrypt Data:" .. sys.Convert(buf2 , T_Convert.HexText));
end

Msg("Test ecb:");
buf = AES_obj:ecb_EncryptDecrypt(string.char(table.unpack(AES_EData)) , string.char(table.unpack(AES_Key)) , true);
if buf == string.char(table.unpack(AES_DData)) then
Msg("encrypt complet");
else
Msg("encrypt Data:" .. sys.Convert(buf , T_Convert.HexText));
end
buf2 = AES_obj:ecb_EncryptDecrypt(buf , string.char(table.unpack(AES_Key)) , false);
if buf2 == string.char(table.unpack(AES_EData)) then
Msg("decrypt complet");
else
Msg("decrypt Data:" .. sys.Convert(buf2 , T_Convert.HexText));
end

-- 默认 IV:1
AES_IV = {0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00};
Msg("Test cbc:");
buf = AES_obj:cbc_EncryptDecrypt(string.char(table.unpack(AES_EData)) , string.char(table.unpack(AES_Key)) , string.char(table.unpack(AES_IV)) , true);
buf2 = AES_obj:cbc_EncryptDecrypt(buf , string.char(table.unpack(AES_Key)) , string.char(table.unpack(AES_IV)) , false);
if buf2 == string.char(table.unpack(AES_EData)) then
Msg("complet");
else
Msg("Data:" .. sys.Convert(buf2 , T_Convert.HexText));
end

AES_IV = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F};
AES_Key = {0x2B , 0x7E , 0x15 , 0x16 , 0x28 , 0xAE , 0xD2 , 0xA6 , 0xAB , 0xF7 , 0x15 , 0x88 , 0x09 , 0xCF , 0x4F , 0x3C};
AES_EData = {0x6B , 0xC1 , 0xBE , 0xE2 , 0x2E , 0x40 , 0x9F , 0x96 , 0xE9 , 0x3D , 0x7E , 0x11 , 0x73 , 0x93 , 0x17 , 0x2A , 0xAE , 0x2D , 0x8A , 0x57 , 0x1E , 0x03 , 0xAC , 0x9C , 0x9E , 0xB7 , 0x6F , 0xAC , 0x45 , 0xAF , 0x8E , 0x51 , 0x30 , 0xC8 , 0x1C , 0x46 , 0xA3 , 0x5C , 0xE4 , 0x11 , 0xE5 , 0xFB , 0xC1 , 0x19 , 0x1A , 0x0A , 0x52 , 0xEF , 0xF6 , 0x9F , 0x24 , 0x45 , 0xDF , 0x4F , 0x9B , 0x17 , 0xAD , 0x2B , 0x41 , 0x7B , 0xE6 , 0x6C , 0x37 , 0x10};
AES_DData = {0x76 , 0x49 , 0xAB , 0xAC , 0x81 , 0x19 , 0xB2 , 0x46 , 0xCE , 0xE9 , 0x8E , 0x9B , 0x12 , 0xE9 , 0x19 , 0x7D , 0x50 , 0x86 , 0xCB , 0x9B , 0x50 , 0x72 , 0x19 , 0xEE , 0x95 , 0xDB , 0x11 , 0x3A , 0x91 , 0x76 , 0x78 , 0xB2 , 0x73 , 0xBE , 0xD6 , 0xB8 , 0xE3 , 0xC1 , 0x74 , 0x3B , 0x71 , 0x16 , 0xE6 , 0x9E , 0x22 , 0x22 , 0x95 , 0x16 , 0x3F , 0xF1 , 0xCA , 0xA1 , 0x68 , 0x1F , 0xAC , 0x09 , 0x12 , 0x0E , 0xCA , 0x30 , 0x75 , 0x86 , 0xE1 , 0xA7};
Msg("Test cbc2:");
buf = AES_obj:cbc_EncryptDecrypt(string.char(table.unpack(AES_EData)) , string.char(table.unpack(AES_Key)) , string.char(table.unpack(AES_IV)) , true);
if buf == string.char(table.unpack(AES_DData)) then
Msg("encrypt complet");
else
Msg("encrypt Data:" .. sys.Convert(buf , T_Convert.HexText));
end
buf2 = AES_obj:cbc_EncryptDecrypt(buf , string.char(table.unpack(AES_Key)) , string.char(table.unpack(AES_IV)) , false);
if buf2 == string.char(table.unpack(AES_EData)) then
Msg("decrypt complet");
else
Msg("decrypt Data:" .. sys.Convert(buf2 , T_Convert.HexText));
end

使用详见`test.lua`。
72 changes: 72 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local AES=require'AES'
local util=require'util'

local aes = AES:new()

local i , cipher_ByteList , plain_ByteList;

-- 使用 Crypto++ Library 8.2 的部分用例
local AES_Key = {0x2B , 0x7E , 0x15 , 0x16 , 0x28 , 0xAE , 0xD2 , 0xA6 , 0xAB , 0xF7 , 0x15 , 0x88 , 0x09 , 0xCF , 0x4F , 0x3C};
local Plain_ByteList = {0x6B , 0xC1 , 0xBE , 0xE2 , 0x2E , 0x40 , 0x9F , 0x96 , 0xE9 , 0x3D , 0x7E , 0x11 , 0x73 , 0x93 , 0x17 , 0x2A , 0xAE , 0x2D , 0x8A , 0x57 , 0x1E , 0x03 , 0xAC , 0x9C , 0x9E , 0xB7 , 0x6F , 0xAC , 0x45 , 0xAF , 0x8E , 0x51 , 0x30 , 0xC8 , 0x1C , 0x46 , 0xA3 , 0x5C , 0xE4 , 0x11 , 0xE5 , 0xFB , 0xC1 , 0x19 , 0x1A , 0x0A , 0x52 , 0xEF , 0xF6 , 0x9F , 0x24 , 0x45 , 0xDF , 0x4F , 0x9B , 0x17 , 0xAD , 0x2B , 0x41 , 0x7B , 0xE6 , 0x6C , 0x37 , 0x10};
local Cipher_ByteList = {0x3A , 0xD7 , 0x7B , 0xB4 , 0x0D , 0x7A , 0x36 , 0x60 , 0xA8 , 0x9E , 0xCA , 0xF3 , 0x24 , 0x66 , 0xEF , 0x97 , 0xF5 , 0xD3 , 0xD5 , 0x85 , 0x03 , 0xB9 , 0x69 , 0x9D , 0xE7 , 0x85 , 0x89 , 0x5A , 0x96 , 0xFD , 0xBA , 0xAF , 0x43 , 0xB1 , 0xCD , 0x7F , 0x59 , 0x8E , 0xCE , 0x23 , 0x88 , 0x1B , 0x00 , 0xE3 , 0xED , 0x03 , 0x06 , 0x88 , 0x7B , 0x0C , 0x78 , 0x5E , 0x27 , 0xE8 , 0xAD , 0x3F , 0x82 , 0x23 , 0x20 , 0x71 , 0x04 , 0x72 , 0x5D , 0xD4};

print("Basic Test:")
aes:set_key(AES_Key , 16)
cipher_ByteList = {};
for i = 1 , 64 , 16 do
aes:encrypt(Plain_ByteList , i , cipher_ByteList , i)
end
assert(#Plain_ByteList == #cipher_ByteList,
string.format("dl:%d bl:%d" , #Plain_ByteList , #cipher_ByteList))

for i = 1 , 64 do
assert(Cipher_ByteList[i] == cipher_ByteList[i],
string.format("encrypt err: %d , %02x , %02x\n cipher_ByteList: %s" , i , Cipher_ByteList[i] , cipher_ByteList[i] , util.toHexString(cipher_ByteList)))
end
print("encrypt complet")
----------------

plain_ByteList = {};
for i = 1 , 64 , 16 do
aes:decrypt(Cipher_ByteList , i , plain_ByteList , i)
end
for i = 1 , 64 do
assert(Plain_ByteList[i] == plain_ByteList[i],
string.format("decrypt err: %d , %02x , %02x\n plain_ByteList: %s" , i , Plain_ByteList[i] , plain_ByteList[i] , util.toHexString(plain_ByteList)))
end
print("decrypt complet")
----------------
print''

print"Advance Test:"
local ciyher_Str,plain_Str
print("Test ecb:")
ciyher_Str = aes:ecb_EncryptDecrypt(util.byteListToString(Plain_ByteList) , util.byteListToString(AES_Key) , true)
assert(ciyher_Str == util.byteListToString(Cipher_ByteList), string.format('ciyher_str in byte list: %s', util.toHexString(ciyher_Str)))
print("encrypt complet")

plain_Str = aes:ecb_EncryptDecrypt(ciyher_Str , util.byteListToString(AES_Key) , false)
assert(plain_Str == util.byteListToString(Plain_ByteList), string.format('plain_str in byte list: %s', util.toHexString(plain_Str)))
print("decrypt complet")

local AES_IV = {0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00};
print("Test cbc:")
ciyher_Str = aes:cbc_EncryptDecrypt(util.byteListToString(Plain_ByteList) , util.byteListToString(AES_Key) , util.byteListToString(AES_IV) , true)
plain_Str = aes:cbc_EncryptDecrypt(ciyher_Str , util.byteListToString(AES_Key) , util.byteListToString(AES_IV) , false)
assert(plain_Str == util.byteListToString(Plain_ByteList), string.format('plain_str in byte list: %s', util.toHexString(plain_Str)))
print'encrypt & decrypt complet'


print("Test cbc2:")
local AES_IV = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F};
local Cipher_ByteList = {0x76 , 0x49 , 0xAB , 0xAC , 0x81 , 0x19 , 0xB2 , 0x46 , 0xCE , 0xE9 , 0x8E , 0x9B , 0x12 , 0xE9 , 0x19 , 0x7D , 0x50 , 0x86 , 0xCB , 0x9B , 0x50 , 0x72 , 0x19 , 0xEE , 0x95 , 0xDB , 0x11 , 0x3A , 0x91 , 0x76 , 0x78 , 0xB2 , 0x73 , 0xBE , 0xD6 , 0xB8 , 0xE3 , 0xC1 , 0x74 , 0x3B , 0x71 , 0x16 , 0xE6 , 0x9E , 0x22 , 0x22 , 0x95 , 0x16 , 0x3F , 0xF1 , 0xCA , 0xA1 , 0x68 , 0x1F , 0xAC , 0x09 , 0x12 , 0x0E , 0xCA , 0x30 , 0x75 , 0x86 , 0xE1 , 0xA7};

ciyher_Str = aes:cbc_EncryptDecrypt(util.byteListToString(Plain_ByteList) , util.byteListToString(AES_Key) , util.byteListToString(AES_IV) , true)
assert(ciyher_Str == util.byteListToString(Cipher_ByteList), string.format('ciyher_str in byte list: %s', util.toHexString(ciyher_Str)))
plain_Str = aes:cbc_EncryptDecrypt(ciyher_Str , util.byteListToString(AES_Key) , util.byteListToString(AES_IV) , false)
assert(plain_Str == util.byteListToString(Plain_ByteList), string.format('plain_str in byte list: %s', util.toHexString(plain_Str)))
print'encrypt & decrypt complet'
----------------
print''

print'finished'
33 changes: 33 additions & 0 deletions util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local public={}
local private={}

function private.bytesToHex(bytes)
local hexBytes = "";

for i,byte in ipairs(bytes) do
hexBytes = hexBytes .. string.format("%02x ", byte);
end

return hexBytes;
end

function public.toHexString(data)
local type = type(data);
if (type == "number") then
return string.format("%08x",data);
elseif (type == "table") then
return private.bytesToHex(data);
elseif (type == "string") then
local bytes = {string.byte(data, 1, #data)};

return private.bytesToHex(bytes);
else
return data;
end
end

function public.byteListToString(byteList)
return string.char(table.unpack(byteList))
end

return public