本文介绍了Javascript / NodeJS等效代码为Java代码Cipher.doFinal(byte [])?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些服务器端Java代码迁移到新的NodeJS服务器。
我正在寻找一个等价的方法调用JavaScript到Java的Cipher.doFinal(byte [])
请注意,我不能使用NodeJS缓冲区,因为他们不支持负字节值。所以要做我的加密,我需要一个接受正数和负数数组的方法。

I am migrating some server-side Java code to a new NodeJS server.I am looking for an equivalent method call in Javascript to Java's Cipher.doFinal(byte[])Note that I can't use NodeJS Buffers because they don't support negative Byte values. So to do my encryption, I'll need a method that accepts an array of positive and negative numbers.

这里是我目前有关的所有与这个问题有关的:

Here's all of what I currently have that is related to this problem:

Node JS / Javascript:

var crypto = require('crypto');
var cipher = crypto.createCipher('aes256',key);

Node JS / Javascript:
var crypto = require('crypto'); var cipher = crypto.createCipher('aes256',key);

Java(javax.crypto.Cipher) / p>

Java (javax.crypto.Cipher):

Cipher cipher;
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
try {
    cipher = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException e) {
}try {
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
} catch (InvalidKeyException e) {
}



,我调用这个方法,其中Iv表示初始化向量:
byte [] newIv = cipher.doFinal(myIv);

如何在JavaScript中获得与doFinal Java方法中相同的结果?

How can I get the same result in JavaScript as I do in the doFinal Java method?

推荐答案

你可以放置一个空IV如下:

Turns out you can place an empty IV as follows:

var cipher = require('crypto')。createCipheriv('aes-256'ecb', key,'');

对于替换方法,只需将旧的IV临时存储为新的IV,然后尝试更新新的IV使用旧的。下面是在NodeJS中使用创建为缓冲区的Initialization Vectors上的一些代码的样子:


var newIV = oldIV.slice();
newIV = cipher.update(newIV);
oldIV = newIV;

As for the replacement method, simply store your old IV temporarily as a new IV and then attempt to update that new IV using the old one. Here's how it would look like in NodeJS using some of the above code on Initialization Vectors created as buffers:
var newIV = oldIV.slice();newIV = cipher.update(newIV); oldIV = newIV;

这篇关于Javascript / NodeJS等效代码为Java代码Cipher.doFinal(byte [])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:52