lua-resty-string icon indicating copy to clipboard operation
lua-resty-string copied to clipboard

Function missing to convert stored hex value back to Ascii for decrypt function

Open doktoroblivion opened this issue 7 years ago • 1 comments

The functions you have are very good, but they make the assumption that if you want to encrypt and decrypt you will always have the encrypted ascii value (non-converted around). If you have to store this value into a cache table or dictionary there is no existing function (aside from atoi) to convert it back to something the decrypt function will take. So, there are two options:

  • provide a new function to convert any stored value back to ascii
  • change the decrypt function to handle hex and other data primitives

The code that I have had to use to invoke decrypt from a stored value. cache_key is supplied by an OS envvar.

local cache_key = os.getenv("ENCRYPT_KEY");

-- hex to char conversion, used to perform substitution
local c_table = {};
for jj = 0, 255 do
    c_table[("%02X"):format(jj)] = string.char(jj);
    c_table[("%02x"):format(jj)] = string.char(jj);
end

-- decryptIt function
local function decryptIt(value)
    local aes_128_cbc_md5 = aes:new(cache_key);
    return aes_128_cbc_md5:decrypt(value);
end

-- convert to ascii
local function convertAscii(value)
    return value:gsub("(..)", c_table);
end


local value = encryptIt(init_value);
.
. 
ngx.header['Set-Cookie'] = { "hash_val=" .. str.to_hex(value) ..   }
.
. ( a lot of code and reinvoke of proxy, etc... )
.
local url = ngx.var['cookie_hash_val'];
local d_crypted = decryptIt(convertAscii(url));

I think I have the above correct, but if you have any questions let me know.

doktoroblivion avatar Jul 10 '18 13:07 doktoroblivion

same problem here.

rocky1001 avatar Apr 23 '19 07:04 rocky1001