当前位置: 首页 > news >正文

openresty开发lua-resty-openssl之对称加密解密 - liuxm

openresty开发lua-resty-openssl之对称加密解密

local cipher = require "resty.openssl.cipher"
local rand = require "resty.openssl.rand"
local pkey = require "resty.openssl.pkey"-- cbc需要32位密钥随机生成32位秘钥
local random_bytes = rand.bytes(32)-- 加密过程
local function aes_encrypt(data, key)-- 创建 AES-256-CBC 加密对象local c, err = cipher.new("aes-256-cbc")if not c then return nil, err end-- 生成随机 IV(AES-CBC 模式需要 16 字节 IV)local iv = rand.bytes(16)if not iv then return nil, "生成 IV 失败" end-- 初始化加密上下文local ok, err = c:init(key, iv, {is_encrypt=true}) -- true 表示加密模式if not ok then return nil, err end-- 加密数据(需处理填充)local encrypted, err = c:final(data)if not encrypted then return nil, err end-- 返回 IV + 加密后的数据(解密时需要 IV)return iv .. encrypted
end-- 解密过程
local function aes_decrypt(encrypted_data, key)-- 提取 IV(前 16 字节)local iv = encrypted_data:sub(1, 16)local data = encrypted_data:sub(17)-- 创建解密对象local c, err = cipher.new("aes-256-cbc")if not c then return nil, err end-- 初始化解密上下文local ok, err = c:init(key, iv, {is_encrypt=false}) -- false 表示解密模式if not ok then return nil, err end-- 解密数据local decrypted, err = c:final(data)if not decrypted then return nil, err endreturn decrypted
end-- 加密数据
local encode_bin = aes_encrypt("hello",random_bytes)-- 输出解密结果
ngx.say(aes_decrypt(encode_bin,random_bytes))

 

http://www.rkmt.cn/news/22285.html

相关文章:

  • 腾讯云 OpenCloudOS 8 docker安装
  • 哈希乱搞:CF1418G Three Occurrences
  • 悲伤 自卑 乖戾 独自哭泣 陪伴空虚 kill my memory 让我将痛苦全忘记
  • 工程师的 “指尖实验室”!正点原子 LT1 电桥镊子深度测评:同价位竞品谁能打?
  • 破解跨域监控难题:国标GB28181算法算力平台EasyGBS视频调阅技术在跨域安防监控中的核心应用
  • 2025 年电缆桥架源头厂家最新推荐排行榜:聚焦优质供应商核心竞争力,助力工程采购精准选型
  • 2025年交通杯-爆破题wp
  • 挖象浏览器下载安装教程|支持淘宝、拼多多、抖音多平台账号分区管理
  • 2025 年国内活性炭回收交易公司最新推荐排行榜:实力厂商深度解析,助力企业精准选合作方回收果壳活性炭/回收煤质柱状活性炭/库存各种活性炭公司推荐
  • 2025-10-15 CSP-J 模拟赛 赛后总结【ZROI】
  • Oracle故障处理:轻松搞定ORA-01190报错
  • EAS_接口新增单据提示没有组织单据新增权限
  • 全自动红外测油仪厂家推荐/国产红外测油仪品牌推荐/靠谱供应商采购推荐
  • 一对一直播源码搭建:后来者的源码选择与专业研发的关键考量
  • 总氮检测仪靠谱供应商,总氮水质分析仪厂家推荐,总磷/氨氮/COD等仪器哪家好?
  • 多领域对话自动评估技术突破
  • 直面挑战:MySQL 千万级数据高性能优化实战指南
  • 常见的名词
  • CF2155 Codeforces Round 1056 (Div. 2) 游记(VP)
  • 【隐语SecretFlow社区】万字长文解读构建可信数据空间相关标准
  • 编程计算定投黄金的收益率
  • 客户管理软件是什么?深度解析及标杆产品推荐
  • uni-app x开发商城系统,tabBar
  • 组织研磨仪厂家品牌推荐/知名品牌,组织研磨仪哪家好?
  • C# SerialPort send and receive full example
  • 自监督学习在医疗AI中的科技达成路径分析(中)
  • 进口微量粘度计代理商推荐,优质供应商分享
  • Apache Doris 内部数据裁剪与过滤机制的完成原理
  • 阿里面试:Redis挂了怎么办?集群 节点挂,怎么 恢复数据? 多长时间 的数据 可能 丢失?
  • 2025年石墨干燥机厂家推荐榜:真空干燥机/振动流化床干燥机/闪蒸干燥机高效环保成主流,这家企业凭实力登顶