【发布时间】:2019-08-11 06:05:21
【问题描述】:
我有用于加密的java代码
public String encrypt() throws Exception {
String data = "Hello World";
String secretKey = "j3u8ue8xmrhsth59";
byte[] keyValue = secretKey.getBytes();
Key key = new SecretKeySpec(keyValue, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(StringUtils.getBytesUtf8(data));
String encryptedValue = Base64.encodeBase64String(encVal);
return encryptedValue;
}
它返回与tool here 相同的值(eg5pK6F867tyDhBdfRkJuA==)
我将代码转换为 Nodejs(加密)
var crypto = require('crypto')
encrypt(){
var data = "Hello World"
var cipher = crypto.createCipher('aes-128-ecb','j3u8ue8xmrhsth59')
var crypted = cipher.update(data,'utf-8','base64')
crypted += cipher.final('base64')
return crypted;
}
但这给出了不同的值( POixVcNnBs3c8mwM0lcasQ== )
如何从两者获得相同的价值? 我错过了什么?
【问题讨论】:
-
See the param name and paras 4 to 6 of the documentation。使用 createCipheriv,即使 ECB 的 IV 为空 (
new Buffer(0))。此外,使用一小组可打印字符作为密钥是很弱的,并且使用 ECB 通常是不安全的,但这些是不同堆栈的主题。
标签: java node.js aes cryptojs ecb