gpt4 book ai didi

java - 如何将Java AES ECB加密代码转换为Nodejs

转载 作者:行者123 更新时间:2023-12-02 01:47:24 24 4
gpt4 key购买 nike

我有用于加密的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 相同的值(例如5pK6F867tyDhBdfRkJuA==) enter image description here

我将代码转换为 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== )

如何从两者获得相同的值?我错过了什么?

最佳答案

感谢戴夫,现在我在 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 - 如何将Java AES ECB加密代码转换为Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com