Note that there are some explanatory texts on larger screens.

plurals
  1. POEncryption in .NET and decryption in Android throws BadPaddingException: pad block corrupted
    primarykey
    data
    text
    <p>I did lot of research but i couldn't find answer to my issue. I'm doing AES encryption (Rijndael block size 128 bits) in .NET and decryption in Android (AES) with same password, salt &amp; IV between them. </p> <p>C# Encryption code snippet:</p> <pre><code> byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = new PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Padding = PaddingMode.PKCS7; symmetricKey.BlockSize = 128; symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor( keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string cipherText = Convert.ToBase64String(cipherTextBytes); </code></pre> <p>Android Decryption code snippet:</p> <pre><code> KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), iterationCount); SecretKey key = SecretKeyFactory.getInstance( "PBEWithSHA1And128BitAES-CBC-BC").generateSecret(keySpec); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv.getBytes())); byte[] decrypted = cipher.doFinal(encrypted); </code></pre> <p>cipher.doFinal call throws following exception. </p> <p>"05-02 18:17:38.239: W/System.err(25547): javax.crypto.BadPaddingException: pad block corrupted"</p> <p>I did set the padding to "PKCS7Padding" in both .NET and Android and the encryption block size to 128 bits. </p> <p>However, encrypt in Android &amp; decrypt in Android works fine. Same with Encrypt in .NET and Decrypt in .NET works fine too. </p> <p>Sample Test:</p> <pre><code>String PlainText = "hello world"; String EncryptedDotNetblob = "JyijoGEALMI25Zej7d+fMg=="; String EncryptedAndroidblob = "1HhuTQaLRJEK4zxI9FAO6A=="; </code></pre> <p>as you can see above blobs are different too. </p> <p>Anyone knows what's the problem here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload