From 78dd8cd4c96315a6588bf05a3fc81ed6d4b416f5 Mon Sep 17 00:00:00 2001 From: RobertL Date: Tue, 10 May 2022 18:25:12 +0800 Subject: [PATCH] integration *+test ready to run *document --- AES.lua | 13 +++++--- Class.lua | 14 ++++----- README.md | 93 ++----------------------------------------------------- test.lua | 72 ++++++++++++++++++++++++++++++++++++++++++ util.lua | 33 ++++++++++++++++++++ 5 files changed, 122 insertions(+), 103 deletions(-) create mode 100644 test.lua create mode 100644 util.lua diff --git a/AES.lua b/AES.lua index 802f9fa..386ad55 100644 --- a/AES.lua +++ b/AES.lua @@ -1,4 +1,5 @@ +require'Class' BaseFun_AES = Class("AES"); function BaseFun_AES:ctor() @@ -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; @@ -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; @@ -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; @@ -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 \ No newline at end of file diff --git a/Class.lua b/Class.lua index 5f009a1..0cc2849 100644 --- a/Class.lua +++ b/Class.lua @@ -1,7 +1,7 @@ --- ÀàĬÈϳõʼ»¯º¯Êý ctor ½öΪÀà³õʼ¶ÔÏóʱʹÓã¬ÇÒ´´½¨Ê±±ØÈ»»á±»µ÷Óã¬ctorº¯Êý¿ÉÓÃÀ´ÔÚ´´½¨¶ÔÏóʱ¹¹½¨Ä¬ÈϱäÁ¿ --- ÔÚ×ÓÀà³õʼ»¯Ê±£¬¿ÉÒÔͨ¹ýÀàÃûǰ¼Ó˫ϻ®Ïß"__"ÏÔʽÒýÓø¸Àà¶ÔÏó²¢µ÷ÓÃÆäÖеÄ×Ô¶¨Òå³õʼ»¯º¯ÊýÀ´´«µÝ²ÎÊý --- ²ÎÊý £ºÀàÃû £¬¸¸ÀàÁбí +-- 类默认åˆå§‹åŒ–函数 ctor 仅为类åˆå§‹å¯¹è±¡æ—¶ä½¿ç”¨ï¼Œä¸”创建时必然会被调用,ctor函数å¯ç”¨æ¥åœ¨åˆ›å»ºå¯¹è±¡æ—¶æž„建默认å˜é‡ +-- 在å­ç±»åˆå§‹åŒ–时,å¯ä»¥é€šè¿‡ç±»åå‰åŠ åŒä¸‹åˆ’线"__"显å¼å¼•用父类对象并调用其中的自定义åˆå§‹åŒ–函数æ¥ä¼ é€’傿•° +-- 傿•° :类å ,父类列表 function Class(className, ...) if type(className) ~= "string" then error("class name must string type"); @@ -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 @@ -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; @@ -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; diff --git a/README.md b/README.md index 9ed2ce4..f55168f 100644 --- a/README.md +++ b/README.md @@ -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`。 diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..7487d27 --- /dev/null +++ b/test.lua @@ -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' \ No newline at end of file diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..b07d6a9 --- /dev/null +++ b/util.lua @@ -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 \ No newline at end of file