本文介绍了如何将Java AES ECB加密代码转换为Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Java中有用于加密的代码
I have code in java for encryption
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;
}
它返回与
我将代码转换为Nodejs(密码)
I converted the code to Nodejs (crypto)
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 ==)
But this is giving a different value ( POixVcNnBs3c8mwM0lcasQ== )
如何从两者中获得相同的价值?我想念什么?
How to get same value from both?What am I missing?
推荐答案
现在感谢dave我对于Java和Nodejs/JavaScript都得到了相同的结果
Thanks to dave now I am getting same result both for Java and 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 AES ECB加密代码转换为Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!