2

while converting a .net code of decryption to java I got an exception

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)

well I'm trying it for the first time both decryption and converting a .net code to java

here is the .net code that I'm trying to convert

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

I went through many posts and found Decrypting bytes encrypted by .NET's RijndaelManaged using Java is more related to my case.

I followed these and wrote my decrypt function as ::

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

but now I'm getting Exception in thread "main" java.lang.IllegalArgumentException: Missing argument.

can anyone help me getting these errors fixed. any help would be appreciated. thanks!!

Community
  • 1
  • 1
Sinha
  • 773
  • 2
  • 16
  • 34
  • 1
    Which line is throwing the exception? If it is the `cipher.init(...)` line, then split it onto separate lines, so you can see exactly which of the three parameters is throwing. It could be `cipher.init()` or it could be one of the two `new`'s that is throwing. Break things out so you can localise the error more. – rossum Oct 14 '12 at 15:52
  • The culprit is new SecretKeySpec(sessionKey, "AES").. – Sinha Oct 14 '12 at 16:08
  • it was because of byte[] sessionKey = null; but even if I'm putting byte[] sessionKey = Key.getBytes(); and byte[] iv = IV.getBytes() ; It is not solving my purpose... any idea where I'm missing – Sinha Oct 14 '12 at 17:02
  • Please post complete stacktrace. – Maarten Bodewes Oct 15 '12 at 03:29
  • Wrong IV length: must be 16 bytes long at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineInit(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at com.motorola.gst.DecryptTest3.Decrypt(DecryptTest3.java:96) at com.motorola.gst.DecryptTest3.main(DecryptTest3.java:39) – Sinha Oct 15 '12 at 04:12

1 Answers1

1

For me the problem was calling new SecretKeySpec(sessionKey, "AES") with sessionKey = null.

asherbret
  • 5,439
  • 4
  • 38
  • 58