C#使用3DES(三重数据标准标准)算法解密字符串

时间:2017-03-11 06:51:40

标签: c# encryption

我们有第三方应用程序,它使用3DES(三重数据加密标准)算法发送加密数据。

第三方应用程序发送以下加密字符串

WCgsTFmwAdy0OSVx57LqI / MFjrAdcn95QcbkzjuuqgY60avU + / O4R134j8LJ7 /分米

原始字符串这是字符串值,键是 KeyValue

我正在尝试使用以下Decrypt函数解密数据。

在此解释How to implement Triple DES in C# (complete example)

我试过CipherMode& PaddingMode组合找出使用过的CipherMode& PaddingMode但没有运气,它的返回垃圾值。

如何使用完美算法解密上述数据或识别加密算法

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);
}

0 个答案:

没有答案