Q:

Decryption of AES 256 CBC With iv & PKCS7Padding Encrypted String (Encrypted using Java)

I am new to LUA and Cryptography. I am trying to decrypt a string in Lua which was encrypted in Java using AES 256 CBC with an IV and PKCS7.
I came across resty.aes. But I am unable to decrypt the string using this. I am getting nil.
Here is the sample example:
CODE I TRIED IN LUA:

local aes = require "resty.aes"
local str = require "resty.string"

--- Plain text data: 'hello123uvsayb1b1'
local encryptedData = '6d6b2082fb9596a4a731b43c9e6f5d63556dfa7a30dc5d7284c82dbfa43523c8'

local key = "pIyOhraBZhkELJo1DAfWabababababab"
local aes256Cbc, err = aes:new(key, nil, aes.cipher(256, "cbc"), {iv="bd7e9fdeb5d8403c"})

if(err ~= nil) then
  ngx.say("Some error occurred")
end

local decrypted_data, err = aes256Cbc:decrypt(encryptedData)

if decrypted_data == nil then
  ngx.say("Decrypted data is nil")
end

The output is: Decrypted data is nil.
I am unable to find out what am I doing wrong here. 

A:

Your encrypted data is hex-encoded. You need to decode it to raw bytes string:
local function fromhex(str)
    return (str:gsub('..', function (cc)
        return string.char(tonumber(cc, 16))
    end))
end

... 

local encryptedData = fromhex('6d6b2082fb9596a4a731b43c9e6f5d63556dfa7a30dc5d7284c82dbfa43523c8')

...

ngx.say(decrypted_data)

returns hello123uvsayb1b1

