0

I know, this topic was handled here before, but I just dont get my error. Code in Android:

public String encryptMsg(String input) {
    try {
        byte[] key_Array = Base64.decode(password, Base64.DEFAULT);

        Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        byte[] iv = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        Key SecretKey = new SecretKeySpec(key_Array, "AES");
        _Cipher.init(Cipher.ENCRYPT_MODE, SecretKey, ivspec);

        return Base64.encodeToString(_Cipher.doFinal(input.getBytes()), Base64.DEFAULT);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return null;
}

public String decryptMsg(String input) {
    try {
        byte[] key_Array = Base64.decode(password, Base64.DEFAULT);

        Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        byte[] iv = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        Key SecretKey = new SecretKeySpec(key_Array, "AES");
        _Cipher.init(Cipher.DECRYPT_MODE, SecretKey, ivspec);

        byte DecodedMessage[] = Base64.decode(input, Base64.DEFAULT);
        return new String(_Cipher.doFinal(DecodedMessage));

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return null;
}

My code in C#:

private string newEncrypt(string input)
    {
        RijndaelManaged aes = new RijndaelManaged();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        byte[] KeyArrBytes32Value = new byte[32];
        Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);

        byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        byte[] IVBytes16Value = new byte[16];
        Array.Copy(ivArr, IVBytes16Value, 16);

        aes.Key = KeyArrBytes32Value;
        aes.IV = IVBytes16Value;

        ICryptoTransform cipher = aes.CreateEncryptor();

        byte[] plainTextByte = ASCIIEncoding.UTF8.GetBytes(input);
        byte[] CipherText = cipher.TransformFinalBlock(plainTextByte, 0, plainTextByte.Length);
        return Convert.ToBase64String(CipherText);
    }

    private string newDecrypt(string input)
    {
        RijndaelManaged aes = new RijndaelManaged();
        aes.BlockSize = 128;
        aes.KeySize = 256;

        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        byte[] KeyArrBytes32Value = new byte[32];
        Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);

        byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        byte[] IVBytes16Value = new byte[16];
        Array.Copy(ivArr, IVBytes16Value, 16);

        aes.Key = KeyArrBytes32Value;
        aes.IV = IVBytes16Value;

        ICryptoTransform cipher = aes.CreateDecryptor();

        byte[] encryptedBytes = Convert.FromBase64CharArray(input.ToCharArray(), 0, input.Length);
        byte[] decryptedData = cipher.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
        return ASCIIEncoding.UTF8.GetString(decryptedData);
    }

I get an error while decrypting text in the android application after i encrypted in the c# application. The error is as follows: "javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt" If I'm not totally wrong the padding on both sides is the same ("PKCS").

Does anyone have an idea, what I'm doing wrong?

PS: The key I used to encrypt is 32 numeric characters

  • The code says KeySize = 256. Also make sure the Block size 128 is the same in Jave. – jdweng Jun 22 '18 at 10:36
  • see https://stackoverflow.com/questions/31292419/badpaddingexception-when-decrypting-aes-with-the-same-key – TAHA SULTAN TEMURI Jun 22 '18 at 10:37
  • @jdwen i didn't formulate that correctly, me KEYSTRING hast the length of 32 characters. leading to a byte[] of the length 32. I changed the KeySize in C# to 128 (32byte), but that lead to the same exception. I also tried to change the change the "mode" in java to aes_128 (to make sure block size is 128) but that lead to a "noSuchAlgorithm"-exception – markthwain1797 Jun 22 '18 at 13:59
  • Rijndael algorithm supports key lengths of 128, 192, or 256 bits; defaulting to 256 bits. This algorithm supports block sizes of 128, 192, or 256 bits; defaulting to 128 bits (Aes-compatible). The Differences Between Rijndael and AES see :https://blogs.msdn.microsoft.com/shawnfa/2006/10/09/the-differences-between-rijndael-and-aes/ – jdweng Jun 22 '18 at 14:32

0 Answers0