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



背景: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是一个参考数字: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('', 32-#key), nil,
aes.cipher(256,"cbc"), { iv = '' })
ngx.say(str.to_hex(aes_java:encrypt('111111')))
-- output: 740f0edd2f00a64cf2a42b9d5458f964

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

最新更新