非原创。搬运,解读,应用。 增加PKCS7 Padding。
纯Lua实现,测试Lua 5.3,支持EBC/CBC、Zero/PKCS7 Padding,AES-128 bit。
- clone至文件夹AES
如:
ZeroBraneStudio\lualibs\AES\。 - 代码中
require 'AES'。 - 需要包解析路径包含
?\?.lua。
BaseFun_AES = AES_Class,后续简称AES。
需实例化后使用,实例化方法为AES:new(),后续实例简称aes。
AES:set_key(key , keylen)
内部使用。
key:密钥。字节数组。keylen:密钥长度——16 / 24 / 32 (128/192/256bits)。
参见GetStringKey。
内部使用。
aes:encrypt(plain , pPos , cipher , cPos)
内部使用。
aes:decrypt(cipher , pPos , plain , cPos)
strData_output = aes:ecb_EncryptDecrypt(strData , strKey , bEncrypt, padding)
strData:字符串。bEncrypt:加密/解密。布尔。true- Encrypt,false- Decrypt。strData_output:字符串。padding:false/nil/"ZERO""PKCS7"
| Encrypt | Decrypt | |
|---|---|---|
bEncrypt |
true |
false |
strData |
plain | cipher |
strData_output |
cipher | plain |
strData_output = aes:cbc_EncryptDecrypt(strData , strKey , strIV , bEncrypt, padding)
strData:字符串。bEncrypt:加密/解密。布尔。true- Encrypt,false- Decrypt。strIV:初始向量。字符串。strData_output:字符串。padding:false/nil/"ZERO""PKCS7"
| Encrypt | Decrypt | |
|---|---|---|
bEncrypt |
true |
false |
strData |
plain | cipher |
strData_output |
cipher | plain |
byteList_key = AES.GetStringKey(strKey)
内部使用。
包含“零填充”,填充至16/24/32位。
填充后传递给set_key。
byteList_data= = GetBlockList(strData, strPadding)
strPadding:
false/nil/"ZERO""PKCS7"
内部使用。
略。
Plain_ByteList:常量,标准值。明文。字节数组。 原名AES_EData(Encrypt Data?)。Cipher_ByteList:常量,标准值。密文。字节数组。 原名AES_DData(Decrypt Data?)。plain_ByteList:返回的结果。明文。字节数组。cipher_ByteList:返回的结果。密文。字节数组。plain_Str:返回的结果。密文。字符串。ciyher_Str:返回的结果。密文。字符串。
byteList_HexString = toHexString(data)
data:支持32位number、number intable、string。byteList_HexString:形如'01 02 .. FF'。
自aeslua/util.lua at master · bighil/aeslua (github.com)。
str = byteListToString(byteList)
byteList:字节数组。
包含可用功能:
padByteString/unpadByteString:使用到随机数,貌似为ISO10126。getByte: get byte at position indexputByte: put number into int at position indexbytesToInts: convert byte array to int arrayintsToBytes: convert int array to byte array
字节数组 形如{0x01,0x02,..}。
bighil/aeslua: Implementation of aes in nearly pure lua (bitlib is required) (github.com)
支持AES128 / AES192 / AES256、ECB/CBC/OFB/CFB,Padding with ISO10126? 有多个测试例。
问题:
- 依赖
bitlib。 分支gdyr/aeslua53: Implementation of AES in pure Lua 5.3 (based on bighil/aeslua) (github.com)使用Lua 5.3位操作。 - 不支持iv传入。 分支TobleMiner/aeslua at feature-iv (github.com)加入支持。
- 貌似数据长度有问题,结果与外部工具测试不一致、不兼容(解密报错)。
AES在线解密 AES在线加密 Aes online hex 十六进制密钥 - The X 在线工具 (the-x.cn)