473,388 Members | 954 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,388 software developers and data experts.

Encryption Error "Length of the data to decrypt is invalid"

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.

Please find below the two routines I use to encrypt my text and decrypt
the text. I have used the two routines in a very simple app that takes
a string from a label and then calls EncryptString and displays it on
another label. I then call DecryptString and pass it the contents of
the encrypted label text property.

The CryptKey is byte[32] and is set on creation of the object
containing these methods.

Could somebody please enlighten me as to what I am doing wrong!
Thanks in advance

Jimski

public string EncryptString(string pText)
{
string strResult = "";

try
{
// Create the rijndael encryptor
ICryptoTransform encryptor =
_RijndaelEncryptor.CreateEncryptor();

// Encrypt the data to the memory stream
MemoryStream msEncrypt = new MemoryStream(pText.Length);
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);

// Convert the string to a byte array.
byte[] beforeEncrypt = Encoding.UTF8.GetBytes(pText);

// Write all the data to the crypto stream and flush it to the
MemoryStream.
csEncrypt.Write(beforeEncrypt, 0, beforeEncrypt.Length);
csEncrypt.FlushFinalBlock();

// Read encrypted array of bytes from the MemoryStream
byte[] afterEncrypt = new byte[msEncrypt.Length];
msEncrypt.Position = 0;
msEncrypt.Read(afterEncrypt, 0, afterEncrypt.Length);

csEncrypt.Close();

// Returns the encrypted text as a string
strResult = Encoding.UTF8.GetString(afterEncrypt);
}
catch (Exception ex)
{
// An error occurred during encryption
}
return strResult;
}
public string DecryptString(string pEncryptedText)
{
string strResult = "";

try
{
// Convert the string to a byte array.
byte[] beforeDecrypt = Encoding.UTF8.GetBytes(pEncryptedText);

// Create the rijndael decryptor
ICryptoTransform decryptor =
_RijndaelEncryptor.CreateDecryptor();

// decrypt the data to the memory stream
MemoryStream msDecrypt = new
MemoryStream(beforeDecrypt.Length);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Write);

// Write all the data to the crypto stream and flush it.
csDecrypt.Write(beforeDecrypt, 0, beforeDecrypt.Length);
// ERROR occurs in this next line.
csDecrypt.FlushFinalBlock();

// Read decrypted array of bytes from the MemoryStream
byte[] afterDecrypt = new byte[msDecrypt.Length];
msDecrypt.Position = 0;
msDecrypt.Read(afterDecrypt, 0, afterDecrypt.Length);

csDecrypt.Close();

// Returns the decrypted text as a string
strResult = Encoding.UTF8.GetString(afterDecrypt);
}
catch (Exception ex)
{
// Show error - which in this case is "Length of the data to
decrypt is invalid"
}

return strResult;
}

Nov 16 '05 #1
3 29179
Here's a sample that can should help:

http://www.dalepreston.com/Blog/Arch...0_Archive.html

DalePres
MCAD, MCDBA, MCSE

"Jimski" <ji**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

Please find below the two routines I use to encrypt my text and decrypt
the text. I have used the two routines in a very simple app that takes
a string from a label and then calls EncryptString and displays it on
another label. I then call DecryptString and pass it the contents of
the encrypted label text property.

The CryptKey is byte[32] and is set on creation of the object
containing these methods.

Could somebody please enlighten me as to what I am doing wrong!
Thanks in advance

Jimski

public string EncryptString(string pText)
{
string strResult = "";

try
{
// Create the rijndael encryptor
ICryptoTransform encryptor =
_RijndaelEncryptor.CreateEncryptor();

// Encrypt the data to the memory stream
MemoryStream msEncrypt = new MemoryStream(pText.Length);
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);

// Convert the string to a byte array.
byte[] beforeEncrypt = Encoding.UTF8.GetBytes(pText);

// Write all the data to the crypto stream and flush it to the
MemoryStream.
csEncrypt.Write(beforeEncrypt, 0, beforeEncrypt.Length);
csEncrypt.FlushFinalBlock();

// Read encrypted array of bytes from the MemoryStream
byte[] afterEncrypt = new byte[msEncrypt.Length];
msEncrypt.Position = 0;
msEncrypt.Read(afterEncrypt, 0, afterEncrypt.Length);

csEncrypt.Close();

// Returns the encrypted text as a string
strResult = Encoding.UTF8.GetString(afterEncrypt);
}
catch (Exception ex)
{
// An error occurred during encryption
}
return strResult;
}
public string DecryptString(string pEncryptedText)
{
string strResult = "";

try
{
// Convert the string to a byte array.
byte[] beforeDecrypt = Encoding.UTF8.GetBytes(pEncryptedText);

// Create the rijndael decryptor
ICryptoTransform decryptor =
_RijndaelEncryptor.CreateDecryptor();

// decrypt the data to the memory stream
MemoryStream msDecrypt = new
MemoryStream(beforeDecrypt.Length);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Write);

// Write all the data to the crypto stream and flush it.
csDecrypt.Write(beforeDecrypt, 0, beforeDecrypt.Length);
// ERROR occurs in this next line.
csDecrypt.FlushFinalBlock();

// Read decrypted array of bytes from the MemoryStream
byte[] afterDecrypt = new byte[msDecrypt.Length];
msDecrypt.Position = 0;
msDecrypt.Read(afterDecrypt, 0, afterDecrypt.Length);

csDecrypt.Close();

// Returns the decrypted text as a string
strResult = Encoding.UTF8.GetString(afterDecrypt);
}
catch (Exception ex)
{
// Show error - which in this case is "Length of the data to
decrypt is invalid"
}

return strResult;
}

Nov 16 '05 #2
Thanks Dale,

Your code identified an area that I was implementing wrong.

In my decryption routine I should have populated the memory stream with
the bytes and then created the CryptoStream. Writing them afterwards
was causing problems.

Cheers for you help.
Jimski

Nov 16 '05 #3
I am glad it helped. That's what makes it worth the time to write it.

DalePres

"Jimski" <ji**********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Thanks Dale,

Your code identified an area that I was implementing wrong.

In my decryption routine I should have populated the memory stream with
the bytes and then created the CryptoStream. Writing them afterwards
was causing problems.

Cheers for you help.
Jimski

Nov 16 '05 #4

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

Similar topics

0
by: vincent wehren | last post by:
Hi, Trying to grasp Py_NewInterpreter()in a simple app embedding Python, I was wondering why the following gives me an error: int main() { PyThreadState *tstate; Py_Initialize();
1
by: arvee | last post by:
Hi - I'm updating an Oracle table with an empty string and getting the error: An unhandled exception of type 'System.Exception' occurred in system.data.dll Additional information: Parameter...
3
by: Stephen Poley | last post by:
Could some kind soul explain the errors and warnings that the W3C CSS validator generates for page: http://www.atlis.nl/testsite/nl/ Results at: http://tinyurl.com/5pxqx The error "Invalid...
3
by: 21novembre | last post by:
Hi all, I made a question several days before to describe my strange trouble of mysqldump. But I still can't figour it out. Well, I just want to ask another question whether I could just backup...
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: Simon Harris | last post by:
I'm trying to access a password protected web service. My code is as follows: Dim BS7666 As New bs7666.BS7666 'New instance of my web service Dim CredCache As CredentialCache = New...
6
by: Patrick Dugan | last post by:
Hello, I'm trying to load different images (icons) into a PictureBox1.Image. The first image loads just fine, but the second image always returns the error "Invalid property used." It doesn't...
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. ...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.