Having trouble decrypting with TripleDes

May 12 2010 5:05 AM
I was given a problem to decrypt.  I was told that it was in base64 and that it was done with TripleDes.  I was given the key and IV.  For the most part, I figured it out, but one thing stumps me.  I think it should be written like this
        private void btnDecrypt_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String("ABvAsOKcGXqc5uQ4O5Z53isJaH31Pa8+PeddljE4mSU="); //Decode64();
ICryptoTransform tfrm;
MemoryStream mStream = new MemoryStream(data);
byte[] decrypted = new byte[data.Length];
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tfrm = tdes.CreateDecryptor(UTF8Encoding.UTF8.GetBytes("0123456789ABCDEF"), UTF8Encoding.UTF8.GetBytes("ABCDEFGH"));
CryptoStream encStream = new CryptoStream(mStream, tdes.CreateDecryptor(UTF8Encoding.UTF8.GetBytes("0123456789ABCDEF"), UTF8Encoding.UTF8.GetBytes("ABCDEFGH")), CryptoStreamMode.Read);
encStream.Read(decrypted, 0, decrypted.length);
txtDecrypted.Text = UTF8Encoding.UTF8.GetString(decrypted);
}

but this gives me a bad data error.  However, if I change the next to last line to

 encStream.Read(decrypted, 0, 24);
it works fine.  I found the number 24 by going through the debugger and finding out that it was trying to read 32 bytes where there were only 24.  Can someone explain to me how I would get the right size without hard coding it?

Thanks