Node JS加密代码转Java加密代码

Node JS encryption code to Java encryption code

我正在尝试为以下 Node JS 代码找到类似的 java 代码,

节点JS代码:

     var crypto = require('crypto');

     var mykey = crypto.createCipher('aes-128-ecb', 'XXXXXXXX00000000');
     var mystr = mykey.update('HelloWorld', 'utf8', 'hex')
     mystr += mykey.final('hex');

      console.log(mystr);

加密结果:ce25d577457cf8113fa4d9eb16379529

Java代码:

 public static String toHex(String arg) throws UnsupportedEncodingException {
              return String.format("%x", new BigInteger(1, arg.getBytes("UTF-8")));
        }

 public static void main(String args[]) throws Exception{
 
    byte[] key = "XXXXXXXX".getBytes();
    String message = "HelloWorld";
        
    try {
        key = Arrays.copyOf(key, 16); 
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);         
        byte encstr[] = cipher.update(message.getBytes());
        String encData = new String(encstr, "UTF-8");
        encData = toHex(encData);
        byte encstr2[] = cipher.doFinal();
        String encData2 = new String(encstr2);
        encData  = encData + toHex(encData2);           
        System.out.println(encData);
    } catch (NoSuchAlgorithmException nsae) {
        throw new Exception("Invalid Java Version");
    } catch (NoSuchPaddingException nse) {
        throw new Exception("Invalid Key");
    }
    
}

加密结果:056efbfbdefbfbd7c7760efbfbdefbfbdefbfbd39262cefbfbdefbfbd5166

根据@Robert 和@Topaco 的评论,我编写了一个简单的解密程序,该程序适用于给定的密码 'XXXXXXXX00000000'。

请记住,此程序使用的是不安全的 AES ECB 模式和不安全的 MD5 哈希算法。 该程序不提供任何适当的异常处理。

这是结果:

ciphertext Java:   ce25d577457cf8113fa4d9eb16379529
ciphertext NodeJS: ce25d577457cf8113fa4d9eb16379529

我的代码:

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainSo {
    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        System.out.println("

        String plaintext = "HelloWorld";
        String keyString =  "XXXXXXXX00000000"; // 16 chars
        byte[] key = keyString.getBytes(StandardCharsets.UTF_8);
        // md5 hashing is unsecure !!
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] keyMd5 = md.digest(key);
        // aes ecb mode encryption is unsecure
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyMd5, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        System.out.println("ciphertext Java:   " + bytesToHex(ciphertext));
        System.out.println("ciphertext NodeJS: " + "ce25d577457cf8113fa4d9eb16379529");
    }
    private static String bytesToHex(byte[] bytes) {
        // service method for displaying a byte array as hex string
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}