I need to decrypt a message that was encrypted using 3DES in OFB mode.
I have an encrypted message. I have a key. I have an IV.
I'm on a .Net platform
The encrypted message is 24 chars long in base64. The key is 24 chars long in base64. and the IV is a 64-bit binary number.
Because of the lack of examples I tried using an ECB mode example, as follows:
public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
This is the error I get:
A Cryptographic error occurred: Specified key is not a valid size for this algorithm.
I've tried other code examples where I've changed the algorithm to OFB and it says it's not supported.
Can anyone please help me? I'm obviously out of my depth with this stuff so please be patient if I'm messing up somthing obvious.
There are loads of examples of 3DES decryption in ECB mode but little or nothing I can find about OFB mode.
The error message tells you precisely what the problem is: "Specified key is not a valid size for this algorithm."
You say that "The key is 24 chars long in base64". Base64 encodes 6 bits per char, so that's 144 bits in total. But a 3DES key should be 64 bits (==DES), 128 bits, or 196 bits. You have to either use a key of the appropriate length or work out what the library on the other end is doing to convert the key to an appropriate length.