473,834 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Syst em.String)
Error Line: strDecrpt = sr.ReadToEnd();

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

But it works for

id1: 02FPObSRAf6bARv t5FM3wA==

Any ideas appreciated,

Thanks
Gane

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

{

if (System.Configu ration.Configur ationSettings.A ppSettings["ENV"] != "DEV")

{

string Key;

string strEncrpt = null;

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

{

int i = 0;

Key = CRYPTO_KEY;

try

{

byte[] bytIn = System.Text.ASC IIEncoding.ASCI I.GetBytes(Sour ce);

System.IO.Memor yStream ms = new System.IO.Memor yStream();

byte[] bytKey = GetLegalKey(Key );

mobjCryptoServi ce.Key = bytKey;

mobjCryptoServi ce.IV = bytKey;

ICryptoTransfor m encrypto = mobjCryptoServi ce.CreateEncryp tor();

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

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

cs.FlushFinalBl ock();

byte[] bytOut = ms.GetBuffer();

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

{

if ((bytOut[i] == 0))

{

goto exitForStatemen t0;

}

}

exitForStatemen t0: ;

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

return strEncrpt;

}

catch (Exception Exp)

{

throw Exp;

}

}

else

{return strEncrpt; }

}

return Source;

}
public string Decrypting(stri ng Source)

{

if (System.Configu ration.Configur ationSettings.A ppSettings["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. FromBase64Strin g(Source);

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

byte[] bytKey = GetLegalKey(Key );

mobjCryptoServi ce.Key = bytKey;

mobjCryptoServi ce.IV = bytKey;

ICryptoTransfor m encrypto = mobjCryptoServi ce.CreateDecryp tor();

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

System.IO.Strea mReader sr = new System.IO.Strea mReader(cs);

strDecrpt = sr.ReadToEnd();

return strDecrpt;

}

catch (Exception Exp)

{

throw Exp;

}

}

else

{return strDecrpt;}

}

return Source;

}
Nov 17 '05 #1
1 5362
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(Syst em.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(stri ng source)
{
if (ConfigurationS ettings.AppSett ings["ENV"] == "DEV")
{
return source;
}

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

byte[] keyData = GetLegalKey (CRYPTO_KEY);
mobjCryptoServi ce.Key = keyData;
mobjCryptoServi ce.IV = keyData;
ICryptoTransfor m transform = mobjCryptoServi ce.CreateEncryp tor();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms , transform
CryptoStreamMod e.Write))
{
using (StreamWriter writer = new StreamWriter(cs ))
{
writer.Write (source);
}
cs.FlushFinalBl ock();
}
return Convert.ToBase6 4String (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.co m>
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
29293
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 of the data to decrypt is invalid" and occurs on the csDecrypt.FlushFinalBlock.
7
17882
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 able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes); byte key = Encoding.ASCII.GetBytes("0123456789012345");
0
3378
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 actually points to this line of code in byte decrypt function: cs.FlushFinalBlock(); public static byte encrypt(byte clearData, byte Key, byte IV)
1
8048
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 the following routine are:
4
6235
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. I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES keys (it'll run on two separate computers). Please notice that - this is my first C# code :)
4
5705
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 = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256;
3
4401
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? On Windows Vista, if file encryption is used, Windows suggests to back up the key. If I import the key on another PC, then can I decrypt a data protected by the Protect() method? Or it is impossible by any means?
1
2500
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 Decrypting(string Source, string Key) { string ret = ""; try
13
3602
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 removed" on the decryption piece. From everything I can see, I am doing things correctly here My code is as follows: private const string PassStr = "MyPrivateKey"; private static readonly byte PassSalt = new byte { Byte Byte Byte Byte Byte};...
0
9797
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10509
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10219
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7757
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6954
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5626
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.