0

I want to decrypt an HTTP response which is base64 encoded. I've also already got the fPassword and fSalt, but the offset of the response and length of the string I can not confirm.

Now I've got an prompt:

CryptographicException: Length of the data to decrypt is invalid.

Does it mean the offset or length of the data is not correct? What is the meaning of this error?

The exception happened when FlushfinalBlock was executing by the prompt.

   public static byte[] dd_1(string string_0)
    {
        byte[] buffer = Convert.FromBase64String(string_0);

        AesManaged managed = new AesManaged();
        Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(fPassword, fSalt);
        managed.BlockSize = managed.LegalBlockSizes[0].MaxSize;
        managed.KeySize = managed.LegalKeySizes[0].MaxSize;
        managed.Key = bytes.GetBytes(managed.KeySize / 8);
        managed.IV = bytes.GetBytes(managed.BlockSize / 8);
        ICryptoTransform transform = managed.CreateDecryptor();
        MemoryStream stream = new MemoryStream();
        CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
        stream2.Write(buffer, 0, buffer.Length);
        stream2.Close();
        return stream.ToArray();
    }
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Mr.ke
  • 65
  • 1
  • 9
  • How are you encrypting it? "Length of the data to decrypt is invalid" usually means you did something wrong on the encrypting side either forgetting to flush out the stream before reading the byte[] or encoding the byte[] to text improperly. – Scott Chamberlain Dec 11 '13 at 03:04
  • @Scott Chamberlain: Thanks! The data had not encrypted by me but by others. But we know the encrypt algorithm. I am not familiar with encrypt algorithm,just got the silverlight client souce by reversing. I am sure I've got the complete encrypted HTTP respone.So I assume that just offset which I use for cut from whole encrypted HTTP respone is wrong... – Mr.ke Dec 11 '13 at 03:32
  • 1
    My guess is that it essentially means "the data that you have given me is rubbish, I know because its not the right length". Have you read [Length of the data to decrypt is invalid](http://stackoverflow.com/questions/942139/length-of-the-data-to-decrypt-is-invalid) and [length of the data to decrypt is invalid in c#](http://stackoverflow.com/questions/4613143/length-of-the-data-to-decrypt-is-invalid-in-c-sharp)? Also you should test your code with data that you have encrypted (so you can verify that the data is definitely good) before testing with other peoples data. – Justin Dec 11 '13 at 09:22
  • @Justin: Thank you for the hint,let me give another try. – Mr.ke Dec 11 '13 at 11:10

0 Answers0