-2

更新:我终于设法按照第三方服务的要求重新创建了整个 Java 代码。我必须补充一点,一些使用的库已被弃用,但我无能为力,因为那是对方正在使用的,我必须遵守。

Java 代码

   SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),
    "AES");
   Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   cipher.init(1, secretKeySpec);

   byte[] aBytes = cipher.doFinal(inputString.getBytes());

输入键:xxxxxxxxyyyyyyyy
输入文本:maryhadalittlelamb

输出:Z22GETg3Anl92%2BoyqdVWs9haQveaZxkDn8sQYP08iCY%3D

node.js 代码

var cipher = crypto.createCipher('aes-128-ecb', key);
var encryptedPassword = cipher.update(text, 'utf8', 'base64');
encryptedPassword += cipher.final('base64');
console.log(encryptedPassword);

输入键:xxxxxxxxyyyyyyyy
输入文本:maryhadalittlelamb

输出:mnqrpA2eqAhmseTrkBtH3YSGMoFs+ECPUamVd8/bgAQ=

相同输入字符串和键的输出对于两者都是不同的。事实上,node.js 是不同的,但 base64 看起来还是一样的。

我对这些东西还很陌生,因此我已经失去了可能。

4

1 回答 1

0

在 node.js 中,您在加密之前对输入字符串进行 base64 编码,它需要是需要进行 base64 编码的加密输出

此外,您需要调用cipher.final(..)aftercipher.update(..)来完成加密操作。请记住捕获两者的输出。

除此之外,请注意ECB 模式是不安全的

于 2016-06-22T04:55:39.013 回答