0

We have a third party application which sends encrypted data using 3DES (Triple Data Encryption Standard) algorithm.

Third party application sends below encrypted string

WCgsTFmwAdy0OSVx57LqI/MFjrAdcn95QcbkzjuuqgY60avU+/O4R134j8LJ7/dm

for original string This is string value and key is KeyValue

I am trying to Decrypt data using below Decrypt function.

explained here How to implement Triple DES in C# (complete example)

i have tried CipherMode & PaddingMode combinations to find out used CipherMode & PaddingMode but no luck, its returning garbage values.

How can i Decrypt above data using perfect algorithm or identify Encryption algorithm.

string[] modes = Enum.GetNames(typeof(CipherMode));
string[] paddings = Enum.GetNames(typeof(PaddingMode));

foreach (string mode in modes)
{
    foreach (string pad in paddings)
    {
        try
        {

            string decryptstr = Decrypt("WCgsTFmwAdy0OSVx57LqI/MFjrAdcn95QcbkzjuuqgY60avU+/O4R134j8LJ7/dm",true, "KeyValue",(CipherMode)Enum.Parse(typeof(CipherMode),mode), (PaddingMode)Enum.Parse(typeof(PaddingMode), pad));
            Console.WriteLine(decryptstr);
        }
        catch
        {

        }
    }    
}


public static string Decrypt(string cipherString, bool useHashing, string key, CipherMode mode, PaddingMode paddingmode)
{
    byte[] keyArray;
    //get the byte code of the string

    byte[] toEncryptArray = Convert.FromBase64String(cipherString);




    if (useHashing)
    {
        //if hashing was used get the hash code with regards to your key
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        //release any resource held by the MD5CryptoServiceProvider

        hashmd5.Clear();
    }
    else
    {
        //if hashing was not implemented get the byte code of the key
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    //set the secret key for the tripleDES algorithm
    tdes.Key = keyArray;
    //mode of operation. there are other 4 modes. 
    //We choose ECB(Electronic code Book)

    tdes.Mode = mode;// CipherMode.ECB;
    //padding mode(if any extra byte added)
    tdes.Padding = paddingmode;//; PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(
                         toEncryptArray, 0, toEncryptArray.Length);
    //Release resources held by TripleDes Encryptor                
    tdes.Clear();
    //return the Clear decrypted TEXT
    return UTF8Encoding.UTF8.GetString(resultArray);
}
Community
  • 1
  • 1
Azar Shaikh
  • 445
  • 9
  • 26
  • Are you sure you are using the right key. Try to encrypt and decrypt a string using the same key. See what happens. – Amir H. Bagheri Mar 11 '17 at 07:05
  • Yes @AHBagheri. Third party application Decrypt value correctly with this key. but i dont want to use third party application for decryption – Azar Shaikh Mar 11 '17 at 07:08
  • 1
    I understand that. I am suggesting use the same source's encrypt method to encrypt and decrypt a known string with a known key just to ensure that it is working properly. – Amir H. Bagheri Mar 11 '17 at 07:09
  • @AHBagheri. i have tested encrypting and Decryption working properly with third party application with known string and known key.Is there any way to decrypt this string in c# – Azar Shaikh Mar 11 '17 at 07:14
  • Set useHashing to false. I doubt whether it needs to be set true. – Amir H. Bagheri Mar 11 '17 at 07:27
  • @AHBagheri when set useHashing to false, throws excxeption Specified key is not a valid size for this algorithm. Thanks for your efforts – Azar Shaikh Mar 11 '17 at 07:38
  • You could also check your string here. – Amir H. Bagheri Mar 11 '17 at 07:46
  • 1
    Probably you are missing a piece here... Your code **is correct**. I've even tested for other hashing algorithms (SHA1, SHA256) and for AES, but still no correct decryption – xanatos Mar 11 '17 at 08:31
  • "KeyValue" is 8 bytes long and as such a valid DES key, 3DES not so much. By default C# uses 3DES EDE (Encrypt-Decrypt-Encrypt) where the first and last 8 bytes should be different from the middle 8 bytes. If you only have an 8 byte key, 3DES EDE doesn't make sense, but 3DES EEE would. If you hash your "KeyValue" with MD5, you get a 16 byte key which is usable for 3DES EDE and 3DES EEE through the normal key expansion. – Artjom B. Mar 11 '17 at 08:34
  • 1
    I'll add that, given the length of the base64-decoded encrypted string (48), I'll say that the encoding used is Unicode, because *This is string value* is only 20 bytes long in UTF8. – xanatos Mar 11 '17 at 08:40
  • 1
    There are simply too [many options](http://stackoverflow.com/a/31734468/1816580) to try. This question is not answerable, because it is just guessing. You should look for a specification of encryption in the third party application or ask for it. – Artjom B. Mar 11 '17 at 09:33
  • Thanks all for your efforts – Azar Shaikh Mar 11 '17 at 09:56
  • As already said, too many variables here. But your clear string is 20 bytes, the expected length of encrypting that is 32. Your encrypted data is 48 bytes, so you probably have a 16 byte header. Find out what size the IV is, I think it's 8. That leaves 8 bytes unaccounted for, maybe a Salt for the key? How sure are you that MD5 is used? – H H Mar 11 '17 at 11:29
  • And your current code uses the default IV from the decryptor, so all those CB modes don't have a chance. – H H Mar 11 '17 at 11:32

0 Answers0