1 、openresty github 官方示例
https://github.com/openresty/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))
2 、自己编写 lua 测试
local aes = require "resty.aes"
local str = require "resty.string"
local aes_128_cbc_md5 = aes:new("gOxiO7IIRthJ406X")
-- 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("hello")
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))
-- 输出结果
AES 128 CBC (MD5) Encrypted HEX: de6641e8a49ef1911a10d9ec88cc477b
AES 128 CBC (MD5) Decrypted: hello
3 、java 实现
@Test
public void testEncryptText() {
String key = "gOxiO7IIRthJ406X";
byte[] iv = new byte[16];
String text = "hello";
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, SecureUtil.md5(key).getBytes(), iv);
System.out.println(aes.encryptHex(text));
// 输出:446d1192d40aa0d05e3c30392ac43ec3
aes = new AES(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv);
System.out.println(aes.encryptHex(text));
// 输出:190ede3e1359a6e4d8ecf38c8f4bce63
}