25.04.2019, 10:20
There are two issues here that cause data errors:
1. aes.lua hashes the secret key
2. PHP uses different padding for secret key and data.
Solution:
1. Use this aes library which supports turning off padding: https://github.com/openresty/lua-resty-s...ty/aes.lua
2. Add library loading as mentioned in this post: https://forum.logicmachine.net/showthrea...7#pid10237
Working example for decrypt:
1. aes.lua hashes the secret key
2. PHP uses different padding for secret key and data.
Solution:
1. Use this aes library which supports turning off padding: https://github.com/openresty/lua-resty-s...ty/aes.lua
2. Add library loading as mentioned in this post: https://forum.logicmachine.net/showthrea...7#pid10237
Working example for decrypt:
Code:
function pad(str, bits)
local bytes = bits / 8
local rem = #str % bytes
if rem > 0 then
str = str .. string.rep('\0', bytes - rem)
end
return str
end
encdec = require('encdec')
aes = require('aes')
key = encdec.base64dec('SsqJ1jifeTA+K1igNwzX9/jx1WDkXGbAY0hiwYTduvc=')
data = encdec.base64dec('iKIpbQcvg5kq8Ib9KTrvpAkKy55uh5CET96kqzqGBLWriHG6c2FMiIXrr0NMxr71To7tJvmAWGUlpZtYXmnz/API/PKLsWSMsLBuQEqN63GQZ3kdFOM5eXF0633dsaJ3')
hash = { iv = string.rep('\0', 16) } -- no hashing method for key
aes_256_ecb, err = aes:new(pad(key, 256), nil, aes.cipher(256, 'ecb'), hash, nil, 0)
res = aes_256_ecb:decrypt(pad(data, 128)) -- data block size is always 128 bits
log(res)