RaZzLe
RaZzLe

Reputation: 2120

Rijndael CreateDecryptor Throws Length of the data to decrypt is invalid

In my project there is a section which makes decryption using one among several cryptography providers such as DESCryptoServiceProvider, TripleDESCryptoServiceProvider, RijndaelManaged etc.

Project was running without any issue with TripleDESCryptoServiceProvider, but after Fortify suggests to use RijndaelManaged, system starts to throw Length of the data to decrypt is invalid.

This was the story in short. I'm not a professional on cryptography things, but if I'm not wrong, the problem should be related with the way I'm creating my byte[]. Now here comes the codes:

This is the part where is set methodology:

public System.Security.Cryptography.SymmetricAlgorithm _crypto;

public void Symmetric(MyProject.Framework.Security.Symmetric.Provider provider)
    {
        switch (provider)
        {
            case MyProject.Framework.Security.Symmetric.Provider.DES:
                this._crypto = new System.Security.Cryptography.DESCryptoServiceProvider();
                break;
            case MyProject.Framework.Security.Symmetric.Provider.RC2:
                this._crypto = new System.Security.Cryptography.RC2CryptoServiceProvider();
                break;
            case MyProject.Framework.Security.Symmetric.Provider.Rijndael:
                this._crypto = new System.Security.Cryptography.RijndaelManaged();
                break;
            case MyProject.Framework.Security.Symmetric.Provider.TripleDES:
                this._crypto = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                break;
        }
    }

And this is the method which generates the byte array:

    public byte[] FromHex(string hexEncoded)
    {
        if (hexEncoded == null || hexEncoded.Length == 0)
        {
            return null;
        }

        checked
        {
            byte[] result;

            try
            {
                int num = Convert.ToInt32((double)hexEncoded.Length / 2.0);

                byte[] array = new byte[num - 1 + 1];

                int arg_36_0 = 0;

                int num2 = num - 1;

                for (int i = arg_36_0; i <= num2; i++)
                {
                    array[i] = Convert.ToByte(hexEncoded.Substring(i * 2, 2), 16);
                }

                result = array;
            }
            catch (Exception expr_5A)
            {
                throw new FormatException("The provided string does not appear to be Hex encoded", expr_5A);
            }

            return result;
        }
    }

Below is my Decrypt method,

    public void Decrypt(byte[] encryptedData)
    {
        System.IO.MemoryStream stream = new System.IO.MemoryStream(encryptedData, 0, encryptedData.Length);

        checked
        {
            byte[] array = new byte[encryptedData.Length - 1 + 1];

            using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(stream, this._crypto.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read))
            {
                try
                {
                    cryptoStream.Read(array, 0, encryptedData.Length - 1);
                }
                catch (System.Security.Cryptography.CryptographicException expr_56)
                {
                    throw new System.Security.Cryptography.CryptographicException("Unable to decrypt data. The provided key may be invalid.", expr_56);
                }
                finally
                {
                    cryptoStream.Close();
                }
            }
        }
    }

When I call Decrypt method,

        var provider = MyProject.Framework.Security.Symmetric.Provider.Rijndael;

        instance.Symmetric(provider);

        var openString = "C9B7163BFA3E5E46";

        var byteArray = instance.FromHex(openString);

        instance.Decrypt(byteArray);

It throws Length of the data to decrypt is invalid on that cryptoStream.Read(...) as described on above.

Could someone advice me with the correct way of handling these? Is it really the way I'm creating that byte array which I send to my Decrypt method?

Thanks in advance.

Upvotes: 0

Views: 246

Answers (0)

Related Questions