关于lua-resty-string和Google的aes.js交互问题,请解答。

627 views
Skip to first unread message

Chan WingChung

unread,
Nov 2, 2014, 10:08:27 AM11/2/14
to open...@googlegroups.com
想问问aes.js加密和解密的数据怎么通过lua-resty-string正确解出来。
lua-resty-string:
local aes = require "resty.aes"
    local str = require "resty.string"
    local aes_128_cbc_md5 = aes:new("AKeyForAES")
        -- the default cipher is AES 128 CBC with 1 round of MD5
        -- for the key and a nil salt
    local encrypted = aes_128_cbc_md5:encrypt("Secret message!")
    ngx.say("AES 128 CBC (MD5) Encrypted HEX: ", str.to_hex(encrypted))
    ngx.say("AES 128 CBC (MD5) Decrypted: ", aes_128_cbc_md5:decrypt(encrypted))
http://code.google.com/p/crypto-js/#AES
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
   
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");

   
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
</script>

Nero.Ping

unread,
Nov 11, 2014, 3:15:54 AM11/11/14
to open...@googlegroups.com

我 也 遇到 这个问题了,我不知道从页面穿过来的字符串怎么去解密,我把
str.to_hex(encrypted)
传到页面后,再从页面把这个字符串传回来,我不知道怎么处理才能用

aes_128_cbc_md5:decrypt(str_from_page)
来解开它.

lhmwzy

unread,
Nov 11, 2014, 3:46:07 AM11/11/14
to open...@googlegroups.com
<script>
    var encrypted = CryptoJS.AES.encrypt("11", "11");
    var decrypted = CryptoJS.AES.decrypt(encrypted, "11");
    alert(decrypted);
</script>
显示解密结果为3131,为何?

--
--
邮件来自列表“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

lhmwzy

unread,
Nov 11, 2014, 3:49:43 AM11/11/14
to open...@googlegroups.com
是显示的字符的ascii码。。。

Nero.Ping

unread,
Nov 11, 2014, 6:01:08 AM11/11/14
to open...@googlegroups.com
搞了一下午终于找到了解法,我把服务端的秘文用
ngx.encode_base64(encrypted)
送到前端, 然后在从前端把秘文送会后端时,再用
aes_128_cbc_md5:decrypt(ngx.decode_base64(str_from_page))
就能获取原文了,对于 Chan WingChung兄弟的用例,我猜在js端将秘文进行base64编码应该能够在后端解开。

Chan WingChung

unread,
Nov 12, 2014, 12:19:32 PM11/12/14
to open...@googlegroups.com
估计大家误解我的意思了,我是说比如前端是html,客户使用aes后通过args之类传到后端使用lua-resty-string进行解密。问题是crypto-js到后端的数据用了ngx.decode_base64之后用lua-resty-string无法解密。

在 2014年11月11日星期二UTC+8下午7时01分08秒,Nero.Ping写道:

Nero.Ping

unread,
Nov 18, 2014, 2:44:08 AM11/18/14
to open...@googlegroups.com
兄弟,问题的关键在于加密和解密算法的一致性,我刚google一把,借鉴那些C#, Java的例子,今天测试通过了:

            var key = CryptoJS.MD5("my key");
            var iv = CryptoJS.MD5("my IV");
            encrypted = CryptoJS.AES.encrypt("message here我!", key, {iv: iv});

把encrypted.toString()传到ngx_lua的内容处理器中,

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

local args = ngx.req.get_uri_args()
local md5 = resty_md5:new()

md5:update("my key")
local digest = md5:final()
local aes_iv_key = digest
md5 = resty_md5:new()
md5:update("my IV")
digest = md5:final()
local aes_iv_val = digest
local aes_iv = aes:new(aes_iv_key, nil, aes.cipher(128,"cbc"), {iv=aes_iv_val})
ngx.say(aes_iv:decrypt(ngx.decode_base64(args.destr)))

我的测试已经 通过了,我的代码凌乱了些,你自己改改 吧,重要的是你要用

AES 128 CBC with IV and no SALT

这个东东(我也不太懂CBC和IV是个啥).


在 2014年11月2日星期日UTC+8下午11时08分27秒,Chan WingChung写道:

Chan WingChung

unread,
Nov 18, 2014, 10:47:42 AM11/18/14
to open...@googlegroups.com
看来是不能加盐,一加盐就有问题。

在 2014年11月18日星期二UTC+8下午3时44分08秒,Nero.Ping写道:
Reply all
Reply to author
Forward
0 new messages