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);
}
14
  • Are you sure you are using the right key. Try to encrypt and decrypt a string using the same key. See what happens. Commented Mar 11, 2017 at 7:05
  • 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. Commented Mar 11, 2017 at 7:09
  • 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 Commented Mar 11, 2017 at 8:31
  • 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. Commented Mar 11, 2017 at 8:40
  • 1
    There are simply too many options 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. Commented Mar 11, 2017 at 9:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.