在 Java 中从 node.js 中解密字符串?

Decrypting strings from node.js in Java?(在 Java 中从 node.js 中解密字符串?)
本文介绍了在 Java 中从 node.js 中解密字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 node.js 中运行了以下简单的加密代码:

I have the following simple encryption code running in node.js:

var crypto = require('crypto');

var encKey = "FOO"; // Not the real key. Assume it works though.

var encrypt = function(str) {
  var cipher = crypto.createCipher('aes-256-cbc', encKey);
  var crypted = cipher.update(str, 'utf-8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
};

我也可以解密如下:

var crypto = require('crypto');

var encKey = "FOO"; // Not the real key. Assume it works though.

var decrypt = function(str) {
  var decipher = crypto.createDecipher('aes-256-cbc', encKey);
  var decrypted = decipher.update(str, 'hex', 'utf-8');
  decrypted += decipher.final('utf-8');
  return decrypted;
};

这一切都很好.字符串按预期加密和解密.但现在我面临着用 Java 从这个 node.js 代码中解密加密字符串的任务.这就是事情出错的地方,我不知道为什么.

This all works fine. Strings are encrypting and decrypting as expected. But now I am faced with task of decrypting encrypted strings from this node.js code, in Java. And that is where things are going wrong and I am not sure why.

对于解密,我的 Java 代码如下所示:

For decryption, My Java code looks like this:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import java.security.MessageDigest;
import java.util.Arrays;

private static final String encKey = "FOO";
private static SecretKeySpec secretKey;
private static byte[] key;

public static String decrypt(String str) throws Exception {
  String hexDecodedStr = new String(Hex.decodeHex(str.toCharArray()));
  setKey(encKey);
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  cipher.init(Cipher.DECRYPT_MODE, secretKey);
  return new String(cipher.doFinal(hexDecodedStr.getBytes()));
}

private static void setKey(String myKey) throws Exception {
  MessageDigest sha = null;
  try {
    key = myKey.getBytes("UTF-8");
    sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key, 16); 
    secretKey = new SecretKeySpec(key, "AES");
  } 
  catch (Exception e) {
    throw e;
  } 
}

而且它不起作用.似乎无论我尝试什么,我最终都会在 cipher.doFinal() 调用中遇到一些异常,或者我得到的字符串是完全错误的.我知道 node.js 代码使用的是 aes-256-cbc,而 Java 代码使用的是 AES/ECB/PKCS5Padding 而不是 AES/CBC/PKCS5Padding,但是当我尝试使用 AES/CBC/PKCS5Padding 时,它需要一个我在 node.js 中没有的 InitVector,所以我不确定如何继续.如果没有提供一个 InitVector,节点是否会在后台创建一个 InitVector?我是否遗漏了一些非常明显的东西?

And it doesn't work. It seems like no matter what I try, I end up with some exception on the cipher.doFinal() call, or the String I get back is totally wrong. I know the node.js code is using aes-256-cbc, while the Java code is using AES/ECB/PKCS5Padding instead of AES/CBC/PKCS5Padding, but when I tried to use AES/CBC/PKCS5Padding, it was requiring an InitVector which I didn't have in node.js so I was unsure of how to proceed. Is node making an InitVector under the hood if not provided with one? Am I missing something totally obvious?

推荐答案

你似乎和其他人有同样的问题 OpenSSL 加密失败解密C#

You seems to have the same issue as others OpenSSL encryption failing to decrypt C#

据我了解文档,加密库使用 openssl.openssl 使用其 EVP_BytesToKey 函数和随机盐(不仅仅是哈希)从密码创建 IV 和密钥.正如 dave 指出的那样,加密库不使用盐.

As far I understood the docs, the crypto libeary uses openssl. The openssl creates IV and key from the password using its EVP_BytesToKey function and random salt (not just hash). As dave pointed out, the crypto library uses no salt.

openssl 的输出是 Salted_{8 bytes salt}{ciphertext} 所以检查密码的输出是什么(我现在做不到)

我写了一篇小文章如何加密在Java中正确

I wrote a small article how to encrypt properly in Java

这篇关于在 Java 中从 node.js 中解密字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)