1

在将.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)

好吧,我第一次尝试解密并将.net代码转换为java

这是我要转换的 .net 代码

 private static string Decrypt(string encryptedText, string completeEncodedKey, int keySize)
        {
            RijndaelManaged aesEncryption = new RijndaelManaged();
            aesEncryption.KeySize = keySize;
            aesEncryption.BlockSize = 128;
            aesEncryption.Mode = CipherMode.CBC;
            aesEncryption.Padding = PaddingMode.PKCS7;
            aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
            aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);
            ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
            byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
            return ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length));
        }

我浏览了许多帖子,发现使用 Java 由 .NET 的 RijndaelManaged 加密的解密字节与我的情况更相关。

我遵循了这些并将我的解密函数写为::

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;
}

但现在我在线程“main”java.lang.IllegalArgumentException 中遇到异常:缺少参数。

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

4

1 回答 1

0

对我来说,问题是new SecretKeySpec(sessionKey, "AES")sessionKey = null.

于 2020-06-05T14:53:23.400 回答