Length of the data to decrypt is invalid.

G

gane kol

Hi

I am using DES algorithm. I am getting an error message in a few cases of
the querystring

Error Message : Length of the data to decrypt is invalid.
Error Method : System.String Decrypting(System.String)
Error Line: strDecrpt = sr.ReadToEnd();

QueryString Data:
---------------------
id1: IVUTMOv8Hno0eG0=

But it works for

id1: 02FPObSRAf6bARvt5FM3wA==

Any ideas appreciated,

Thanks
Gane

code
------
public string Encrypting(string Source)

{

if (System.Configuration.ConfigurationSettings.AppSettings["ENV"] != "DEV")

{

string Key;

string strEncrpt = null;

if ((Source != "") && (Source != null))

{

int i = 0;

Key = CRYPTO_KEY;

try

{

byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);

System.IO.MemoryStream ms = new System.IO.MemoryStream();

byte[] bytKey = GetLegalKey(Key);

mobjCryptoService.Key = bytKey;

mobjCryptoService.IV = bytKey;

ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

cs.Write(bytIn, 0, bytIn.Length);

cs.FlushFinalBlock();

byte[] bytOut = ms.GetBuffer();

for (i = 8; i <= bytOut.Length - 1; i++)

{

if ((bytOut == 0))

{

goto exitForStatement0;

}

}

exitForStatement0: ;

strEncrpt = System.Convert.ToBase64String(bytOut, 0, i);

return strEncrpt;

}

catch (Exception Exp)

{

throw Exp;

}

}

else

{return strEncrpt; }

}

return Source;

}


public string Decrypting(string Source)

{

if (System.Configuration.ConfigurationSettings.AppSettings["ENV"] != "DEV")

{

string strDecrpt = null;

if ((Source != "") && (Source != null))

{

string Key;

Key = CRYPTO_KEY;

Source = Source.Replace(" ","+");


char padChar = Convert.ToChar("=");

int length = Source.Length;

if (length % 4 > 0)

{

Source = Source.PadRight(length + (4-(length % 4)),padChar);

}

try

{

byte[] bytIn = System.Convert.FromBase64String(Source);

System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0,
bytIn.Length);

byte[] bytKey = GetLegalKey(Key);

mobjCryptoService.Key = bytKey;

mobjCryptoService.IV = bytKey;

ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();

CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

System.IO.StreamReader sr = new System.IO.StreamReader(cs);

strDecrpt = sr.ReadToEnd();

return strDecrpt;

}

catch (Exception Exp)

{

throw Exp;

}

}

else

{return strDecrpt;}

}

return Source;

}
 
J

Jon Skeet [C# MVP]

gane kol said:
I am using DES algorithm. I am getting an error message in a few cases of
the querystring

Error Message : Length of the data to decrypt is invalid.
Error Method : System.String Decrypting(System.String)
Error Line: strDecrpt = sr.ReadToEnd();

<snip>

Well, this is quite odd code, to be honest. It could be made far more
readable quite easily. Some comments though:

1) You're truncating the encrypted data at the first 0 byte after the
8th byte. Why? Are you trying to get to the end of the real stream
data? If so, just use ms.ToArray() instead, or just use the length of
the stream rather than testing things.

2) You're converting the text data to binary using Encoding.ASCII - are
you sure that your data will never contain non-ASCII characters?


As an example of making the code simpler, here's what I think
Encrypting should look like:

public string Encrypting(string source)
{
if (ConfigurationSettings.AppSettings["ENV"] == "DEV")
{
return source;
}

if (source==null || source.Length==0)
{
return null;
}

byte[] keyData = GetLegalKey (CRYPTO_KEY);
mobjCryptoService.Key = keyData;
mobjCryptoService.IV = keyData;
ICryptoTransform transform = mobjCryptoService.CreateEncryptor();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, transform
CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cs))
{
writer.Write (source);
}
cs.FlushFinalBlock();
}
return Convert.ToBase64String (ms.ToArray());
}
}

(I suspect the call to FlushFinalBlock is actually unnecessary here, as
disposing of the StreamWriter will close the crypto stream, which will
flush it automatically.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top