github lua-string-resty地址:
https://github.com/openresty/lua-resty-string
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))
使用openresty的aes加密,採用的補齊方式如下:
如果不滿足16字節的整倍數,差幾個,就補齊\0x幾
例如: vpn12345vpn,共11個,還差5個,則後面補齊5個\0x5
如果是16的整倍數,則補齊16個\0x10,
固上述aes加密後的密文解密如下:
from Crypto.Cipher import AES
import base64
def decryptByKey(key, orgtext, iv):
pad = ["\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x10", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f"]
orgtext = base64.b64decode(orgtext)
decryptor = AES.new(key, AES.MODE_CBC, iv)
result = decryptor.decrypt(orgtext)
key = result[-1:]
if key in pad:
result = result.rstrip(key)
return result
參考鏈接:
https://groups.google.com/forum/#!topic/openresty/syqjf7C8O74