473,419 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Length of the data to decrypt is invalid.

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.AppSet tings["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.AppSet tings["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;

}
Nov 17 '05 #1
1 5246
gane kol <ga*****@softca.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
3
by: Sin Jeong-hun | last post by:
It seems like the Protect() uses the Windows accout information to encrypt data. If I know the user name and the password, can I decrypt it on another PC? If it is not, how about the exported key?...
1
by: SM | last post by:
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...
13
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.