Hi,
I need your help, the following code is not working, ret = sr.ReadToEnd()
throws the message error : Length of the data to decrypt is invalid.
Any idea ?
thank you

public string Decrypting(string Source, string Key)
{
string ret = "";
try
{
// 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);

ret = sr.ReadToEnd();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message.ToString());
}
return ret;
}

Re: Help with "Length of the data to decrypt is invalid" message error by Peter

Peter
Thu May 08 13:35:52 CDT 2008

On Thu, 08 May 2008 06:41:24 -0700, SM <sauvemark@hotmail.com> wrote:

> I need your help, the following code is not working, ret =3D sr.ReadTo=
End()
> throws the message error : Length of the data to decrypt is invalid.
> Any idea ?

Unfortunately, the most obvious idea is that the length of the data to =

decrypt is in fact invalid.

If you can post a concise-but-complete code example that reliably =

demonstrates the problem from start to finish, then perhaps some advice =
=

can be offered. Such a sample would start with some given data, would =

encode the text to bytes, encrypt it, convert to Base64, and then pass =

that result to something like you've posted here to be converted back fr=
om =

Base64 and then decrypted and read as text.

Without such an example, it's hard to say. You've only posted half of t=
he =

implementation, and the error could be somewhere in the encryption stage=
, =

or some asymmetry between a valid encryption and your decryption code.

Pete