通用加解密函数(crypto)
一、简介
加解密算法是保证数据安全的基础技术,无论是在数据传输、存储,还是用户身份验证中,都起着至关重要的作用。随着互联网的发展和信息安全威胁的增加,了解并掌握常用的加解密算法已经成为开发者和安全从业者的必修课。
【三种分类】
- 对称加密:密钥只有一个,加密、解密都是这个密码,加解密速度快,典型的对称加密有 DES、AES、RC4 等
- 非对称加密:密钥成对出现,分别为公钥和私钥,从公钥无法推知私钥,反之,从私钥也无法推知公钥,加密和解密使用不同的密钥,公钥加密需要私钥解密,反之,私钥加密需要公钥解密。非对称加密速度较慢,典型的非对称算法有:RSA,DSA,DSS.
- Hash 算法,这是一种不可逆的算法,它常用于验证数据的完整性
二、演示功能概述
本教程教你如何用 Air724 开发板,来测试有关加解密的各种 API 的功能,并通过日志观察实验结果。
三、准备硬件环境
3.1 开发板准备
使用 EVB_Air724 开发板,如下图所示:
淘宝购买链接:Air724UG-NFM 开发板淘宝购买链接 ;
此开发板的详细使用说明参考:Air724UG 产品手册 中的《EVB_Air724UG_AXX 开发板使用说明》,写这篇文章时最新版本的使用说明为:《EVB_Air724UG_A14 开发板使用说明》;开发板使用过程中遇到任何问题,可以直接参考这份使用说明文档。
api:https://doc.openluat.com/wiki/21?wiki_page_id=2068
3.2 数据通信线
USB 数据线一根(micro USB)。
3.3 PC 电脑
WIN7 以及以上版本的 WINDOWS 系统。
3.4 SIM 卡
中国大陆环境下,可以上网的 SIM 卡。一般来说,使用移动,电信,联通的物联网卡或者手机卡都行。
3.5 组装硬件环境
USB 数据线插入 USB 口,另一端与电脑相连,拨码开关全部拨到 ON ,串口切换开关选择 UART1,USB 供电的 4V 对应开关拨至 ON 档,SIM 卡放到 SIM 卡槽中锁紧,如下图所示。
四、准备软件环境
4.1 下载调试工具
使用说明参考:Luatools 下载和详细使用
4.2 源码及固件
1.底层 core 下载
下载底层固件,并解压
链接:https://docs.openluat.com/air724ug/luatos/firmware/
如下图所示,红框的是我们要使用到的。
2.本教程使用的 demo:
https://gitee.com/openLuat/LuatOS-Air724UG/tree/master/script_LuaTask/demo/crypto
4.3 下载固件和脚本到开发板中
打开 Luatools,开发板上电开机,如开机成功 Luatools 会打印如下信息。
点击项目管理测试选项。
进入管理界面,如下图所示。
- 点击选择文件,选择底层固件,我的文件放在 D:\luatOS\Air724 路径中。
- 点击增加脚本或资源文件,选择 之前下载的程序源码,如下图所示。
- 点击下载底层和脚本,下载完成如下图所示。
五、代码示例介绍
5.1 API 说明
5.1.1 crypto.md5(str)
计算 md5 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 md5 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 md5
log.info("md5", crypto.md5("abc"))
5.1.2 crypto.hmac_md5(str, key)
计算 hmac_md5 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
string | 密钥 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 hmac_md5 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 hmac_md5
log.info("hmac_md5", crypto.hmac_md5("abc", "1234567890"))
5.1.3 crypto.sha1(str)
计算 sha1 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 sha1 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 sha1
log.info("sha1", crypto.sha1("abc"))
5.1.4 crypto.hmac_sha1(str, key)
计算 hmac_sha1 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
string | 密钥 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 hmac_sha1 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 hmac_sha1
log.info("hmac_sha1", crypto.hmac_sha1("abc", "1234567890"))
5.1.5 crypto.sha256(str)
计算 sha256 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 sha256 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 sha256
log.info("sha256", crypto.sha256("abc"))
5.1.6 crypto.hmac_sha256(str, key)
计算 hmac_sha256 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
string | 密钥 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 hmac_sha256 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 hmac_sha256
log.info("hmac_sha256", crypto.hmac_sha256("abc", "1234567890"))
5.1.7 crypto.sha512(str)
计算 sha512 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 sha512 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 sha512
log.info("sha512", crypto.sha512("abc"))
5.1.8 crypto.hmac_sha512(str, key)
计算 hmac_sha512 值
参数
传入值类型 | 解释 |
---|---|
string | 需要计算的字符串 |
string | 密钥 |
返回值
返回值类型 | 解释 |
---|---|
string | 计算得出的 hmac_sha512 值的 hex 字符串 |
例子
-- 计算字符串"abc"的 hmac_sha512
log.info("hmac_sha512", crypto.hmac_sha512("abc", "1234567890"))
5.1.9 crypto.base64_encode(data)
将数据进行 base64 编码
参数
传入值类型 | 解释 |
---|---|
string | 待编码的数据 |
返回值
返回值类型 | 解释 |
---|---|
string | 编码后的数据 |
例子
-- 本函数与 string.toBase64 是同一个
local data = "123"
local bdata = crypto.base64_encode(data)
log.info("base64", "encode", data, bdata)
data = crypto.base64_decode(data)
log.info("base64", "decode", data, bdata)
5.1.10 crypto.base64_decode(data)
将数据进行 base64 解码
参数
传入值类型 | 解释 |
---|---|
string | 待解码的数据 |
返回值
返回值类型 | 解释 |
---|---|
string | 解码后的数据 |
例子
-- 本函数与 string.fromBase64 是同一个
local data = "123"
local bdata = crypto.base64_encode(data)
log.info("base64", "encode", data, bdata)
data = crypto.base64_decode(data)
log.info("base64", "decode", data, bdata)
5.1.11 crypto.cipher_list()
获取当前固件支持的 cipher 列表
参数
无
返回值
返回值类型 | 解释 |
---|---|
table | 本固件支持的 cipher 列表,字符串数组 |
例子
-- 本 API 于 2022.07.27 添加
local ciphers = crypto.cipher_list()
if ciphers then
log.info("crypto", "ciphers list", json.encode(ciphers))
end
5.1.12 crypto.cipher_suites()
获取当前固件支持的 cipher suites 列表
参数
无
返回值
返回值类型 | 解释 |
---|---|
table | 本固件支持的 cipher suites 列表,字符串数组 |
例子
-- 本 API 于 2022.11.16 添加
local suites = crypto.cipher_suites()
if suites then
log.info("crypto", "ciphers suites", json.encode(suites))
end
5.1.13 crypto.cipher_encrypt(type, padding, str, key, iv)
对称加密
参数
传入值类型 | 解释 |
---|---|
string | 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅 crypto.cipher_list() |
string | 对齐方式, 支持 PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE |
string | 需要加密的数据 |
string | 密钥,需要对应算法的密钥长度 |
string | IV 值, 非 ECB 算法需要 |
返回值
返回值类型 | 解释 |
---|---|
string | 加密后的字符串 |
例子
-- 计算 AES
local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
local data2 = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "1234567890123456", "1234567890123456", "1234567890666666")
5.1.14 crypto.cipher_decrypt(type, padding, str, key, iv)
对称解密
参数
传入值类型 | 解释 |
---|---|
string | 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅 crypto.cipher_list() |
string | 对齐方式, 支持 PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE |
string | 需要解密的数据 |
string | 密钥,需要对应算法的密钥长度 |
string | IV 值, 非 ECB 算法需要 |
返回值
返回值类型 | 解释 |
---|---|
string | 解密后的字符串 |
例子
-- 用 AES 加密,然后用 AES 解密
local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
local data2 = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data, "1234567890123456")
-- data 的 hex 为 757CCD0CDC5C90EADBEEECF638DD0000
-- data2 的值为 1234567890123456
5.1.15 crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)
计算 CRC16
参数
传入值类型 | 解释 |
---|---|
string | CRC16 模式(”IBM”,”MAXIM”,”USB”,”MODBUS”,”CCITT”,”CCITT-FALSE”,”X25”,”XMODEM”,”DNP”,”USER-DEFINED”) |
string | 字符串 |
int | poly 值 |
int | initial 值 |
int | finally 值 |
int | 输入反转,1 反转,默认 0 不反转 |
int | 输入反转,1 反转,默认 0 不反转 |
返回值
返回值类型 | 解释 |
---|---|
int | 对应的 CRC16 值 |
例子
-- 计算 CRC16
local crc = crypto.crc16("")
5.1.16 crypto.crc16_modbus(data, start)
直接计算 modbus 的 crc16 值
参数
传入值类型 | 解释 |
---|---|
string | 数据 |
int | 初始化值,默认 0xFFFF |
返回值
返回值类型 | 解释 |
---|---|
int | 对应的 CRC16 值 |
例子
-- 计算 CRC16 modbus
local crc = crypto.crc16_modbus(data)
-- 2023.11.06 新增初始值设置
crc = crypto.crc16_modbus(data, 0xFFFF)
5.1.17 crypto.crc32(data)
计算 crc32 值
参数
传入值类型 | 解释 |
---|---|
string | 数据 |
返回值
返回值类型 | 解释 |
---|---|
int | 对应的 CRC32 值 |
例子
-- 计算 CRC32
local crc = crypto.crc32(data)
5.1.18 crypto.crc8(data, poly, start, revert)
计算 crc8 值
参数
传入值类型 | 解释 |
---|---|
string | 数据 |
int | crc 多项式,可选,如果不写,将忽略除了数据外所有参数 |
int | crc 初始值,可选,默认 0 |
boolean | 是否需要逆序处理,默认否 |
返回值
返回值类型 | 解释 |
---|---|
int | 对应的 CRC8 值 |
例子
-- 计算 CRC8
local crc = crypto.crc8(data)
local crc = crypto.crc8(data, 0x31, 0xff, false)
5.1.19 crypto.crc7(data, poly, start)
计算 crc7 值
参数
传入值类型 | 解释 |
---|---|
string | 数据 |
int | crc 多项式,可选,默认 0xE5 |
int | crc 初始值,可选,默认 0x00 |
返回值
返回值类型 | 解释 |
---|---|
int | 对应的 CRC7 值 |
例子
-- 计算 CRC7, 本 API 于 2023.10.07 新增
local crc = crypto.crc7(data)
local crc = crypto.crc7(data, 0x31, 0xff)
5.1.20 crypto.checksum(data, mode)
计算 checksum 校验和
参数
传入值类型 | 解释 |
---|---|
string | 待计算的数据,必选 |
int | 模式,累加模式, 0 - 异或, 1 - 累加, 默认为 0 |
返回值
返回值类型 | 解释 |
---|---|
int | checksum 值,校验和 |
例子
-- 本函数在 2022.12.28 添加
-- 单纯计算 checksum 值
local ck = crypto.checksum("OK")
log.info("checksum", "ok", string.format("%02X", ck))-- 第二个参数 mode 在 2023.5.23 日添加
5.1.21 crypto.trng(len)
生成真随机数
参数
传入值类型 | 解释 |
---|---|
int | 数据长度 |
返回值
返回值类型 | 解释 |
---|---|
string | 指定随机数字符串 |
例子
-- 生成 32 位随机数 ir
local r = crypto.trng(4)
local _, ir = pack.unpack(r, "I")
5.1.22 crypto.totp(secret,time)
计算 TOTP 动态密码的结果
参数
传入值类型 | 解释 |
---|---|
string | 网站提供的密钥(就是 BASE32 编码后的结果) |
int | 可选,时间戳,默认当前时间 |
返回值
返回值类型 | 解释 |
---|---|
int | 计算得出的六位数结果 计算失败返回 nil |
例子
--使用当前系统时间计算
local otp = crypto.totp("asdfassdfasdfass")
5.1.23 crypto.md_file(tp, path, hmac)
计算文件的 hash 值(md5/sha1/sha256 及 hmac 形式)
参数
传入值类型 | 解释 |
---|---|
string | hash 类型, 大小字母, 例如 “MD5” “SHA1” “SHA256” |
string | 文件路径, 例如 /luadb/logo.jpg |
string | hmac 值,可选 |
返回值
返回值类型 | 解释 |
---|---|
string | HEX 过的 hash 值,若失败会无返回值 |
例子
-- 无 hmac 的 hash 值
log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))
-- 带 hmac 的 hash 值
log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))
5.1.24 crypto.md(tp, data, hmac)
计算数据的 hash 值(md5/sha1/sha256 及 hmac 形式)
参数
传入值类型 | 解释 |
---|---|
string | hash 类型, 大小字母, 例如 “MD5” “SHA1” “SHA256” |
string | 待处理的数据 |
string | hmac 值,可选 |
返回值
返回值类型 | 解释 |
---|---|
string | HEX 过的 hash 值,若失败会无返回值 |
例子
-- 无 hmac 的 hash 值
log.info("md5", crypto.md("MD5", "1234567890"))
log.info("sha1", crypto.md("SHA1", "1234567890"))
log.info("sha256", crypto.md("SHA256", "1234567890"))
-- 带 hmac 的 hash 值
log.info("hmac_md5", crypto.md("MD5", "1234567890", "123456"))
log.info("hmac_sha1", crypto.md("SHA1", "1234567890", "123456"))
log.info("hmac_sha256", crypto.md("SHA256", "1234567890", "123456"))
5.1.25 crypto.hash_init(tp)
创建流式 hash 用的 stream
参数
传入值类型 | 解释 |
---|---|
string | hash 类型, 大写字母, 例如 “MD5” “SHA1” “SHA256” |
string | hmac 值,可选 |
返回值
返回值类型 | 解释 |
---|---|
userdata | 成功返回一个数据结构,否则返回 nil |
例子
-- 无 hmac 的 hash stream
local md5_stream = crypto.hash_init("MD5")
local sha1_stream = crypto.hash_init("SHA1")
local sha256_stream = crypto.hash_init("SHA256")
-- 带 hmac 的 hash stream
local md5_stream = crypto.hash_init("MD5", "123456")
local sha1_stream = crypto.hash_init("SHA1", "123456")
local sha256_stream = crypto.hash_init("SHA256", "123456")
5.1.26 crypto.hash_update(stream, data)
流式 hash 更新数据
参数
传入值类型 | 解释 |
---|---|
userdata | crypto.hash_init()创建的 stream, 必选 |
string | 待计算的数据,必选 |
return | 无 |
返回值
无
例子
crypto.hash_update(stream, "OK")
5.1.27 crypto.hash_finish(stream)
获取流式 hash 校验值并释放创建的 stream
参数
传入值类型 | 解释 |
---|---|
userdata | crypto.hash_init()创建的 stream,必选 |
返回值
返回值类型 | 解释 |
---|---|
string | 成功返回计算得出的流式 hash 值的 hex 字符串,失败无返回 |
例子
local hashResult = crypto.hash_finish(stream)
5.2 testJson.lua 代码
为了防止开机打印日志丢失,首先延时 8s,然后我们对一个字符串 test 进行了各种加解密的验证。
- 首先是 base64,传入字符串以及长度即可,解密时也是需要传入字符串以及长度。
- 然后是 xxtea 算法,传入字符串以及加密的密钥,xxtea 属于对称加密,解密时同样传入需要解密的字符串以及密钥即可。
- md5 属于不可逆运算,所以不存在解密函数,计算时传入字符串以及字符串长度。
- crc 有多种计算方式,最常用的为 modebus 其他方式详见 API 说明。
- AES 加密时传入加密模式,填充方式,加密字符串,密钥,解密时同样如此。
- DES 整体与 AES 相似,不同的是 DES 还需要传入偏移。
- RSA 主要是加解密场景和验签场景,需要注意的是,公钥和私钥的长度,上述 demo 为 2048 bit。
--- 模块功能:算法功能测试.
-- @author openLuat
-- @module crypto.testCrypto
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.20
module(...,package.seeall)
require"utils"
require "common"
--[[
加解密算法结果,可对照
http://tool.oschina.net/encrypt?type=2
http://www.ip33.com/crc.html
http://tool.chacuo.net/cryptaes
进行测试
]]
local slen = string.len
--- base64加解密算法测试
-- @return 无
-- @usage base64Test()
local function base64Test()
local originStr = "123456crypto.base64_encodemodule(...,package.seeall)sys.timerStart(test,5000)jdklasdjklaskdjklsa"
local encodeStr = crypto.base64_encode(originStr,slen(originStr))
log.info("testCrypto.base64_encode",encodeStr)
log.info("testCrypto.base64_decode",crypto.base64_decode(encodeStr,slen(encodeStr)))
end
--- hmac_md5算法测试
-- @return 无
-- @usage hmacMd5Test()
local function hmacMd5Test()
local originStr = "asdasdsadas"
local signKey = "123456"
log.info("testCrypto.hmac_md5",crypto.hmac_md5(originStr,slen(originStr),signKey,slen(signKey)))
end
--- xxtea算法测试
-- @return 无
-- @usage xxteaTest()
local function xxteaTest()
if crypto.xxtea_encrypt then
local text = "Hello World!";
local key = "07946";
local encrypt_data = crypto.xxtea_encrypt(text, key);
log.info("testCrypto.xxteaTest","xxtea_encrypt:"..encrypt_data)
local decrypt_data = crypto.xxtea_decrypt(encrypt_data, key);
log.info("testCrypto.xxteaTest","decrypt_data:"..decrypt_data)
end
end
--- 流式md5算法测试
-- @return 无
-- @usage flowMd5Test()
local function flowMd5Test()
local fmd5Obj=crypto.flow_md5()
local testTable={"lqlq666lqlq946","07946lq94607946","lq54075407540707946"}
for i=1, #(testTable) do
fmd5Obj:update(testTable[i])
end
log.info("testCrypto.flowMd5Test",fmd5Obj:hexdigest())
end
--- md5算法测试
-- @return 无
-- @usage md5Test()
local function md5Test()
--计算字符串的md5值
local originStr = "sdfdsfdsfdsffdsfdsfsdfs1234"
log.info("testCrypto.md5",crypto.md5(originStr,slen(originStr)))
-- crypto.md5,第一个参数为文件路径,第二个参数必须是"file"
log.info("testCrypto.sys.lua md5",crypto.md5("/lua/sys.lua","file"))
end
--- 流式sm3算法测试
-- @return 无
-- @usage flowSm3Test()
local function flowSm3Test()
local fSm3Obj=crypto.sm3start()
local testTable={"lqlq666lqlq946","07946lq94607946","lq54075407540707946"}
for i=1, #(testTable) do
fSm3Obj:sm3update(testTable[i])
end
log.info("testCrypto.flowSm3Test",fSm3Obj:sm3finish())
end
--- hmac_sha1算法测试
-- @return 无
-- @usage hmacSha1Test()
local function hmacSha1Test()
local originStr = "asdasdsadasweqcdsjghjvcb"
local signKey = "12345689012345"
log.info("testCrypto.hmac_sha1",crypto.hmac_sha1(originStr,slen(originStr),signKey,slen(signKey)))
end
--- sha1算法测试
-- @return 无
-- @usage sha1Test()
local function sha1Test()
local originStr = "sdfdsfdsfdsffdsfdsfsdfs1234"
log.info("testCrypto.sha1",crypto.sha1(originStr,slen(originStr)))
end
--- sha256算法测试
-- @return 无
-- @usage sha256Test()
local function sha256Test()
local originStr = "sdfdsfdsfdsffdsfdsfsdfs1234"
log.info("testCrypto.sha1",crypto.sha256(originStr,slen(originStr)))
end
local function hmacSha256Test()
if type(crypto.hmac_sha256)=="function" then
local originStr = "asdasdsadasweqcdsjghjvcb"
local signKey = "12345689012345"
log.info("testCrypto.hmac_sha256",crypto.hmac_sha256(originStr,signKey))
end
end
--- crc算法测试
-- @return 无
-- @usage crcTest()
local function crcTest()
local originStr = "sdfdsfdsfdsffdsfdsfsdfs1234"
--crypto.crc16()第一个参数是校验方法,必须为以下几个;第二个参数为计算校验的字符串
log.info("testCrypto.crc16_MODBUS",string.format("%04X",crypto.crc16("MODBUS",originStr)))
log.info("testCrypto.crc16_IBM",string.format("%04X",crypto.crc16("IBM",originStr)))
log.info("testCrypto.crc16_X25",string.format("%04X",crypto.crc16("X25",originStr)))
log.info("testCrypto.crc16_MAXIM",string.format("%04X",crypto.crc16("MAXIM",originStr)))
log.info("testCrypto.crc16_USB",string.format("%04X",crypto.crc16("USB",originStr)))
log.info("testCrypto.crc16_CCITT",string.format("%04X",crypto.crc16("CCITT",originStr)))
log.info("testCrypto.crc16_CCITT-FALSE",string.format("%04X",crypto.crc16("CCITT-FALSE",originStr)))
log.info("testCrypto.crc16_XMODEM",string.format("%04X",crypto.crc16("XMODEM",originStr)))
log.info("testCrypto.crc16_DNP",string.format("%04X",crypto.crc16("DNP",originStr)))
log.info("testCrypto.USER-DEFINED",string.format("%04X",crypto.crc16("USER-DEFINED",originStr,0x8005,0x0000,0x0000,0,0)))
-- log.info("testCrypto.crc16_modbus",string.format("%04X",crypto.crc16_modbus(originStr,slen(originStr))))
-- log.info("testCrypto.crc32",string.format("%08X",crypto.crc32(originStr,slen(originStr))))
end
--- aes算法测试(参考http://tool.chacuo.net/cryptaes)
-- @return 无
-- @usage aesTest()
local function aesTest()
local originStr = "AES128 ECB ZeroP test"--长度为16的整数倍
--加密模式:ECB;填充方式:ZeroPadding;密钥:1234567890123456;密钥长度:128 bit
local encodeStr = crypto.aes_encrypt("ECB","ZERO",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","ZERO",encodeStr,"1234567890123456"))
originStr = "AES128 ECB Pkcs5Padding test"
--加密模式:ECB;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS5",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS5",encodeStr,"1234567890123456"))
originStr = "AES128 ECB Pkcs7Padding test"
--加密模式:ECB;填充方式:Pkcs7Padding;密钥:1234567890123456;密钥长度:128 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS7",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS7",encodeStr,"1234567890123456"))
--lyy 21/05/26 添加ECB NonePadding算法测试
originStr = ("AES128 ECB NoneP")--长度为16的整数倍
--加密模式:ECB;填充方式:NONE;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("ECB","NONE",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","NONE",encodeStr,"1234567890123456"))
--lyy 21/05/26 添加ECB NonePadding算法测试
originStr = "AES192 ECB ZeroPadding test"
--加密模式:ECB;填充方式:ZeroPadding;密钥:123456789012345678901234;密钥长度:192 bit
local encodeStr = crypto.aes_encrypt("ECB","ZERO",originStr,"123456789012345678901234")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","ZERO",encodeStr,"123456789012345678901234"))
originStr = "AES192 ECB Pkcs5Padding test"
--加密模式:ECB;填充方式:Pkcs5Padding;密钥:123456789012345678901234;密钥长度:192 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS5",originStr,"123456789012345678901234")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS5",encodeStr,"123456789012345678901234"))
originStr = "AES192 ECB Pkcs7Padding test"
--加密模式:ECB;填充方式:Pkcs7Padding;密钥:123456789012345678901234;密钥长度:192 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS7",originStr,"123456789012345678901234")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS7",encodeStr,"123456789012345678901234"))
originStr = "AES256 ECB ZeroPadding test"
--加密模式:ECB;填充方式:ZeroPadding;密钥:12345678901234567890123456789012;密钥长度:256 bit
local encodeStr = crypto.aes_encrypt("ECB","ZERO",originStr,"12345678901234567890123456789012")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","ZERO",encodeStr,"12345678901234567890123456789012"))
originStr = "AES256 ECB Pkcs5Padding test"
--加密模式:ECB;填充方式:Pkcs5Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS5",originStr,"12345678901234567890123456789012")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS5",encodeStr,"12345678901234567890123456789012"))
originStr = "AES256 ECB Pkcs7Padding test"
--加密模式:ECB;填充方式:Pkcs7Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit
encodeStr = crypto.aes_encrypt("ECB","PKCS7",originStr,"12345678901234567890123456789012")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("ECB","PKCS7",encodeStr,"12345678901234567890123456789012"))
originStr = "AES128 CBC ZeroPadding test"
--加密模式:CBC;填充方式:ZeroPadding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CBC","ZERO",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","ZERO",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES128 CBC Pkcs5Padding test"
--加密模式:CBC;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS5",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS5",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES128 CBC Pkcs7Padding test"
--加密模式:CBC;填充方式:Pkcs7Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS7",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS7",encodeStr,"1234567890123456","1234567890666666"))
--lyy 21/05/26 添加CBC NonePadding算法测试
originStr = ("AES128 CBC NoneP")--长度为16的整数倍
--加密模式:CBC;填充方式:NONE;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","NONE",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","NONE",encodeStr,"1234567890123456","1234567890666666"))
--lyy 21/05/26 添加CBC NonePadding算法测试
originStr = "AES192 CBC ZeroPadding test"
--加密模式:CBC;填充方式:ZeroPadding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CBC","ZERO",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","ZERO",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES192 CBC Pkcs5Padding test"
--加密模式:CBC;填充方式:Pkcs5Padding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS5",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS5",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES192 CBC Pkcs7Padding test"
--加密模式:CBC;填充方式:Pkcs7Padding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS7",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS7",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES256 CBC ZeroPadding test"
--加密模式:CBC;填充方式:ZeroPadding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CBC","ZERO",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","ZERO",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES256 CBC Pkcs5Padding test"
--加密模式:CBC;填充方式:Pkcs5Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS5",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS5",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES256 CBC Pkcs7Padding test"
--加密模式:CBC;填充方式:Pkcs7Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CBC","PKCS7",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CBC","PKCS7",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES128 CTR ZeroPadding test"
--加密模式:CTR;填充方式:ZeroPadding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CTR","ZERO",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","ZERO",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES128 CTR Pkcs5Padding test"
--加密模式:CTR;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS5",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS5",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES128 CTR Pkcs7Padding test"
--加密模式:CTR;填充方式:Pkcs7Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS7",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS7",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES128 CTR NoneP"--长度为16的整数倍
--加密模式:CTR;填充方式:NonePadding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","NONE",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","NONE",encodeStr,"1234567890123456","1234567890666666"))
originStr = "AES192 CTR ZeroPadding test"
--加密模式:CTR;填充方式:ZeroPadding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CTR","ZERO",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","ZERO",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES192 CTR Pkcs5Padding test"
--加密模式:CTR;填充方式:Pkcs5Padding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS5",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS5",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES192 CTR Pkcs7Padding test"
--加密模式:CTR;填充方式:Pkcs7Padding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS7",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS7",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES192 CTR NoneP"--长度为16的整数倍
--加密模式:CTR;填充方式:NonePadding;密钥:123456789012345678901234;密钥长度:192 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","NONE",originStr,"123456789012345678901234","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","NONE",encodeStr,"123456789012345678901234","1234567890666666"))
originStr = "AES256 CTR ZeroPadding test"
--加密模式:CTR;填充方式:ZeroPadding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
local encodeStr = crypto.aes_encrypt("CTR","ZERO",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","ZERO",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES256 CTR Pkcs5Padding test"
--加密模式:CTR;填充方式:Pkcs5Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS5",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS5",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES256 CTR Pkcs7Padding test"
--加密模式:CTR;填充方式:Pkcs7Padding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","PKCS7",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","PKCS7",encodeStr,"12345678901234567890123456789012","1234567890666666"))
originStr = "AES256 CTR NoneP"--长度为16的整数倍
--加密模式:CTR;填充方式:NonePadding;密钥:12345678901234567890123456789012;密钥长度:256 bit;偏移量:1234567890666666
encodeStr = crypto.aes_encrypt("CTR","NONE",originStr,"12345678901234567890123456789012","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.aes_decrypt("CTR","NONE",encodeStr,"12345678901234567890123456789012","1234567890666666"))
end
--rsa算法测试
local function rsaTest()
--local plainStr = "1234567890asdfghjklzxcvbnm"
local plainStr = "firmId=10015&model=zw-sp300&sn=W01201910300000108&version=1.0.0"
--公钥加密(2048bit,这个bit与实际公钥的bit要保持一致)
local encryptStr = crypto.rsa_encrypt("PUBLIC_KEY",io.readFile("/lua/public.key"),2048,"PUBLIC_CRYPT",plainStr)
log.info("rsaTest.encrypt",encryptStr:toHex())
--私钥解密(2048bit,这个bit与实际私钥的bit要保持一致)
local decryptStr = crypto.rsa_decrypt("PRIVATE_KEY",io.readFile("/lua/private.key"),2048,"PRIVATE_CRYPT",encryptStr)
log.info("rsaTest.decrypt",decryptStr) --此处的decryptStr应该与plainStr相同
--私钥签名(2048bit,这个bit与实际私钥的bit要保持一致)
local signStr = crypto.rsa_sha256_sign("PRIVATE_KEY",io.readFile("/lua/private.key"),2048,"PRIVATE_CRYPT",plainStr)
log.info("rsaTest.signStr",signStr:toHex())
--公钥验签(2048bit,这个bit与实际公钥的bit要保持一致)
local verifyResult = crypto.rsa_sha256_verify("PUBLIC_KEY",io.readFile("/lua/public.key"),2048,"PUBLIC_CRYPT",signStr,plainStr)
log.info("rsaTest.verify",verifyResult)
--私钥解密某个客户的公钥加密密文
encryptStr = string.fromHex("af750a8c95f9d973a033686488197cffacb8c1b2b5a15ea8779a48a72a1cdb2f9c948fe5ce0ac231a16de16b5fb609f62ec81c7646c1f018e333860627b5d4853cfe77f71ea7e4573323905faf0a759d59729d2afb80e46ff1f1b715227b599a14f3b9feb676f1feb1c2acd97f4d494124237a720ca781a16a2b600c17e348a5fdd3c374384276147b93ce93cc5a005a0aaf1581cdb7d58bfa84b4e4d7263efc02bf7ad80b15937ce8b37ced4e1ef8899be5c2a7d338cb5c4784c6b8a1cb31e7ecd1ec48597a02050b1190a3e13f2253a35e8cbc094c0af28b968f05a7f946a7a8cf3f9da2013d53ee51ca74279f8f36662e093b37db83caef5b18b666d405d4")
decryptStr = crypto.rsa_decrypt("PRIVATE_KEY",io.readFile("/lua/private.key"),2048,"PRIVATE_CRYPT",encryptStr)
log.info("rsaTest.decrypt",decryptStr)
--公钥验签某个客户的私钥签名密文
signStr = string.fromHex("7251fd625c01ac41e277d11b5b795962ba42d89a645eb9fe2241b2d8a9b6b5b6ea70e23e6933ef1324495749abde0e31eaf4fefe6d09f9270c0510790bd6075595717522539b7b70b798bdc216dae3873389644d73b04ecaeb01b25831904955a891d2459334a3f9f1e4558f7f99906c35f94c377f7f95cf0d3e062d8eb513fd723ad8b3981027b09126fbeb72d5fe4554a32b9c270f8f46032ede59387769b1fb090f0b4be15aaac2744a666dfbde7c04e02979f1c1b4e4c0f23c6bb9f60941312850caf41442d68ad7c9e939b7305ac6712ad31427f1c1d7b4f68001df9ce03367bd35e401a420f526aee3c96c2caaccb9a8db09b30930172b4c2847725d05")
verifyResult = crypto.rsa_sha256_verify("PUBLIC_KEY",io.readFile("/lua/public.key"),2048,"PUBLIC_CRYPT",signStr,"firmId=10015&model=zw-sp300&sn=W01201910300000108&version=1.0.0")
log.info("rsaTest.verifyResult customer",verifyResult)
end
--DES算法测试
local function desTest()
local originStr = "123456781234"
--加密模式:DES CBC;填充方式:ZERO;密钥:12345678; 偏移:00000000
encodeStr = crypto.des_encrypt("CBC","ZERO",originStr,"12345678","00000000")
print(originStr,"DES CBC ZeroPadding encrypt",string.toHex(encodeStr))
log.info("DES ECB ZeroPadding decrypt",crypto.des_decrypt("CBC","ZERO",encodeStr,"12345678","00000000"))
originStr = "123456789"
--加密模式:DES CBC;填充方式:Pkcs5Padding;密钥:12345678; 偏移:00000000
encodeStr = crypto.des_encrypt("CBC","PKCS5",originStr,"12345678","00000000")
print(originStr,"DES CBC Pkcs5Padding encrypt",string.toHex(encodeStr))
log.info("DES ECB Pkcs5Padding decrypt",crypto.des_decrypt("CBC","PKCS5",encodeStr,"12345678","00000000"))
--lyy 21/05/26 添加CBC NonePadding算法测试
originStr = ("DES064 CBC NoneP")--长度为16的整数倍,且密钥长度只支持8位
--加密模式:CBC;填充方式:NONE;密钥:12345678;密钥长度:64 bit;偏移量:00000000
encodeStr = crypto.des_encrypt("CBC","NONE",originStr,"12345678","00000000")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.des_decrypt("CBC","NONE",encodeStr,"12345678","00000000"))
--lyy 21/05/26 添加CBC NonePadding算法测试
--lyy 21/05/26 添加CBC Pkcs7Padding算法测试
originStr = "123456789"
--加密模式:DES CBC;填充方式:Pkcs7Padding;密钥:12345678; 偏移:00000000
encodeStr = crypto.des_encrypt("CBC","PKCS7",originStr,"12345678","00000000")
print(originStr,"DES CBC Pkcs7Padding encrypt",string.toHex(encodeStr))
log.info("DES CBC Pkcs7Padding decrypt",crypto.des_decrypt("CBC","PKCS7",encodeStr,"12345678","00000000"))
--lyy 21/05/26 添加CBC Pkcs7Padding算法测试
originStr = "123456789"
--加密模式:DES ECB;填充方式:Pkcs7Padding;密钥:12345678
encodeStr = crypto.des_encrypt("ECB","PKCS7",originStr,"12345678","12345678")
print(originStr,"DES ECB Pkcs7Padding encrypt",string.toHex(encodeStr))
log.info("DES ECB Pkcs7Padding decrypt",crypto.des_decrypt("ECB","PKCS7",encodeStr,"12345678"))
originStr = ("31323334353637383900000000000000"):fromHex()
--加密模式:DES ECB;填充方式:NONE;密钥:12345678
encodeStr = crypto.des_encrypt("ECB","NONE",originStr,"12345678")
print(originStr,"DES ECB NonePadding encrypt",string.toHex(encodeStr))
log.info("DES ECB NonePadding decrypt",string.toHex(crypto.des_decrypt("ECB","NONE",encodeStr,"12345678")))
--lyy 21/05/26 添加ECB Pkcs5Padding算法测试
originStr = "123456789"
--加密模式:DES ECB;填充方式:Pkcs5Padding;密钥:12345678
encodeStr = crypto.des_encrypt("ECB","PKCS5",originStr,"12345678","12345678")
print(originStr,"DES ECB Pkcs5Padding encrypt",string.toHex(encodeStr))
log.info("DES ECB Pkcs5Padding decrypt",crypto.des_decrypt("ECB","PKCS5",encodeStr,"12345678"))
--lyy 21/05/26 添加ECB Pkcs5Padding算法测试
--lyy 21/05/26 添加ECB ZeroPadding算法测试
originStr = "123456789"
--加密模式:DES ECB;填充方式:ZERO;密钥:12345678; 偏移:00000000
encodeStr = crypto.des_encrypt("ECB","ZERO",originStr,"12345678")
print(originStr,"DES ECB ZeroPadding encrypt",string.toHex(encodeStr))
log.info("DES ECB ZeroPadding decrypt",crypto.des_decrypt("ECB","ZERO",encodeStr,"12345678"))
--lyy 21/05/26 添加ECB ZeroPadding算法测试
end
--DES3算法测试
local function des3Test()
local originStr = "123456781234"
--加密模式:DES3 CBC;填充方式:ZERO;密钥:123456781234567812345678; 偏移:00000000
encodeStr = crypto.des3_encrypt("CBC","ZERO",originStr,"123456781234567812345678","00000000")
print(originStr,"DES3 CBC ZeroPadding encrypt",string.toHex(encodeStr))
log.info("DES3 CBC ZeroPadding decrypt",crypto.des3_decrypt("CBC","ZERO",encodeStr,"123456781234567812345678","00000000"))
originStr = "123456789"
--加密模式:DES3 CBC;填充方式:Pkcs5Padding;密钥:123456781234567812345678; 偏移:00000000
encodeStr = crypto.des3_encrypt("CBC","PKCS5",originStr,"123456781234567812345678","00000000")
print(originStr,"DES3 CBC Pkcs5Padding encrypt",string.toHex(encodeStr))
log.info("DES3 CBC Pkcs5Padding decrypt",crypto.des3_decrypt("CBC","PKCS5",encodeStr,"123456781234567812345678","00000000"))
--lyy 21/05/26 添加CBC NonePadding算法测试
originStr = ("DES3192 CBC None")--长度为16的整数倍,密钥只支持16或24位
--加密模式:CBC;填充方式:NONE;密钥:123456781234567812345678; 密钥长度:192 bit;偏移量:00000000
encodeStr = crypto.des3_encrypt("CBC","NONE",originStr,"123456781234567812345678","00000000")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testCrypto.decrypt",crypto.des3_decrypt("CBC","NONE",encodeStr,"123456781234567812345678","00000000"))
--lyy 21/05/26 添加CBC NonePadding算法测试
--lyy 21/05/26 添加CBC Pkcs7Padding算法测试
originStr = "123456789"
--加密模式:DES3 CBC;填充方式:Pkcs7Padding;密钥:123456781234567812345678; 偏移:00000000
encodeStr = crypto.des3_encrypt("CBC","PKCS7",originStr,"123456781234567812345678","00000000")
print(originStr,"DES3 CBC Pkcs7Padding encrypt",string.toHex(encodeStr))
log.info("DES3 CBC Pkcs7Padding decrypt",crypto.des3_decrypt("CBC","PKCS7",encodeStr,"123456781234567812345678","00000000"))
--lyy 21/05/26 添加CBC Pkcs7Padding算法测试
originStr = "123456789"
--加密模式:DES3 ECB;填充方式:Pkcs7Padding;密钥:123456781234567812345678
encodeStr = crypto.des3_encrypt("ECB","PKCS7",originStr,"123456781234567812345678","123456781234567812345678")
print(originStr,"DES3 ECB Pkcs7Padding encrypt",string.toHex(encodeStr))
log.info("DES3 ECB Pkcs7Padding decrypt",crypto.des3_decrypt("ECB","PKCS7",encodeStr,"123456781234567812345678"))
originStr = ("31323334353637383900000000000000"):fromHex()
--加密模式:DES3 ECB;填充方式:NONE;密钥:123456781234567812345678
encodeStr = crypto.des3_encrypt("ECB","NONE",originStr,"123456781234567812345678")
print(originStr,"DES3 ECB NonePadding encrypt",string.toHex(encodeStr))
log.info("DES3 ECB NonePadding decrypt",string.toHex(crypto.des3_decrypt("ECB","NONE",encodeStr,"123456781234567812345678")))
--lyy 21/05/26 添加ECB Pkcs5Padding算法测试
originStr = "123456789"
--加密模式:DES3 ECB;填充方式:Pkcs5Padding;密钥:123456781234567812345678
encodeStr = crypto.des3_encrypt("ECB","PKCS5",originStr,"123456781234567812345678","123456781234567812345678")
print(originStr,"DES3 ECB Pkcs5Padding encrypt",string.toHex(encodeStr))
log.info("DES3 ECB Pkcs5Padding decrypt",crypto.des3_decrypt("ECB","PKCS5",encodeStr,"123456781234567812345678"))
--lyy 21/05/26 添加ECB Pkcs5Padding算法测试
--lyy 21/05/26 添加ECB ZeroPadding算法测试
originStr = "123456789"
--加密模式:DES3 ECB;填充方式:ZERO;密钥:123456781234567812345678
encodeStr = crypto.des3_encrypt("ECB","ZERO",originStr,"123456781234567812345678")
print(originStr,"DES3 ECB ZeroPadding encrypt",string.toHex(encodeStr))
log.info("DES3 ECB ZeroPadding decrypt",crypto.des3_decrypt("ECB","ZERO",encodeStr,"123456781234567812345678"))
--lyy 21/05/26 添加ECB ZeroPadding算法测试
end
--- 算法测试入口
-- @return
local function cryptoTest()
print("test start")
hmacMd5Test()
md5Test()
hmacSha1Test()
flowMd5Test()
--从LuatOS-Air_V3102版本开始,才支持SM3算法
pcall(flowSm3Test)
base64Test()
crcTest()
aesTest()
sha1Test()
sha256Test()
hmacSha256Test()
xxteaTest()
print(pcall(rsaTest))
desTest()
des3Test()
print("test end")
end
sys.timerStart(cryptoTest,6000)
5.3 main.lua 代码
本代码为主程序脚本,系统启动后首先会对 4G 网络进行配置,等待网络连接成功,然后加载测试模块。
5.4 private.key 代码
私钥。
5.5 public.key 代码
公钥
六、开机调试
6.1 开发板开机
连接好硬件并下载固件后,启动 Luatools 软件,系统运行信息将显示在界面中。红框中为开发板连接到 PC 机后正常打印的信息,如下图所示。
6.2 功能调试
对一个字符串 test 进行了各种加解密的验证。
- 首先是 base64,传入字符串以及长度即可,解密时也是需要传入字符串以及长度。
- 然后是 xxtea 算法,传入字符串以及加密的密钥,xxtea 属于对称加密,解密时同样传入需要解密的字符串以及密钥即可。
- md5 属于不可逆运算,所以不存在解密函数,计算时传入字符串以及字符串长度。
- crc 有多种计算方式,最常用的为 modebus 其他方式详见 API 说明。
- AES 加密时传入加密模式,填充方式,加密字符串,密钥,解密时同样如此。
- DES 整体与 AES 相似,不同的是 DES 还需要传入偏移。
- RSA 主要是加解密场景和验签场景,需要注意的是,公钥和私钥的长度,上述 demo 为 2048 bit。
对应输出的 log:
[2024-10-29 11:23:28.760] test start
[2024-10-29 11:23:28.760] [I]-[testCrypto.hmac_md5] 38A7B18DC5F6543849DC49F06FADE3CC
[2024-10-29 11:23:28.760] [I]-[testCrypto.md5] 235B69FBC9E75C4FD5E8C59F9CB16500
[2024-10-29 11:23:28.807] [I]-[testCrypto.sys.lua md5] 27860464BEC2FF45589BD9E64C51A585
[2024-10-29 11:23:28.807] [I]-[testCrypto.hmac_sha1] E3BB109BA59AF6A1F677157E8EC6B21349B9220F
[2024-10-29 11:23:28.807] [I]-[testCrypto.flowMd5Test] 1C776E3B5C16B333F3F6E436CE0EB702
[2024-10-29 11:23:28.807] [I]-[testCrypto.flowSm3Test] DA208FCB65C9C70C5C7748009A083C0943925C545BE7ACF8FCE15B825882A0C3
[2024-10-29 11:23:28.807] [I]-[testCrypto.base64_encode] MTIzNDU2Y3J5cHRvLmJhc2U2NF9lbmNvZGVtb2R1bGUoLi4uLHBhY2thZ2Uuc2VlYWxsKXN5cy50aW1lclN0YXJ0KHRlc3Qs
[2024-10-29 11:23:28.807] NTAwMClqZGtsYXNkamtsYXNrZGprbHNh
[2024-10-29 11:23:28.807] decode running
[2024-10-29 11:23:28.807] [I]-[testCrypto.base64_decode] 123456crypto.base64_encodemodule(...,package.seeall)sys.timerStart(test,5000)jdklasdjklaskdjklsa
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_MODBUS] 1184
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_IBM] F8E3
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_X25] B227
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_MAXIM] 071C
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_USB] EE7B
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_CCITT] 87C6
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_CCITT-FALSE] 7551
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_XMODEM] 0D02
[2024-10-29 11:23:28.807] [I]-[testCrypto.crc16_DNP] 15B3
[2024-10-29 11:23:28.807] [I]-[testCrypto.USER-DEFINED] 438C
[2024-10-29 11:23:28.807] AES128 ECB ZeroP test encrypt 03859DED5C9EE54FDB55160234DF4F4BF40CE1C6DFE40AE7B4064FD78CC6CF06 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES128 ECB ZeroP test
[2024-10-29 11:23:28.807] AES128 ECB Pkcs5Padding test encrypt B9BFD9892A73C54B1E64EE21805C0755C37333C787E05BCB0DD37B6F75E0D364 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES128 ECB Pkcs5Padding test
[2024-10-29 11:23:28.807] AES128 ECB Pkcs7Padding test encrypt 9F3DCC9D3C9F3D6A37525AD7E359E489C37333C787E05BCB0DD37B6F75E0D364 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES128 ECB Pkcs7Padding test
[2024-10-29 11:23:28.807] AES128 ECB NoneP encrypt 615619991127988BA9A6BF00E878B219 16
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES128 ECB NoneP
[2024-10-29 11:23:28.807] AES192 ECB ZeroPadding test encrypt D1786C0E706812F4B641066AE69D857782FD85686E0F1883841CFB6CF72DC20D 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES192 ECB ZeroPadding test
[2024-10-29 11:23:28.807] AES192 ECB Pkcs5Padding test encrypt A653D2C78F882AE6BACA95ADD0423BA610CB7A29C540DB2C695EC1847A3CA834 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES192 ECB Pkcs5Padding test
[2024-10-29 11:23:28.807] AES192 ECB Pkcs7Padding test encrypt 193C2FA531E40EDABCAE12F078F06CDB10CB7A29C540DB2C695EC1847A3CA834 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES192 ECB Pkcs7Padding test
[2024-10-29 11:23:28.807] AES256 ECB ZeroPadding test encrypt 6F5DBB86DBA00BFD8AA573D65AEF8674BCA0E8DDB62858AE6169AC9FB2F7E826 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES256 ECB ZeroPadding test
[2024-10-29 11:23:28.807] AES256 ECB Pkcs5Padding test encrypt CED7FDEABA579659FF7EDEAAABF59B8EA6C1C14B1729F94827B7313DF4E1C281 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES256 ECB Pkcs5Padding test
[2024-10-29 11:23:28.807] AES256 ECB Pkcs7Padding test encrypt F8F26E886CBC8392E0D8DA2E10378D83A6C1C14B1729F94827B7313DF4E1C281 32
[2024-10-29 11:23:28.807] [I]-[testCrypto.decrypt] AES256 ECB Pkcs7Padding test
[2024-10-29 11:23:28.979] AES128 CBC ZeroPadding test encrypt EFC28E5B9B2411E25E812C5EE3A84DF1DCB777A089BC1282C12ACB213B156280 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CBC ZeroPadding test
[2024-10-29 11:23:28.979] AES128 CBC Pkcs5Padding test encrypt 4B2C53EF993C391D590AFC00F1462CD68776E3153BE68A42CAD1FDB3751B42F2 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CBC Pkcs5Padding test
[2024-10-29 11:23:28.979] AES128 CBC Pkcs7Padding test encrypt 1D2BD713566E20629FF35BE8BB491BB83472A603F04F235033DB45F12DB12F26 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CBC Pkcs7Padding test
[2024-10-29 11:23:28.979] AES128 CBC NoneP encrypt E89AD3255942B5350A945C757B0E509E 16
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CBC NoneP
[2024-10-29 11:23:28.979] AES192 CBC ZeroPadding test encrypt 1D3D07BFE06746C0BBDF37E62A1A1287BF105F6D455E2B4035775551D5CF3853 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CBC ZeroPadding test
[2024-10-29 11:23:28.979] AES192 CBC Pkcs5Padding test encrypt CA3332B3A7539FCA8D5590661C24EBBC6B43811DED361A8FD447217EBBE29647 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CBC Pkcs5Padding test
[2024-10-29 11:23:28.979] AES192 CBC Pkcs7Padding test encrypt EECECDF55D6246350C0AA3D975D781E578C0548DDA3EA835B618481FDEE5437C 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CBC Pkcs7Padding test
[2024-10-29 11:23:28.979] AES256 CBC ZeroPadding test encrypt 24731FCCB705D7912F7DA1ED6CE48A39A2A78CA67C373FD53E5519EFCA468208 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CBC ZeroPadding test
[2024-10-29 11:23:28.979] AES256 CBC Pkcs5Padding test encrypt 1A7E4EB176C6B28979B2D6D4191D8DF84F9356C1AD4E676132749C40525D2311 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CBC Pkcs5Padding test
[2024-10-29 11:23:28.979] AES256 CBC Pkcs7Padding test encrypt 7800D6E229E5D3A431DFB77CFAE88E60A52C4C24243E8D02D11FB2C99F8681F3 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CBC Pkcs7Padding test
[2024-10-29 11:23:28.979] AES128 CTR ZeroPadding test encrypt EA793EF1E0C3A0AD92281BA907FE6A2B4AB9ED48D224E4F545CE6591992E8920 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CTR ZeroPadding test
[2024-10-29 11:23:28.979] AES128 CTR Pkcs5Padding test encrypt EA793EF1E0C3A0AD92281BA309EF764E7BBCED45D52DA3A154D862E59D2A8D24 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CTR Pkcs5Padding test
[2024-10-29 11:23:28.979] AES128 CTR Pkcs7Padding test encrypt EA793EF1E0C3A0AD92281BA309EF764C7BBCED45D52DA3A154D862E59D2A8D24 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CTR Pkcs7Padding test
[2024-10-29 11:23:28.979] AES128 CTR NoneP encrypt EA793EF1E0C3A0AD92281BBD0DE2602B 16
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES128 CTR NoneP
[2024-10-29 11:23:28.979] AES192 CTR ZeroPadding test encrypt 6CA01867E3A5F75D1FE7D34A30CBE1F08BB75F7F7ACAC722F0674D092A4C041D 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CTR ZeroPadding test
[2024-10-29 11:23:28.979] AES192 CTR Pkcs5Padding test encrypt 6CA01867E3A5F75D1FE7D3403EDAFD95BAB25F727DC38076E1714A7D2E480019 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CTR Pkcs5Padding test
[2024-10-29 11:23:28.979] AES192 CTR Pkcs7Padding test encrypt 6CA01867E3A5F75D1FE7D3403EDAFD97BAB25F727DC38076E1714A7D2E480019 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CTR Pkcs7Padding test
[2024-10-29 11:23:28.979] AES192 CTR NoneP encrypt 6CA01867E3A5F75D1FE7D35E3AD7EBF0 16
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES192 CTR NoneP
[2024-10-29 11:23:28.979] AES256 CTR ZeroPadding test encrypt 0574FEE86035C7EC4BDE08A57E1F4B647E0C050E227FA3DE3DFDD513948E3296 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CTR ZeroPadding test
[2024-10-29 11:23:28.979] AES256 CTR Pkcs5Padding test encrypt 0574FEE86035C7EC4BDE08AF700E57014F0905032576E48A2CEBD267908A3692 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CTR Pkcs5Padding test
[2024-10-29 11:23:28.979] AES256 CTR Pkcs7Padding test encrypt 0574FEE86035C7EC4BDE08AF700E57034F0905032576E48A2CEBD267908A3692 32
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CTR Pkcs7Padding test
[2024-10-29 11:23:28.979] AES256 CTR NoneP encrypt 0574FEE86035C7EC4BDE08B174034164 16
[2024-10-29 11:23:28.979] [I]-[testCrypto.decrypt] AES256 CTR NoneP
[2024-10-29 11:23:28.979] [I]-[testCrypto.sha1] 16EBE919119B9B54C8AF6B4F2A09C18B6B6D8218
[2024-10-29 11:23:28.979] [I]-[testCrypto.sha1] C0DF1D99A20FFE5093920FE5267D1232D1302770247BB512D95465626A2C20B8
[2024-10-29 11:23:28.979] [I]-[testCrypto.hmac_sha256] 2FDF6B243E7E9ABACBA693361185BC51E13163D50870CF4EA11E05537D5AF15C
[2024-10-29 11:23:28.979] [I]-[testCrypto.xxteaTest] xxtea_encrypt:@.߁= %
[2024-10-29 11:23:28.979] [I]-[testCrypto.xxteaTest] decrypt_data:Hello World!
[2024-10-29 11:23:28.979] [I]-[rsaTest.encrypt] C0AD7BA2B54C04243D46584164D0AC57408AE896BA237394A32AFABF86F27E8205F73D754AC8712D9008FFB85B721A0031FDC55A1
[2024-10-29 11:23:28.979] 6306455A17532AB966A68A3A2467FC8789BF3BF21D7501B7D7B5601FB1FD27FB175245C87236E2537FAA5264EC8E9EBB8CA912738C4690A87EF780BEFE3BE1B
[2024-10-29 11:23:28.979] 03DE2BE00DF2A2C98326DACEA208A848BFFFE66B1AA396A38312BDBB6A671F5B3D23BA1499CC02D5AEAA04E0BC88A46A117C8B71CCAF92DFE6D7BC961586EB1
[2024-10-29 11:23:28.979] D19C60C60C666F0D9D12AC13CD587B485C933F420012E576C29BE14557B86308B030990A01E7E11BAB6808A7AF77C5B244D67F0DAB46C77111EECA90C4F0B17
[2024-10-29 11:23:28.979] 8EFF1B6ECA7AF3C5135BA671B0 256
[2024-10-29 11:23:29.244] [I]-[rsaTest.decrypt] firmId=10015&model=zw-sp300&sn=W01201910300000108&version=1.0.0
[2024-10-29 11:23:29.619] [I]-[rsaTest.signStr] 7251FD625C01AC41E277D11B5B795962BA42D89A645EB9FE2241B2D8A9B6B5B6EA70E23E6933EF1324495749ABDE0E31EAF4FEFE6
[2024-10-29 11:23:29.619] D09F9270C0510790BD6075595717522539B7B70B798BDC216DAE3873389644D73B04ECAEB01B25831904955A891D2459334A3F9F1E4558F7F99906C35F94C37
[2024-10-29 11:23:29.619] 7F7F95CF0D3E062D8EB513FD723AD8B3981027B09126FBEB72D5FE4554A32B9C270F8F46032EDE59387769B1FB090F0B4BE15AAAC2744A666DFBDE7C04E0297
[2024-10-29 11:23:29.619] 9F1C1B4E4C0F23C6BB9F60941312850CAF41442D68AD7C9E939B7305AC6712AD31427F1C1D7B4F68001DF9CE03367BD35E401A420F526AEE3C96C2CAACCB9A8
[2024-10-29 11:23:29.619] DB09B30930172B4C2847725D05 256
[2024-10-29 11:23:29.666] [I]-[rsaTest.verify] true
[2024-10-29 11:23:29.979] [I]-[rsaTest.decrypt] {"topic":"/a1B5qECRYOd/PDN9Y4jiSHQ8XisJlTEM/user/box/notify","device_name":"PDN9Y4jiSHQ8XisJlTEM","device
[2024-10-29 11:23:29.979] _secret":"idspMEdivLYoS9QdlWF6ycF8HAekSrKn","product_key":"a1B5qECRYOd"}
[2024-10-29 11:23:30.010] [I]-[rsaTest.verifyResult customer] true
[2024-10-29 11:23:30.010] true
[2024-10-29 11:23:30.010] 123456781234 DES CBC ZeroPadding encrypt BA9FEFA8C77512AE1D97236D8D82AABF 16
[2024-10-29 11:23:30.010] [I]-[DES ECB ZeroPadding decrypt] 123456781234
[2024-10-29 11:23:30.010] 123456789 DES CBC Pkcs5Padding encrypt BA9FEFA8C77512AEFCDD39E0D072C178 16
[2024-10-29 11:23:30.010] [I]-[DES ECB Pkcs5Padding decrypt] 123456789
[2024-10-29 11:23:30.010] DES064 CBC NoneP encrypt DFBB9039FF88F3D927B1C2EE61254950 16
[2024-10-29 11:23:30.010] [I]-[testCrypto.decrypt] DES064 CBC NoneP
[2024-10-29 11:23:30.010] 123456789 DES CBC Pkcs7Padding encrypt BA9FEFA8C77512AEFCDD39E0D072C178 16
[2024-10-29 11:23:30.010] [I]-[DES CBC Pkcs7Padding decrypt] 123456789
[2024-10-29 11:23:30.010] 123456789 DES ECB Pkcs7Padding encrypt 96D0028878D58C8928B46A38A7A27092 16
[2024-10-29 11:23:30.010] [I]-[DES ECB Pkcs7Padding decrypt] 123456789
[2024-10-29 11:23:30.010] 123456789 DES ECB NonePadding encrypt 96D0028878D58C89C733FA06CE00A12B 16
[2024-10-29 11:23:30.010] [I]-[DES ECB NonePadding decrypt] 31323334353637383900000000000000 16
[2024-10-29 11:23:30.010] 123456789 DES ECB Pkcs5Padding encrypt 96D0028878D58C8928B46A38A7A27092 16
[2024-10-29 11:23:30.010] [I]-[DES ECB Pkcs5Padding decrypt] 123456789
[2024-10-29 11:23:30.010] 123456789 DES ECB ZeroPadding encrypt 96D0028878D58C89C733FA06CE00A12B 16
[2024-10-29 11:23:30.010] [I]-[DES ECB ZeroPadding decrypt] 123456789
[2024-10-29 11:23:30.010] 123456781234 DES3 CBC ZeroPadding encrypt BA9FEFA8C77512AE1D97236D8D82AABF 16
[2024-10-29 11:23:30.010] [I]-[DES3 CBC ZeroPadding decrypt] 123456781234
[2024-10-29 11:23:30.010] 123456789 DES3 CBC Pkcs5Padding encrypt BA9FEFA8C77512AEFCDD39E0D072C178 16
[2024-10-29 11:23:30.010] [I]-[DES3 CBC Pkcs5Padding decrypt] 123456789
[2024-10-29 11:23:30.010] DES3192 CBC None encrypt 07D80DF375A4A4E8231FBFD3A5FED7A0 16
[2024-10-29 11:23:30.135] [I]-[testCrypto.decrypt] DES3192 CBC None
[2024-10-29 11:23:30.135] 123456789 DES3 CBC Pkcs7Padding encrypt BA9FEFA8C77512AEFCDD39E0D072C178 16
[2024-10-29 11:23:30.135] [I]-[DES3 CBC Pkcs7Padding decrypt] 123456789
[2024-10-29 11:23:30.135] 123456789 DES3 ECB Pkcs7Padding encrypt 96D0028878D58C8928B46A38A7A27092 16
[2024-10-29 11:23:30.135] [I]-[DES3 ECB Pkcs7Padding decrypt] 123456789
[2024-10-29 11:23:30.135] 123456789 DES3 ECB NonePadding encrypt 96D0028878D58C89C733FA06CE00A12B 16
[2024-10-29 11:23:30.135] [I]-[DES3 ECB NonePadding decrypt] 31323334353637383900000000000000 16
[2024-10-29 11:23:30.135] 123456789 DES3 ECB Pkcs5Padding encrypt 96D0028878D58C8928B46A38A7A27092 16
[2024-10-29 11:23:30.135] [I]-[DES3 ECB Pkcs5Padding decrypt] 123456789
[2024-10-29 11:23:30.135] 123456789 DES3 ECB ZeroPadding encrypt 96D0028878D58C89C733FA06CE00A12B 16
[2024-10-29 11:23:30.135] [I]-[DES3 ECB ZeroPadding decrypt] 123456789
[2024-10-29 11:23:30.135] test end
七、常见问题
7.1 如何生成公钥以及私钥文件
稳妥的方式还是采用 OPENSSL 本地生成,在 Windows 环境下,可自行下载 OPENSSL 工具 http://www.openssl.org/related/binaries.html, 在 Linux 环境下,可安装 OPENSSL 工具包(以 ubuntu 为例,执行 sudo apt-get install openssl)。 如果在线测试功能,可以访问网页 https://www.ssleye.com/pass_double.html
给读者的话
本篇文章由
杨超
开发;本篇文章描述的内容,如果有错误、细节缺失、细节不清晰或者其他任何问题,总之就是无法解决您遇到的问题;
请登录合宙技术交流论坛,点击文档找错赢奖金-Air724UG-LuatOS-软件指南-通用工具库-通用加解密函数(crypto);
用截图标注+文字描述的方式跟帖回复,记录清楚您发现的问题;
我们会迅速核实并且修改文档;
同时也会为您累计找错积分,您还可能赢取月度找错奖金!