25.12.2020, 14:08
Try this function:
Use this like this:
Code:
function receive(sock)
local iv = sock:receive(16)
if not iv then
return nil, 'cannot receive iv'
end
local aes_128_ecb, err = aes:new(key, nil, aes.cipher(128, 'ecb'), { iv = string.rep('\0', 16) }, nil, 0)
local ivdec = aes_128_ecb:decrypt(iv)
local aes_128_cbc, err = aes:new(key, nil, aes.cipher(128, 'cbc'), { iv = ivdec }, nil, 0)
local buf = ''
sock:settimeout(0.1)
while true do
local data, err = sock:receive(16)
if data then
buf = buf .. data
else
break
end
end
sock:settimeout(3)
if #buf > 0 then
return aes_128_cbc:decrypt(buf)
else
return nil, 'receive failed'
end
end
Use this like this:
Code:
reply, err = receive(sock)
log(reply, err)