Link to home
Start Free TrialLog in
Avatar of ocortes
ocortes

asked on

cryptographicexception was unhandled length of the data to decrypt is invalid

I have an app to Encrypt and Decrypt a string. I am new using the functions and I don't really know why I get a cryptographicexception telling me that length of the data to decrypt is invalid with some strings but the app works great with other stirngs. Attached is the code that I use for the Encrypt, Decrypt and I call the funtion on Windows with the following code:

private void button2_Click(object sender, EventArgs e)
        {
            Crypto cr = new Crypto(Crypto.SymmProvEnum.DES);
            txt2.Text = cr.Encrypting(txt1.Text.Trim());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Crypto cr = new Crypto(Crypto.SymmProvEnum.DES);
            txt3.Text = cr.Decrypting(txt2.Text.Trim());

        }
Everything works fine until I put gonzo as the input string the I get the exception. Like I said I am new using System.Cryptography and don't know where to start. Any help will be appreciated.
namespace WindowsFormsApplication1
{
    public class Crypto
    {
        string Key = "12345678";

        public Crypto()
        {
            //
            // TODO: Add constructor logic here
            //
        }



        /// <remarks>
        /// Supported .Net intrinsic SymmetricAlgorithm classes.
        /// </remarks>
        public enum SymmProvEnum : int
        {
            DES, RC2, RC4, Rijndael
        }

        private SymmetricAlgorithm mobjCryptoService;

        /// <remarks>
        /// Constructor for using an intrinsic .Net SymmetricAlgorithm class.
        /// </remarks>
        public Crypto(SymmProvEnum NetSelected)
        {
            switch (NetSelected)
            {
                case SymmProvEnum.DES:
                    mobjCryptoService = new DESCryptoServiceProvider();
                    break;
                case SymmProvEnum.RC2:
                    mobjCryptoService = new RC2CryptoServiceProvider();
                    break;
                case SymmProvEnum.Rijndael:
                    mobjCryptoService = new RijndaelManaged();
                    break;
                case SymmProvEnum.RC4:
                    mobjCryptoService = new RijndaelManaged();
                    break;
            }
        }

        /// <remarks>
        /// Constructor for using a customized SymmetricAlgorithm class.
        /// </remarks>
        public Crypto(SymmetricAlgorithm ServiceProvider)
        {
            mobjCryptoService = ServiceProvider;
        }

        /// <remarks>
        /// Depending on the legal key size limitations of a specific CryptoService provider
        /// and length of the private key provided, padding the secret key with space character
        /// to meet the legal size of the algorithm.
        /// </remarks>
        private byte[] GetLegalKey(string Key)
        {
            string sTemp = Key;
            if (mobjCryptoService.LegalKeySizes.Length > 0)
            {
                int moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
                // key sizes are in bits
                if (sTemp.Length * 8 > mobjCryptoService.LegalKeySizes[0].MaxSize)
                    // get the left of the key up to the max size allowed
                    sTemp = sTemp.Substring(0, mobjCryptoService.LegalKeySizes[0].MaxSize / 8);
                else if (sTemp.Length * 8 < moreSize)
                    if (mobjCryptoService.LegalKeySizes[0].SkipSize == 0)
                        // simply pad the key with spaces up to the min size allowed
                        sTemp = sTemp.PadRight(moreSize / 8, ' ');
                    else
                    {
                        while (sTemp.Length * 8 > moreSize)
                            moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;

                        sTemp = sTemp.PadRight(moreSize / 8, ' ');
                    }
            }

            // convert the secret key to byte array
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        public string Encrypting(string Source)
        {
            byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(System.Web.HttpUtility.UrlEncode(Source));

            // create a MemoryStream so that the process can be done without I/O files
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create an Encryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

            // create Crypto Stream that transforms a stream using the encryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

            // write out encrypted content into MemoryStream
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();

            // get the output and trim the '\0' bytes
            byte[] bytOut = ms.GetBuffer();
            int i = 0;
            for (i = 0; i < bytOut.Length; i++)
                if (bytOut[i] == 0)
                    break;

            // convert into Base64 so that the result can be used in xml
            return System.Convert.ToBase64String(bytOut, 0, i);
        }

        public string Decrypting(string Source)
        {
            // convert from Base64 to binary
            byte[] bytIn = System.Convert.FromBase64String(Source);
            // create a MemoryStream with the input
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create a Decryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();

            // create Crypto Stream that transforms a stream using the decryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

            // read out the result from the Crypto Stream
            System.IO.StreamReader sr = new System.IO.StreamReader(cs);
            string sEncoded = sr.ReadToEnd();
            return System.Web.HttpUtility.UrlDecode(sEncoded);
        }


    }
}

Open in new window

Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Why are you trimming the encrypted string? The Trim function removes all whitespace characters from the string. If the encryption process adds a space, tab, or newline character at the beginning or end of the string, then the decrypt will fail since data is missing.
It says it pads with spaces if the key isn't long enough, so trimming is likely to cause problems.

Take out the Trim on this line
            txt3.Text = cr.Decrypting(txt2.Text.Trim());
The other one is okay.
Avatar of ocortes
ocortes

ASKER

Hello,
Thanks for the comment but that is not the problem. The problem is that some strings when they are Encrypted they return a short string that cannot be Decrypted. I changed the encryption from DES to Rijndael and the srting that didn't work on the first place worked fine but another string gave me an error. I don't know what I can change on the Encrypt part so that I get a valid string all the time.
Thank you,
Pad it with spaces (like it suggests) something like this (Note: I don't use .NET much so syntax may be off, I'm sure you can fix it)
        public string Encrypting(string Source)
        {
            if(Source.Length() < 10) //Or whatever the limit is
                Source = Source + "          " //Add 10 spaces
            //rest of code
Avatar of ocortes

ASKER

I will try it but I am not sure if it will work work because the Length() that I get it's always 256 so I will have big strings with spaces. I will let you know.
Thanks
You will get extra spaces at the end, but since you are using Trim anyway you can trim them back off.
If you don't want to use spaces you could add some non-printing character that no one would ever use and trim them by default in the decrypt code.
ASKER CERTIFIED SOLUTION
Avatar of ocortes
ocortes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocortes

ASKER

It work fine for most strings but it could be a string that ecrypts to two 0 on the array and it will fail to decrypt