Can I find the key in Triple DES encryption algorithm if I have IV, encryption data and the resulting value?

StackOverflow https://stackoverflow.com/questions/7709370

  •  13-02-2021
  •  | 
  •  

Question

This is not meant for any kind of hacking, I am just trying my hands on Triple DES encryption technique in .NET. I have some test data with me. I have to get the result with the given data. I do also have the result. But the value never matches. I am sure the code is fine (will give that below). So I thought the only possibility could be the key is wrong and so lets find the key and see if that matches. Below is my code:

byte[] key = StringToByteArray("5b70649d4ae0bf2af891c167514aa7515b70649d4ae0bf2a");
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] bytes = Encoding.ASCII.GetBytes("95e4d77c6d6c6c993333303533303833");
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
tripleDes.Mode = CipherMode.CBC;
//tripleDes.Padding = PaddingMode.None;
tripleDes.Key = key;
tripleDes.IV = iv;
MemoryStream ms = new MemoryStream();
ICryptoTransform encryptor = tripleDes.CreateEncryptor();

CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
byte[] cipher = ms.ToArray();
string finalValue = ByteToString(cipher, 16); //ByteToString method will convert byte array to string.

The final result is 1B529558534F43D15556AD65C7E396D5674EE8A09E0A29A84389020EF820AC51B7D5E1B33BDA18A2. Since I need only 16 bytes I have converted only first 16 bytes to hex 1B529558534F43D15556AD65C7E396D5.

But the expected result is 76db821f5c7af12dc8d70a6a79cfcb77 If I can find the key, I may know that there needs to be some kind of processing required in the key as well.

Thanks for the help!

Was it helpful?

Solution

While its certainly possible, I don't think the TripleDESCryptoServiceProvider class is designed to work in "reverse" to give you a key when you have the cypher, IV and the unencrypted data. Why should it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top