兄弟,问题的关键在于加密和解密算法的一致性,我刚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写道:
想问问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>