使用iv和PKCS7Padding加密字符串解密AES 256 CBC(使用Java加密)

问题描述 投票:0回答:1

我是LUA和密码学的新手。我正在尝试解密Lua中的字符串,该字符串在Java中使用IV和PKCS7使用AES 256 CBC进行了加密。我遇到了resty.aes。但是我无法使用此解密字符串。我收到nil

这里是示例示例:

key: pIyOhraBZhkELJo1DAfWabababababab
encrypted_data: 6d6b2082fb9596a4a731b43c9e6f5d63556dfa7a30dc5d7284c82dbfa43523c8
iv: substring of length 16 from sha256(key)

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

输出为:解密数据为零。

我无法找出我在这里做错了什么。

encryption lua openssl aes openresty
1个回答
2
投票

您的加密数据是十六进制编码的。您需要将其解码为原始字节字符串:

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)

返回hello123uvsayb1b1

© www.soinside.com 2019 - 2024. All rights reserved.