Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting error "Padding is invalid and cannot be removed." when trying to decrypt file contents in chunks(using buffer). I am able to decrypt whole file at once but not in blocks. I found many links regarding this problem and most of them suggested to set Padding of AesManaged object like aesManaged.Padding = PaddingMode.None But this property is not available in window phone application. Below is method:

C#
internal static byte[] DecryptBytes(byte[] cipherText, string password)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");

        byte[] decryptedBytes= new byte[cipherText.Length];

        using (var rijAlg = new AesManaged { KeySize = 256, BlockSize = 128 })
        {
        var key = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(Salt));
            rijAlg.Key = key.GetBytes(rijAlg.KeySize / 8);
            rijAlg.IV = key.GetBytes(rijAlg.BlockSize / 8);

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption.
            using (var msDecrypt = new MemoryStream())
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
                {
                    csDecrypt.Write(cipherText, 0, cipherText.Length);
                    csDecrypt.FlushFinalBlock();

                }
                decryptedBytes = msDecrypt.ToArray();
            }

        }
        return decryptedBytes;
    }

Please suggest issue in above code or any other workaround
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900