Click here to Skip to main content
14,491,951 members
Rate this:
Please Sign up or sign in to vote.
See more:
public string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            byte[] keyArray;
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
            }
            else
            {
                keyArray = Encoding.UTF8.GetBytes(key);
            }
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }


What I have tried:

error: byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); ---> Length of the data to decrypt is invalid

text is getting encrypted successfully, but while decrypting it gives me cryptographic exception " Length of the data to decrypt is invalid"

Can you please help me with this??

thanks
Posted
Updated 18-Jul-17 20:25pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Did you use, while encrypting, PaddingMode.None[^]?
See, for instance: Length of the data to decrypt is invalid[^].
   
Comments
Member 10302971 20-Jul-17 8:45am
   
Padding = PaddingMode.PKCS7
Mode = CipheMode.ECB
Member 10302971 20-Jul-17 8:52am
   
this is my project https://www.mediafire.com/?gw64nf1j18zdt59

pls help me. thanks

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100