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

标签 java node.js aes cryptojs ecb

我有用于加密的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/

相关文章:

Node.js JWT,从token获取user_id

java - 波动发生在澄清/误解之前?

java - Spring Aspect 未在测试环境中运行

java - 将字符串日期转换为字符串日期不同格式

Java 选择已绘制的形状,并在其周围绘制矩形

node.js - async/await 函数未按正确顺序执行

node.js - 检查mongoDB中的bulk是否为空

mysql - 是否有 AES 的 --des-key-file 等价物?

javascript - 用Java重现加密方法

java - 为什么我总是得到-1作为in.read(buf)的值?