Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AES/CBC/NOPC在节点中加密使用相同算法在JAVA中添加和解密会产生一些垃圾,如e�J�,�D�|*�5Ҝ��_Java_Android_Encryption_Aes - Fatal编程技术网

使用AES/CBC/NOPC在节点中加密使用相同算法在JAVA中添加和解密会产生一些垃圾,如e�J�,�D�|*�5Ҝ��

使用AES/CBC/NOPC在节点中加密使用相同算法在JAVA中添加和解密会产生一些垃圾,如e�J�,�D�|*�5Ҝ��,java,android,encryption,aes,Java,Android,Encryption,Aes,我必须解密一些在节点中使用AES/CBC/NOPADDING算法加密的文本,但在JAVA中解密时会得到一些垃圾值。。请帮忙 节点加密代码: Java解密代码: 结果:e�J�,�D�|*�5Ҝ�� 最终,我找到了根本原因,罪魁祸首是将密钥转换为MD5,MD5没有按要求转换,MessageDigest将密钥转换为128位,必须是256位,我使用下面的方法按要求转换密钥,这对我来说很有效,感谢您所有有价值的输入 public static String getMd5(String input)

我必须解密一些在节点中使用AES/CBC/NOPADDING算法加密的文本,但在JAVA中解密时会得到一些垃圾值。。请帮忙

节点加密代码:

Java解密代码:


结果:e�J�,�D�|*�5Ҝ��

最终,我找到了根本原因,罪魁祸首是将密钥转换为MD5,MD5没有按要求转换,MessageDigest将密钥转换为128位,必须是256位,我使用下面的方法按要求转换密钥,这对我来说很有效,感谢您所有有价值的输入

public static String getMd5(String input)
{
    try {

        // Static getInstance method is called with hashing MD5
        MessageDigest md = MessageDigest.getInstance("MD5");

        // digest() method is called to calculate message digest
        //  of an input digest() return array of byte
        byte[] messageDigest = md.digest(input.getBytes());

        // Convert byte array into signum representation
        BigInteger no = new BigInteger(1, messageDigest);

        // Convert message digest into hex value
        String hashtext = no.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    }

    // For specifying wrong message digest algorithms
    catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

当您执行以下操作时会发生什么:new Stringcipher.doFinalBase64.getDecoder.decodebase64Cipher,UTF-8?感谢您的回复,但结果相同-e�J�,�D�|*�5Ҝ��JavaScript使用UTF-16或UCS-2进行字符编码,但在Java中,您将字符串减少为UTF-8。这没有多大意义。您没有显示如何在节点端生成密钥,但您似乎正在使用AES-256。但是,在Java端,您使用的是AES-128,因为您的密钥是16个字节。这无关紧要——根据定义,AES-256不能使用128位的密钥数据。在节点代码中,我也没有看到指定没有填充的地方。我很确定Node默认使用PKCS7填充。我建议您解决我和其他人概述的问题,并修改您问题中的代码。请注意,黑客加密通常不会产生安全代码。由于你已经混淆了密码和密钥,假设在这个问答中没有足够的安全性。我投了反对票,因为你主要使用我的评论作为答案,没有解释你实际上在做什么或表明它不安全。
private static final Charset UTF8 = Charset.forName("UTF-8");

    public static String decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
    {
        String base64Cipher = "t7rCN8nBGlruCiSvpQ9DPg==";
        byte [] iv = "0123456789012345".getBytes(UTF8);
        byte [] secretBytes = "Imgine#123$".getBytes(UTF8);

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(secretBytes);

        SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv));

        return new String(cipher.doFinal(Base64.getDecoder().decode(base64Cipher))) ;
   }
public static String getMd5(String input)
{
    try {

        // Static getInstance method is called with hashing MD5
        MessageDigest md = MessageDigest.getInstance("MD5");

        // digest() method is called to calculate message digest
        //  of an input digest() return array of byte
        byte[] messageDigest = md.digest(input.getBytes());

        // Convert byte array into signum representation
        BigInteger no = new BigInteger(1, messageDigest);

        // Convert message digest into hex value
        String hashtext = no.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    }

    // For specifying wrong message digest algorithms
    catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}