从Java中的node.js解密字符串? [英] Decrypting strings from node.js in Java?

查看:117
本文介绍了从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()调用上遇到一些异常,否则我得到的String完全错误。我知道node.js代码使用的是aes-256-cbc,而Java代码使用的是AES / ECB / PKCS5Padding而不是AES / CBC / PKCS5Padding,但是当我尝试使用AES / CBC / PKCS5Padding时,它需要一个我在node.js中没有的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和密钥。正如戴夫指出的那样,加密库不使用盐。

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字节salt} {ciphertext},因此请检查密码(我现在无法做到)

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

I wrote a small article how to encrypt properly in Java

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆