473,378 Members | 1,457 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,378 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 29173
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.