Before spending any time reading this, see the self-answer below. The problem was invalid input.


When I try to decrypt some strings it throws this exception:

    "   at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
       at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
       at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
       at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
       at System.IO.Stream.Close()
       at System.IO.Stream.Dispose()
       at EBookReader.cryptography.DecryptString(String message, String KeyString, String IVString) in C:\\Users\\XWare\\Documents\\Visual Studio 2008\\Projects\\EBookReader\\EBookReader\\cryptography.cs:line 94"

Under debugging it throws the exception on this line :

    byte[] messageBytes = Convert.FromBase64String(message);

I think this problem just appears when string to decrypt is too big because when I try to encrypt and decrypt short strings like "Hi there I am X-Ware" it works fine

    public static string DecryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string decrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;
        rj.Padding = PaddingMode.PKCS7;
        try
        {
            MemoryStream ms = new MemoryStream();
            Encoding enc = new ASCIIEncoding();
            using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
            {
                byte[] messageBytes = Convert.FromBase64String(message);
                cs.Write(messageBytes, 0, messageBytes.Length);
                cs.FlushFinalBlock();  
            }// This is line 94
            byte[] encoded = ms.ToArray();
            decrypted = enc.GetString(encoded);

            ms.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return decrypted;
    }

any suggestions ?!

P.S. I wrote a comment to show you line 94 // This is line 94

有帮助吗?

解决方案 2

The problem is that the string I pass as Base64 string is invalid the problem is just in the input file which makes decryption impossible

Thanx all

其他提示

The FlushFinalBlock method is only intended to flush the last complete or partial block of bytes. Thus if you write more than a single block size it is not the right method call to make. Try instead simply closing the crypto stream before you attempt to read from the memory stream.

To do this in the context of your code, simple remove the line cs.FlushFinalBlock()...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top