使用lua-string-resty的aes加密解密,无法和python的aes进行对接

3,056 views
Skip to first unread message

梅益城

unread,
Mar 1, 2016, 2:50:26 AM3/1/16
to openresty, open...@googlegroups.com

目前想使用aes,客户端传过来一个加密之后的字符串,然后在nginx中使用 lua aes解密,进行对称验证

使用同样的key, iv和mode,用python代码生成的机密字符串,在lua中无法解密,导致无法对接

lua代码:

local aes = require "resty.aes"
local str = require "resty.string"

local iv = "78afc8512559b62f"
local key = "78afc8512559b62f"
local text = "c6d1965bf800d5f7682636826c9a097e"

local aes_128_cbc_with_iv = assert(aes:new(key, nil, aes.cipher(128, "cbc"), {iv=iv, method=nil})
local encrypted = ngx.encode_base64(aes_128_cbc_with_iv:encrypt(text))

ngx.log(ngx.ERR, "#####encrypted: " .. encrypted)
ngx.log(ngx.ERR, "#####decrypted: " .. aes_128_cbc_with_iv:decrypt(ngx.decode_base64(encrypted)))

输出:
#####encrypted: zzZ15s2DIzkebVl47TLaMEwbwcznOg4HVsDGC1h8q4AE1qDglp6P3seQF1jyJXAl
#####decrypted: c6d1965bf800d5f7682636826c9a097e



python代码:


from Crypto.Cipher import AES
import base64

def pad_text16(s):
return s + (16 - len(s) % 16) * '\0'

def encryptByKey(key, orgtext, iv):
encryptor = AES.new(key, AES.MODE_CBC, iv)
result = encryptor.encrypt(pad_text16(orgtext))
return base64.b64encode(result)

def decryptByKey(key, orgtext, iv):
orgtext = orgtext.replace(' ', '+')
orgtext = base64.b64decode(orgtext)
decryptor = AES.new(key, AES.MODE_CBC, iv)
result = decryptor.decrypt(orgtext)
return result.rstrip('\0')

if __name__ == "__main__":
iv = "78afc8512559b62f"
orgtext = "c6d1965bf800d5f7682636826c9a097e"
encrypted = encryptByKey(iv, orgtext, iv)

print '#####encrypted: ', encrypted
print '#####decrypted: ', decryptByKey(iv, encrypted, iv)


输出:
#####encrypted:  zzZ15s2DIzkebVl47TLaMEwbwcznOg4HVsDGC1h8q4Cawc/553a+CyHEUSGABK26
#####decrypted:  c6d1965bf800d5f7682636826c9a097e

两种语言加密出来的字符串再使用base64之后,不一样,有特地请公司的ios小伙伴使用OC去进行同样规则的加密,得到的结果是和python的结果相同,

所以想请教一下大家,是什么地方使用错误



lhmwzy

unread,
Mar 1, 2016, 8:39:01 AM3/1/16
to open...@googlegroups.com
我想问题出在这里
def pad_text16(s):
return s + (16 - len(s) % 16) * '\0'


它在字符末尾加了东西
你在要LUA中也实现类似的东西,要不然,加密的字符串实际上是不一样的





--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

梅益城

unread,
Mar 1, 2016, 11:51:28 AM3/1/16
to openresty
已根据你的建议,在lua中增加同样的方法,但貌似还是无效

function fill_padding(text)
local text_length = string.len(text)
local left = text_length - math.floor(text_length / 16) * 16

local fill_length = 16 - left
local fill = ""
if left ~= 0 then
for i = 1, fill_length do
fill = fill .. '\0'
end
end
return text .. fill
end

在 2016年3月1日星期二 UTC+8下午9:39:01,lhmwzy写道:

wd

unread,
Mar 1, 2016, 9:16:20 PM3/1/16
to open...@googlegroups.com
你这个应该就是 padding 的差异问题,我遇到过类似问题,解决思路就是自己手动处理补全,加解密的时候处理好。

lhmwzy

unread,
Mar 1, 2016, 9:20:27 PM3/1/16
to open...@googlegroups.com
好吧,G了一下,openssl默认的padding是PKCS7 见https://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7,lua-resty-string中调用的是openssl的库,所以加的padding也是和openssl一样的,https://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7,那么在python中,padding也要设置成PKCS7,见https://gist.github.com/chrix2/4171336

这样,加密后的输出就一样了

梅益城

unread,
Mar 2, 2016, 1:17:15 AM3/2/16
to openresty
是的,根据@lhmwzy的建议,现在没问题了

在 2016年3月2日星期三 UTC+8上午10:16:20,wd写道:

梅益城

unread,
Mar 2, 2016, 1:17:41 AM3/2/16
to openresty
非常感谢,根据你的建议,现在已经可以对接上~~


在 2016年3月2日星期三 UTC+8上午10:20:27,lhmwzy写道:
Reply all
Reply to author
Forward
0 new messages