Lua AES加解密
local aes = require "resty.aes"
local str = require "resty.string"
local aes_128_cbc_md5 = aes:new("cQZEdZFA10OS")-- 需要自己写一个函数将16进制转2进制
function hex2bin(hexstr)local str = ""for i = 1, string.len(hexstr) - 1, 2 dolocal doublebytestr = string.sub(hexstr, i, i+1);local n = tonumber(doublebytestr, 16);if 0 == n thenstr = str .. '\00'elsestr = str .. string.format("%c", n)endendreturn str
end-- 加密函数,返回16进制
local function encrypt(content)local encrypted = aes_128_cbc_md5:encrypt(content)return str.to_hex(encrypted)
end-- 解密函数 返回解密字符串
local function dencrypt(content)
local dencrypted = aes_128_cbc_md5:decrypt(hex2bin(content))
return dencrypted
endngx.say(encrypt('123456'))
ngx.say(dencrypt('55858b90358c48d507c3901312b4f20a'))
resty/aes.lua
-- Copyright (C) by Yichun Zhang (agentzh)--local asn1 = require "resty.asn1"
local ffi = require "ffi"
local ffi_new = ffi.new
local ffi_gc = ffi.gc
local ffi_str = ffi.string
local ffi_copy = ffi.copy
local C = ffi.C
local setmetatable = setmetatable
--local error = error
local type = typelocal _M = { _VERSION = '0.09' }local mt = { __index = _M }ffi.cdef[[
typedef struct engine_st ENGINE;typedef struct evp_cipher_st EVP_CIPHER;
typedef struct evp_cipher_ctx_st
{
const EVP_CIPHER *cipher;
ENGINE *engine;
int encrypt;
int buf_len;unsigned char oiv[16];
unsigned char iv[16];
unsigned char buf[32];
int num;void *app_data;
int key_len;
unsigned long flags;
void *cipher_data;
int final_used;
int block_mask;
unsigned char final[32];
} EVP_CIPHER_CTX;typedef struct env_md_ctx_st EVP_MD_CTX;
typedef struct env_md_st EVP_MD;const EVP_MD *EVP_md5(void);
const EVP_MD *EVP_sha(void);
const EVP_MD *EVP_sha1(void);
const EVP_MD *EVP_sha224(void);
const EVP_MD *EVP_sha256(void);
const EVP_MD *EVP_sha384(void);
const EVP_MD *EVP_sha512(void);const EVP_CIPHER *EVP_aes_128_ecb(void);
const EVP_CIPHER *EVP_aes_128_cbc(void);
const EVP_CIPHER *EVP_aes_128_cfb1(void);
const EVP_CIPHER *EVP_aes_128_cfb8(void);
const EVP_CIPHER *EVP_aes_128_cfb128(void);
const EVP_CIPHER *EVP_aes_128_ofb(void);
const EVP_CIPHER *EVP_aes_128_ctr(void);
const EVP_CIPHER *EVP_aes_192_ecb(void);
const EVP_CIPHER *EVP_aes_192_cbc(void);
const EVP_CIPHER *EVP_aes_192_cfb1(void);
const EVP_CIPHER *EVP_aes_192_cfb8(void);
const EVP_CIPHER *EVP_aes_192_cfb128(void);
const EVP_CIPHER *EVP_aes_192_ofb(void);
const EVP_CIPHER *EVP_aes_192_ctr(void);
const EVP_CIPHER *EVP_aes_256_ecb(void);
const EVP_CIPHER *EVP_aes_256_cbc(void);
const EVP_CIPHER *EVP_aes_256_cfb1(void);
const EVP_CIPHER *EVP_aes_256_cfb8(void);
const EVP_CIPHER *EVP_aes_256_cfb128(void);
const EVP_CIPHER *EVP_aes_256_ofb(void);void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher,ENGINE *impl, unsigned char *key, const unsigned char *iv);int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,const unsigned char *in, int inl);int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher,ENGINE *impl, unsigned char *key, const unsigned char *iv);int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,const unsigned char *in, int inl);int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md,const unsigned char *salt, const unsigned char *data, int datal,int count, unsigned char *key,unsigned char *iv);
]]local ctx_ptr_type = ffi.typeof("EVP_CIPHER_CTX[1]")local hash
hash = {md5 = C.EVP_md5(),sha1 = C.EVP_sha1(),sha224 = C.EVP_sha224(),sha256 = C.EVP_sha256(),sha384 = C.EVP_sha384(),sha512 = C.EVP_sha512()
}
_M.hash = hashlocal cipher
cipher = function (size, _cipher)local _size = size or 128local _cipher = _cipher or "cbc"local func = "EVP_aes_" .. _size .. "_" .. _cipherif C[func] thenreturn { size=_size, cipher=_cipher, method=C[func]()}elsereturn nilend
end
_M.cipher = cipherfunction _M.new(self, key, salt, _cipher, _hash, hash_rounds)local encrypt_ctx = ffi_new(ctx_ptr_type)local decrypt_ctx = ffi_new(ctx_ptr_type)local _cipher = _cipher or cipher()local _hash = _hash or hash.md5local hash_rounds = hash_rounds or 1local _cipherLength = _cipher.size/8local gen_key = ffi_new("unsigned char[?]",_cipherLength)local gen_iv = ffi_new("unsigned char[?]",_cipherLength)if type(_hash) == "table" thenif not _hash.iv or #_hash.iv ~= 16 thenreturn nil, "bad iv"endif _hash.method thenlocal tmp_key = _hash.method(key)if #tmp_key ~= _cipherLength thenreturn nil, "bad key length"endffi_copy(gen_key, tmp_key, _cipherLength)elseif #key ~= _cipherLength thenreturn nil, "bad key length"elseffi_copy(gen_key, key, _cipherLength)endffi_copy(gen_iv, _hash.iv, 16)elseif salt and #salt ~= 8 thenreturn nil, "salt must be 8 characters or nil"endif C.EVP_BytesToKey(_cipher.method, _hash, salt, key, #key,hash_rounds, gen_key, gen_iv)~= _cipherLengththenreturn nilendendC.EVP_CIPHER_CTX_init(encrypt_ctx)C.EVP_CIPHER_CTX_init(decrypt_ctx)if C.EVP_EncryptInit_ex(encrypt_ctx, _cipher.method, nil,gen_key, gen_iv) == 0 orC.EVP_DecryptInit_ex(decrypt_ctx, _cipher.method, nil,gen_key, gen_iv) == 0 thenreturn nilendffi_gc(encrypt_ctx, C.EVP_CIPHER_CTX_cleanup)ffi_gc(decrypt_ctx, C.EVP_CIPHER_CTX_cleanup)return setmetatable({_encrypt_ctx = encrypt_ctx,_decrypt_ctx = decrypt_ctx}, mt)
endfunction _M.encrypt(self, s)local s_len = #slocal max_len = s_len + 16local buf = ffi_new("unsigned char[?]", max_len)local out_len = ffi_new("int[1]")local tmp_len = ffi_new("int[1]")local ctx = self._encrypt_ctxif C.EVP_EncryptInit_ex(ctx, nil, nil, nil, nil) == 0 thenreturn nilendif C.EVP_EncryptUpdate(ctx, buf, out_len, s, s_len) == 0 thenreturn nilendif C.EVP_EncryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 thenreturn nilendreturn ffi_str(buf, out_len[0] + tmp_len[0])
endfunction _M.decrypt(self, s)local s_len = #slocal buf = ffi_new("unsigned char[?]", s_len)local out_len = ffi_new("int[1]")local tmp_len = ffi_new("int[1]")local ctx = self._decrypt_ctxif C.EVP_DecryptInit_ex(ctx, nil, nil, nil, nil) == 0 thenreturn nilendif C.EVP_DecryptUpdate(ctx, buf, out_len, s, s_len) == 0 thenreturn nilendif C.EVP_DecryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 thenreturn nilendreturn ffi_str(buf, out_len[0] + tmp_len[0])
endreturn _M
OK.
Lua AES加解密相关推荐
- C语言实现AES加解密
C语言实现AES加解密 AES算法 具体代码 AES算法 (AES)RIJNDAEL算法是一个数据块长度盒密钥长度都可变的分组加密算法,其数据块长度和密钥长度都可独立地选定为大于等于128位且小于等于 ...
- Java code lib aes 加解密
Java aes 加解密 /*** Created by LvJianwei on 2018/2/8.*/import javax.crypto.Cipher; import javax.crypto ...
- 数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)...
近期刚做的一个项目.关于 Socket TCP 通信. 需求方提供了一个 ARM 机器,及数据採集器,须要我做一个服务端与数据採集器进行交互. 目的: 数据採集器:定时将读取到的数据发送到服务端. 服 ...
- 前端 crypto-js aes 加解密
背景 前段时间公司做项目,该项目涉及到的敏感数据比较多,经过的一波讨论之后,决定前后端进行接口加密处理,采用的是 AES + BASE64 算法加密~ 网上关于 AES 对称加密的算法介绍看上一篇! ...
- 记一次Java AES 加解密 对应C# AES加解密 的一波三折
最近在跟三方对接 对方采用AES加解密 作为一个资深neter Ctrl CV 是我最大的优点 所以我义正言辞的问他们要了demo java demo代码: public class EncryptD ...
- C语言实现AES加解密算法
C语言实现AES加解密算法 AES加解密 AES加解密 #include <stdio.h> #include <stdint.h> #include <memory.h ...
- openssl c++实现bouncycastle中AES加解密
0x01 为什么要用bouncycastle 先说说JCE(Java Cryptography Extension)是一组包,它们提供用于加密.密钥生成和协商以及 Message Authentica ...
- aes加密php源码,AES加解密类源码 · ThinkPHP5高阶实战教程 --诠释为API开发而生 · 看云...
# AES加解密类源码 > 根据网络整理 ~~~ /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017 ...
- Golang AES 加解密
文章目录 AES 简介 AES 加解密实现 小结 参考文献 AES 简介 利用 Go 提供的 AES 加解密与 Base64 编解码包,我们可以轻松实现 AES 加解密.实现之前,首先了解一下 AES ...
- aes js 加盐值 解密_cryptoJS AES 加解密简单使用
简单记录一下,前端利用 cryptoJS 如何加解密的.主要是关于 AES 加解密. 需求描述:需要对 url 中的参数进行 AES 解密,然后再把该参数进行 MD5 加密通过接口传递. AES AE ...
最新文章
- linux关闭涉及安全的服务,Linux中关闭不必要服务减少漏洞
- 第一届大数据科学与工程国际会议(2016)征文通知
- java中aop和aoc的区别_AOC与AOP的区别
- visionpro 图片格式转海康图片格式
- AUFN Carplay盒子固件级视频及图文教程!
- Centos8Web服务器搭建
- 思科Cisco BGP 专题(一) BGP基本概念
- 初探linux子系统集之led子系统(三)【转】
- 【增量学习】综述解析:A continual learning survey: Defying forgetting in classification tasks
- Excel文档误删的4种恢复方法,1秒就可以还原所有内容,你用过吗
- 30用一个例子解释mapping到底是什么
- 简单的Android端新闻App的实现。
- php ses 发送邮件,使用 Amazon SES API 和 AWS SDK for PHP 版本 3 验证电子邮件身份 - 适用于 PHP 的 AWS 开发工具包...
- GIT切换分支的简单操作
- SSM使用poi把Excel内容读取到数据库和把数据库内容导出到Excel
- 网络爬虫的原理和案例
- DXOMark是如何评价音频质量的
- android 随机验证码,Android自定义View实现随机验证码
- 三星s8 升级android9,三星S8官方国行版安卓9固件系统线刷升级包:CHC-G9500ZCS3DSF5...
- 梓紫的日记,第一篇出场人物介绍,刚开头的正文(一次性写不完)
热门文章
- 易语言c语言哪个做游戏脚本,游戏简易脚本制作教程
- macOS Catalina 10.15.7 ISO/CDR 虚拟机镜像下载
- 3mx转osgb_OSGB格式的三维倾斜摄影文件转化成ARCGIS栅格文件
- idea 打包不出现target的原因
- STM32串口通信详解
- div css3 border-radius 之圆角 div圆角 图片圆角
- Windows系统与虚拟机共享文件夹,映射的磁盘显示“信号灯超时”
- 关于filenet中的folder以及document简单操作
- matlab 二元函数的极限,利用MATLAB软件求解一元和二元函数的极值
- 三段式状态机的原理/代码/仿真,1001序列检测实例Verilog