如何将 Java AES ECB 加密代码转换为 Nodejs
How to convert Java AES ECB Encryption Code to Nodejs
我在 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;
}
它 returns 与 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==)
如何从两者中获得相同的价值?
我错过了什么?
感谢 dave 现在我得到了 Java 和 Nodejs/JavaScript
相同的结果
var crypto = require('crypto')
encrypt(){
var data = "Hello World"
var iv = new Buffer(0);
const key = 'j3u8ue8xmrhsth59'
var cipher = crypto.createCipheriv('aes-128-ecb',new Buffer(key),new Buffer(iv))
var crypted = cipher.update(data,'utf-8','base64')
crypted += cipher.final('base64')
return crypted;
}
我在 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;
}
它 returns 与 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==)
如何从两者中获得相同的价值? 我错过了什么?
感谢 dave 现在我得到了 Java 和 Nodejs/JavaScript
相同的结果var crypto = require('crypto')
encrypt(){
var data = "Hello World"
var iv = new Buffer(0);
const key = 'j3u8ue8xmrhsth59'
var cipher = crypto.createCipheriv('aes-128-ecb',new Buffer(key),new Buffer(iv))
var crypted = cipher.update(data,'utf-8','base64')
crypted += cipher.final('base64')
return crypted;
}