【问题标题】:Decrypting strings from node.js in Java?在 Java 中从 node.js 中解密字符串?
【发布时间】:2018-01-01 04:34:34
【问题描述】:

我在 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 代码中解密加密字符串的任务。这就是事情出错的地方,我不知道为什么。

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

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?我是否遗漏了一些非常明显的东西?

【问题讨论】:

  • “我在 cipher.doFinal() 调用中遇到了一些异常” -- 好吧,请分享 COMPLETE 堆栈跟踪。
  • 你的 NodeJS 代码太糟糕了。你应该先解决这个问题。
  • @lukepark 还有更多东西坏了:/
  • @lukepark 我很想知道更多关于这个损坏的代码的信息,因为它对我来说可以正常工作。也许我从原始来源中省略了一两行会有所帮助?
  • 我的意思是像调用未定义的行为一样被破坏。 CBC 模式只能与createCipheriv 一起使用。您现在所做的实际上并未在任何地方定义,它可能会在不同版本和/或平台上产生不同的输出。更不用说没有 IV 的加密是不安全的并且容易受到许多微不足道的攻击。在翻译其他地方之前,您应该确保您的 NodeJS 代码安全且正确。

标签: java node.js encryption cryptography


【解决方案1】:

你似乎和其他人有同样的问题OpenSSL encryption failing to decrypt C#

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

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

我写了一个小article如何在Java中正确加密

【讨论】:

  • nodejs 加密使用 OpenSSL 库,每个 doc for createCipher 包括 EVP_BytesToKey 无盐,但不是 OpenSSL 命令行,因此不是带有 @ 的文件格式987654324@.
  • @dave_thompson_085 感谢您的指出。恕我直言,这非常危险(主要是对于不知道自己在做什么的人。也许createCipheriv 使用起来会更安全
猜你喜欢
  • 2019-06-29
  • 2013-11-24
  • 2017-03-10
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多