I found similar code for encoding/decoding strings in VB which works fine.
However I wanted to use it in a C# projected and can't get it to work.
When it executes the TransformFinalBlock() call at the end it always returns
the error
"System.Security.Cryptography.CryptographicExcepti on: Length of the data to
decrypt is invalid."
Can anyone see what I am doing wrong - I have tried playing with the lengths
and the construction of the byte array without any luck:
public string EncryptTripleDES(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
// Compute the MD5 hash.
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCI I.GetBytes(sKey));
// Set the cipher mode.
DES.Mode = CipherMode.ECB;
// Create the encryptor.
ICryptoTransform DESEncrypt = DES.CreateDecryptor();
// Tried with out any luck!
// int byteCount = System.Text.ASCIIEncoding.ASCII.GetByteCount(sIn);
// Byte[] Buffer = new Byte[byteCount];
byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn);
// Transform and return the string.
return Convert.ToBase64String(DESEncrypt.TransformFinalBl ock(Buffer, 0,
Buffer.Length));
}
Its this very last line that fails - I have tried Length-1 but that doesnt
work, I've also tried creatinf the byte array first, with same result
TIA
Tony