PC Review


Reply
Thread Tools Rating: Thread Rating: 3 votes, 4.33 average.

Length of the data to decrypt is invalid.

 
 
gane kol
Guest
Posts: n/a
 
      17th Jun 2005
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[i] == 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;

}


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Jun 2005
gane kol <(E-Mail Removed)> wrote:
> 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.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: decrypt problem: Length of the data to decrypt is invalid Oleg Subachev Microsoft C# .NET 4 21st Dec 2006 06:41 AM
Re: decrypt problem: Length of the data to decrypt is invalid Jon Skeet [C# MVP] Microsoft C# .NET 5 21st Dec 2006 02:12 AM
Re: decrypt problem: Length of the data to decrypt is invalid Robson Siqueira Microsoft C# .NET 1 19th Dec 2006 09:55 PM
"Length of the data to decrypt is invalid." when trying to decrypt TripleDes algorithm Dica Microsoft C# .NET 7 28th Oct 2005 02:07 PM
Encryption Error "Length of the data to decrypt is invalid" Jimski Microsoft C# .NET 3 15th Mar 2005 02:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:36 PM.