Lua和Java计算的AES加密结果不一致

The AES encryption results calculated by Lua and Java are inconsistent

背景:java有一套现成的代码,现在需要迁移到lua

测试时:使用相同的密钥

key = "1938703285589872452";

data = "111111";

1.java的加密代码

pom

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.55</version>
    </dependency>

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.55</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>

代码

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

// ... 

// 加密方法
    public static String encryptWithBC(String data, String key) throws Exception {
        // key
        ByteBuffer keyBuffer = ByteBuffer.allocate(32);
        keyBuffer.put(key.getBytes());
        KeyParameter keyParameter = new KeyParameter(keyBuffer.array());
        // 请求数据
        byte[] dataByteArr = data.getBytes(StandardCharsets.UTF_8);

        // init
        CBCBlockCipher aes = new CBCBlockCipher(new AESEngine());
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding());
        cipher.init(true, keyParameter);

        byte[] output = new byte[cipher.getOutputSize(dataByteArr.length)];
        int len = cipher.processBytes(dataByteArr, 0, dataByteArr.length, output, 0);
        cipher.doFinal(output, len);

        return Base64.encodeBase64String(output);
    }

2.lua的加密代码

-- AES加密
local aes = require "resty.aes"

-- ...

-- 加密方法
function _M.encrypt_128_cbc_pkcs7(en_data, aes_key)
    --local aes_256_cbc_with_padding = aes:new(key, nil, aes.cipher(256,"cbc"), {iv = string.sub(key, 1, 16)}, nil, nil, enable_padding)
    local aes_128_cbc_pkcs7 = aes:new(aes_key, nil, aes.cipher(128, "cbc"), nil, nil, nil, "PKCS7")
    local encrypted = aes_128_cbc_pkcs7:encrypt(en_data)
    -- 转base64
    local encrypted_base64 = wkg_hex_utils.str_to_base64(encrypted)
    local encrypted_hex = wkg_hex_utils.base64_to_hex(encrypted_base64)


    wkg_log_utils.log("AES加密结果(BASE64): {}", encrypted_base64)
    wkg_log_utils.log("AES加密结果(Hex): {}", encrypted_hex)
    return encrypted_base64
end

Lua 是参考 git:https://github.com/openresty/lua-resty-string

只能看到cbc和pkcs7Padding的信息,现在结果值完全错误

lua的结果:

111111
X32vI7ROqoK3hjQ9fvrOKg==
5F7DAF23B44EAA82B786343D7EFACE2A

java的结果:

111111
dA8O3S8ApkzypCudVFj5ZA==
740F0EDD2F00A64CF2A42B9D5458F964

两种语言对base64的处理是相似的。

java:

    public static String base64ToHex(String base64Str) {
        byte[] decode = Base64.decode(base64Str);
        return byteArrToHex(decode).toUpperCase(Locale.ROOT);
    }

lua:

    function wkg_hex_utils.base64_to_hex(base64_str)
    local temp = ngx.decode_base64(base64_str)
    wkg_log_utils.log("base64解码类型: {},值: {}", type(temp), temp)
            return wkg_hex_utils.str_to_hex(temp)
    end

没有方向。我希望你能给我一些指导。谢谢。


现在我正在尝试将加密结果转换为byteArr。他们是这样的

java:

[116, 15, 14, -35, 47, 0, -90, 76, -14, -92, 43, -99, 84, 88, -7, 100]

lua:

[95, 125, 175, 35, 180, 78, 170, 130, 183, 134, 52, 61, 126, 250, 206, 42]

有人可以帮我解答一下吗?谢谢

首先我想告诉你,你在 Java 中所做的是使用 PKCS7 填充的 AES-256-CBC 加密。数字 256 是以位为单位的密钥长度,这意味着您在 Java 代码中使用的 32 个字节。

local aes = require "resty.aes"
local str = require "resty.string"
local key = '1938703285589872452'

local aes_java = aes:new(key .. string.rep('[=10=]', 32-#key), nil,
  aes.cipher(256,"cbc"), { iv = '[=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=]' })
ngx.say(str.to_hex(aes_java:encrypt('111111')))
-- output: 740f0edd2f00a64cf2a42b9d5458f964

由于您的密钥比要求的 32 字节短,我们需要附加零以获取 AES-256-CBC 加密中使用的真实密钥。还有一点,必须指定 iv 向量才能实现 PKCS7 填充。在 Java 中,我认为 PaddedBufferedBlockCipher 会为您生成默认的 iv 向量,但是对于 Lua,您必须将默认的 iv 向量传递给 aes:new