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

背景:java端有一套现成的代码,现在需要迁移到lua。
测试时:使用的key为同一个

key = "1938703285589872452";

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.str_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/...
image.png


我只能看出cbc、pkcs7Padding这几个信息,现在结果值完全对不上。
image.png
没有方向,还望各位指导一下,谢谢啦。

阅读 5.6k
1 个回答

AES-256-CBC encryption with PKCS7 padding.

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

local aes_java = aes:new(key .. string.rep('\0', 32-#key), nil,
  aes.cipher(256,"cbc"), { iv = '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' })
ngx.say(str.to_hex(aes_java:encrypt('111111')))
-- output: 740f0edd2f00a64cf2a42b9d5458f964
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题