473,287 Members | 1,413 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,287 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 29157
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.