468,556 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,556 developers. It's quick & easy.

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 28227
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Patrick Dugan | last post: by
4 posts views Thread by floppyzedolfin | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.