Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

C#
cs.Write(inputBytearray, 0, inputBytearray.Length);
                  cs.FlushFinalBlock();


here i am getting error like
'Length of the data to decrypt is invalid.'


any one help me

thanks.
Posted
Updated 26-Dec-11 18:38pm
v2

Dear Friend,

This error might come due to the encryption technique applied. you might have applied an encryption technique with lower bit and you are encrypting a larger value thats why you are not able to get the decryption write.

Please paste the encryption technique applied and also the string you are encrypting so that i can suggest you to what to do.

Also you can refer this link for reference:-

https://forums.asp.net/t/1225162.aspx[^]

Hope this will help you out.

Thanks
 
Share this answer
 
Comments
K N R 27-Dec-11 0:53am    
hi varun,

this is the encrypted code

public static string EncryptString(string key, string data)
{
if (key.Length != 8) throw new Exception("Key length must be equal 8.");
if (data.Length <= 0) throw new Exception("Length of string to encrypt can not be zero.");
byte[] inputBytearray=new byte[data.Length];
byte[] keyByteArray = new byte[key.Length];
string Result = "";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
keyByteArray = Encoding.UTF8.GetBytes(key);
inputBytearray = Encoding.UTF8.GetBytes(data);
using (CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(keyByteArray,inputBytearray),CryptoStreamMode.Write))
{
cs.Write(inputBytearray,0,inputBytearray.Length);
cs.FlushFinalBlock();
}
Result = Convert.ToBase64String(ms.ToArray());
}
return Result;

}
K N R 27-Dec-11 0:54am    
this one is decrypted code

public DataSet ReadEncrypteddataset()
{
DataSet set = new DataSet();
string Encrypteddata = "";
using (StreamReader reader = new StreamReader(Filepath))
{
Encrypteddata = reader.ReadToEnd();
}
string DecryptedData = DecryptString(myKey, Encrypteddata);
set.ReadXml(new StringReader(DecryptedData));
return set;
}

public static string DecryptString(string key, string data)
{
if (key.Length != 8) throw new Exception("Key length must be equal 8.");
if (data.Length <= 0) throw new Exception("Length of string to encrypt can not be zero.");
byte[] inputBytearray = new byte[data.Length];
byte[] keyByteArray = new byte[key.Length];
string Result = "";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
keyByteArray = Encoding.UTF8.GetBytes(key);
inputBytearray = Encoding.UTF8.GetBytes(data);
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(keyByteArray, inputBytearray), CryptoStreamMode.Write))
{
//byte[] plainTextBytes = new byte[inputBytearray.Length];
//int decryptedByteCount = cs.Write(plainTextBytes, 0, plainTextBytes.Length);

cs.Write(inputBytearray, 0, inputBytearray.Length);
cs.FlushFinalBlock();
}
Result = Encoding.UTF8.GetString(ms.ToArray());
}
return Result;
}
Dear KNR,

Try this code as this is perfectly fine for me. I hope this will help you out:-

public string EncryptString(string pwd)
        {
          try
          {
            byte[] strByte = new byte[pwd.Length];
            strByte = System.Text.Encoding.UTF8.GetBytes(pwd);
            string encodedPwd = Convert.ToBase64String(strByte);
            return encodedPwd;
          }
          catch
          {
            return null;
          }
        }
        public string DecryptString(string pwd)
        {
          try
          {
            UTF8Encoding encoder = new System.Text.UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();

            byte[] toDecode_byte = Convert.FromBase64String(pwd);
            int charCount = utf8Decode.GetCharCount(toDecode_byte, 0, toDecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(toDecode_byte, 0, toDecode_byte.Length, decoded_char, 0);
            string decodedPwd = new string(decoded_char);
            return decodedPwd;
          }
          catch
          {
            return null;
          }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900