dinesh_bali
asked on
Length of the data to decrypt is invalid
I have thousands of encrypted data.
Which is encrypted by below code. Now I wish to decrypt it using the "public string Decrypt" method in the below code.
But it gives error when I try to decrypt my string saying Length of the data to decrypt is invalid
at line
csDecrypt.Read(decrypted, 0, decrypted.Length);
Can anyone help me
My code is given below
CryptionKey is:
private byte[] CryptionKey = {192,177,121,163,130,85,68 ,133,
78,172,10,23,10,157,203,17 4,
235,16,44,114,209,11,199,1 75,
105,100,113,111,195,47,186 ,164};
Code is:
using System.IO;
using System.Security.Cryptograp hy;
using System.Text;
namespace Com.Encryption
{
/// <summary>
/// class to encrypt and decrypt the data using Rijndael method
/// </summary>
public class DataCryptor
{
#region constants
public const string STRING_TERMINATOR = "\0";
#endregion
#region local members
private UnicodeEncoding textConverter;
private RijndaelManaged myRijndael;
//this will be send by the caller
private byte[] cryptionKey;
//hardcode this for all encryption
private byte[] initializationVector = {227,190,12,8,133,67,10,7,
112,152,51,45,89,197,211,1 91};
#endregion local members
#region constructors
public DataCryptor(byte[] cryptionKey32Bit)
{
textConverter = new UnicodeEncoding();
myRijndael = new RijndaelManaged();
this.cryptionKey = cryptionKey32Bit;
}
#endregion constructors
#region public methods
public string Encrypt(string dataToEncrypt)
{
string result;
byte[] encrypted;
byte[] toEncrypt;
ICryptoTransform encryptor = myRijndael.CreateEncryptor (this.cryp tionKey, this.initializationVector) ;
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
toEncrypt = textConverter.GetBytes(dat aToEncrypt );
//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock( );
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();
result = textConverter.GetString(en crypted);
return result;
}
public string Decrypt(string dataToDecrypt)
{
string result;
byte[] decrypted;
byte[] toDecrypt;
ICryptoTransform decryptor = myRijndael.CreateDecryptor (this.cryp tionKey, this.initializationVector) ;
toDecrypt = textConverter.GetBytes(dat aToDecrypt );
//Decrypt the data.
MemoryStream msDecrypt = new MemoryStream(toDecrypt);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
decrypted = new byte[toDecrypt.Length];
//Read the data out of the crypto stream.
csDecrypt.Read(decrypted, 0, decrypted.Length);
result = textConverter.GetString(de crypted).R eplace(STR ING_TERMIN ATOR, string.Empty);
return result;
}
#endregion public methods
}
}
Which is encrypted by below code. Now I wish to decrypt it using the "public string Decrypt" method in the below code.
But it gives error when I try to decrypt my string saying Length of the data to decrypt is invalid
at line
csDecrypt.Read(decrypted, 0, decrypted.Length);
Can anyone help me
My code is given below
CryptionKey is:
private byte[] CryptionKey = {192,177,121,163,130,85,68
78,172,10,23,10,157,203,17
235,16,44,114,209,11,199,1
105,100,113,111,195,47,186
Code is:
using System.IO;
using System.Security.Cryptograp
using System.Text;
namespace Com.Encryption
{
/// <summary>
/// class to encrypt and decrypt the data using Rijndael method
/// </summary>
public class DataCryptor
{
#region constants
public const string STRING_TERMINATOR = "\0";
#endregion
#region local members
private UnicodeEncoding textConverter;
private RijndaelManaged myRijndael;
//this will be send by the caller
private byte[] cryptionKey;
//hardcode this for all encryption
private byte[] initializationVector = {227,190,12,8,133,67,10,7,
112,152,51,45,89,197,211,1
#endregion local members
#region constructors
public DataCryptor(byte[] cryptionKey32Bit)
{
textConverter = new UnicodeEncoding();
myRijndael = new RijndaelManaged();
this.cryptionKey = cryptionKey32Bit;
}
#endregion constructors
#region public methods
public string Encrypt(string dataToEncrypt)
{
string result;
byte[] encrypted;
byte[] toEncrypt;
ICryptoTransform encryptor = myRijndael.CreateEncryptor
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
toEncrypt = textConverter.GetBytes(dat
//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt,
csEncrypt.FlushFinalBlock(
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();
result = textConverter.GetString(en
return result;
}
public string Decrypt(string dataToDecrypt)
{
string result;
byte[] decrypted;
byte[] toDecrypt;
ICryptoTransform decryptor = myRijndael.CreateDecryptor
toDecrypt = textConverter.GetBytes(dat
//Decrypt the data.
MemoryStream msDecrypt = new MemoryStream(toDecrypt);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
decrypted = new byte[toDecrypt.Length];
//Read the data out of the crypto stream.
csDecrypt.Read(decrypted, 0, decrypted.Length);
result = textConverter.GetString(de
return result;
}
#endregion public methods
}
}
ASKER
Unfortunately, I cannot change the encrypt method as thousands of users has registerd our site
so i can only change in dcrypt function
ASKER
In .Net 2003 it is working fine
I am using .Net 2005. So please help me for .Net 2005
I am using .Net 2005. So please help me for .Net 2005
I have tested your code from my end.
For data "This is a test", the encryption and decryption work file.
Are all encrypted data have the problem or just some of data have it?
For data "This is a test", the encryption and decryption work file.
Are all encrypted data have the problem or just some of data have it?
ASKER
If u have 5 character then it will encrypt and dycrypt fine but if it is more than 5 or the lenght of string is big then it is not decrypting. But it encrypts the any length of characters.
What are you using? Its is working fine on .Net 1.1 but it is not working fine on .Net 2.0
Where have you tested it.
Regards,
Dinesh
What are you using? Its is working fine on .Net 1.1 but it is not working fine on .Net 2.0
Where have you tested it.
Regards,
Dinesh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would suggest you write a small program, do a loop to decrypt all encrypted string, capture exception and in the exception output the dataToDecrypt. Then analyze those problem input.
Change
result = textConverter.GetString(en
To
result = Convert.ToBase64String(enc
And in decrypt
Change
toDecrypt = textConverter.GetBytes(dat
To
toDecrypt = Convert.FromBase64String(d
I am thinking there might be 0 value byte in your encrypted byte array, it could cause the string terminated.