Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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
在java中转换RijndaelManaged时缺少参数_Java_Aes_Rijndaelmanaged - Fatal编程技术网

在java中转换RijndaelManaged时缺少参数

在java中转换RijndaelManaged时缺少参数,java,aes,rijndaelmanaged,Java,Aes,Rijndaelmanaged,在将.net解密代码转换为java时,我遇到了一个异常 Exception in thread "main" java.lang.IllegalArgumentException: Missing argument at javax.crypto.spec.SecretKeySpec.<init>(DashoA13*..) at com.motorola.gst.DecryptTest3.Decrypt(DecryptTest3.java:90) at com

在将.net解密代码转换为java时,我遇到了一个异常

Exception in thread "main" java.lang.IllegalArgumentException: Missing argument
    at javax.crypto.spec.SecretKeySpec.<init>(DashoA13*..)
    at com.motorola.gst.DecryptTest3.Decrypt(DecryptTest3.java:90)
    at com.motorola.gst.DecryptTest3.main(DecryptTest3.java:36)
我浏览了很多帖子,发现这与我的情况更相关

我遵循以下步骤编写了解密函数::

private static String Decrypt(String encryptedText, String completeEncodedKey,int keySize) {
        //get completeEncodedKey in bytes and then to string
        String decodedcompleteEncodedKey = StringUtils.newStringUtf8(Base64.decodeBase64(completeEncodedKey));
        System.out.println("Decoded completeEncodedKey Key ::  "+decodedcompleteEncodedKey);
        int indexComma = decodedcompleteEncodedKey.indexOf(',');
        System.out.println("COmma Index :: "+indexComma);
        String IV = decodedcompleteEncodedKey.substring(0, indexComma);
        String Key = decodedcompleteEncodedKey.substring(indexComma+1,decodedcompleteEncodedKey.length());
        System.out.println("IV::: "+IV);
        System.out.println("Key::: "+Key);


    byte[] sessionKey = null; 
    byte[] iv = null ; 
    byte[] plaintext = encryptedText.getBytes(); 
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
        byte[] ciphertext = cipher.doFinal(plaintext);
    } catch (IllegalBlockSizeException e) {
        System.out.println("IllegalBlockSizeException");
        e.printStackTrace();
    } catch (BadPaddingException e) {
        System.out.println("BadPaddingException");
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        System.out.println("InvalidKeyException");
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        System.out.println("InvalidAlgorithmParameterException");
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException");
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        System.out.println("NoSuchPaddingException");
        e.printStackTrace();
    }
    return null;
}
但现在我在主线程java.lang.IllegalArgumentException中遇到异常:缺少参数

谁能帮我修复这些错误。 任何帮助都将不胜感激。
谢谢

对我来说,问题是调用新的SecretKeySpecsessionKey,sessionKey=null的AES。

哪一行引发异常?如果是密码。初始化。。。行,然后将其拆分为单独的行,以便您可以确切地看到三个参数中的哪一个正在抛出。它可能是cipher.init,也可能是正在抛出的两个新的。找出问题的症结所在,以便进一步定位错误。罪魁祸首是新的SecretKeySpecsessionKey,AES..这是因为byte[]sessionKey=null;但即使我把byte[]sessionKey=Key.getBytes;字节[]iv=iv.getBytes;这并不能解决我的目的。。。知道我遗漏了什么吗?请在com.sun.crypto.provider.SunJCE_f.aDashoA13*上发布完整的stacktrace。错误的IV长度:必须是16字节长。。请访问com.sun.crypto.provider.aesciper.engineInitDashoA13*。。位于javax.crypto.Cipher.aDashoA13*。。位于javax.crypto.Cipher.aDashoA13*。。位于javax.crypto.Cipher.initDashoA13*。。位于javax.crypto.Cipher.initDashoA13*。。com.motorola.gst.DecryptTest3.DecryptDecryptTest3.java:96 com.motorola.gst.DecryptTest3.mainDecryptTest3.java:39
private static String Decrypt(String encryptedText, String completeEncodedKey,int keySize) {
        //get completeEncodedKey in bytes and then to string
        String decodedcompleteEncodedKey = StringUtils.newStringUtf8(Base64.decodeBase64(completeEncodedKey));
        System.out.println("Decoded completeEncodedKey Key ::  "+decodedcompleteEncodedKey);
        int indexComma = decodedcompleteEncodedKey.indexOf(',');
        System.out.println("COmma Index :: "+indexComma);
        String IV = decodedcompleteEncodedKey.substring(0, indexComma);
        String Key = decodedcompleteEncodedKey.substring(indexComma+1,decodedcompleteEncodedKey.length());
        System.out.println("IV::: "+IV);
        System.out.println("Key::: "+Key);


    byte[] sessionKey = null; 
    byte[] iv = null ; 
    byte[] plaintext = encryptedText.getBytes(); 
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
        byte[] ciphertext = cipher.doFinal(plaintext);
    } catch (IllegalBlockSizeException e) {
        System.out.println("IllegalBlockSizeException");
        e.printStackTrace();
    } catch (BadPaddingException e) {
        System.out.println("BadPaddingException");
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        System.out.println("InvalidKeyException");
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        System.out.println("InvalidAlgorithmParameterException");
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException");
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        System.out.println("NoSuchPaddingException");
        e.printStackTrace();
    }
    return null;
}