简体   繁体   English

如何在 Nodejs 上解密 Java AES

[英]How to decrypt Java AES on Nodejs

I have the following code on Java that decrypts AES encryption and I need to do the same on Node.js我在 Java 上有以下代码可以解密 AES 加密,我需要在 Node.js 上做同样的事情

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey) {
    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 (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

 public static String decrypt(String strToDecrypt, String secret) 
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    } 
    catch (Exception e) 
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

I have tried using Crypt under the following code, but it doesn't give me the same results我曾尝试在以下代码下使用 Crypt,但它并没有给我相同的结果

  var aesDecrypt = (text, password, bit) => {
  var decipher = crypto.createDecipheriv('aes-' + bit + '-ecb', password, Buffer.alloc(0));
  decipher.setAutoPadding(false);
  return Buffer.concat([
    decipher.update(text, 'base64'),
    decipher.final()
   ]).toString();
  };

How could I mimick that Java code from above into Node.js?我怎么能从上面模仿 Java 代码到 Node.js 中?

As James says, the Java code is hashing (and truncating) the password to form the key.正如詹姆斯所说,Java 代码正在散列(并截断)密码以形成密钥。 Also it does use standard padding.使用标准填充。 The following works for ASCII data:以下适用于ASCII数据:

const crypto = require ('crypto');
const mydecrypt = (pw,ctx) => {
    var h = crypto.createHash('sha1'); h.update(pw,'utf8'); var k = h.digest().slice(0,16);
    var d = crypto.createDecipheriv('aes-128-ecb', k, Buffer.alloc(0)); 
    return Buffer.concat([d.update(ctx,'base64'), d.final()]) .toString();
}
console.log(mydecrypt('password','ks7qtmk7kt5riV/Qyy3glQ=='));

->
testdata

It may not work for non-ASCII data.它可能不适用于非 ASCII 数据。 Java new String(byte[]) uses a JVM-dependent encoding which may be UTF8 or may be something different depending on your platform, build, and environment, none of which you described. Java new String(byte[])使用依赖于 JVM 的编码,该编码可能是 UTF8,也可能根据您的平台、构建和环境而有所不同,您都没有描述。 OTOH nodejs Buffer.toString() always uses UTF8. OTOH nodejs Buffer.toString()始终使用 UTF8。 You may need to change it to toString(somethingelse) to match the Java.您可能需要将其更改为toString(somethingelse)以匹配 Java。


If this 'password' is truly a password, ie chosen or even remembered by one or more human(s), using a simple hash of it is very weak and will probably be broken if used for anything not utterly trivial;如果这个“密码”真的是一个密码,即由一个或多个人选择甚至记住,使用一个简单的 hash 是非常弱的,如果用于任何不重要的事情可能会被破坏; you should use a Password-Based Key Derivation Function designed for the purpose by someone competent, like older (PKCS5) PBKDF2 or newer bcrypt, scrypt, or argon2.您应该使用由有能力的人为此目的设计的基于密码的密钥派生 Function ,例如较旧的 (PKCS5) PBKDF2 或较新的 bcrypt、scrypt 或 argon2。 However, that's not a programming question and is offtopic here;但是,这不是编程问题,在这里是题外话; it has been discussed many times and at length on https://crypto.stackexchange.com and https://security.stackexchange.com . it has been discussed many times and at length on https://crypto.stackexchange.com and https://security.stackexchange.com .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM