三重Des解密坏数据错误

时间:2014-10-21 04:43:28

标签: c# encryption

我有一个加密密码,即(6A2E0108C610E71AAFC07A1BCC7594F),我希望这个加密密码使用T-Des技术解密我使用的密钥(123456789012345678901234567890123456789012345678) 但是当我解密时它会向我显示一个错误,即错误数据错误该怎么办?

我的代码是:

 public static string Decrypt(string cipherString, bool useHashing)
        {
        byte[] keyArray;
        byte[] toEncryptArray = new byte[1024];
        toEncryptArray=Convert.FromBase64String(cipherString);

       // System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = "123456789012345678901234567890123456789012345678";

        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider { KeySize = 192, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
       // TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
       // tdes.Mode = CipherMode.ECB;
        //tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        tdes.Clear();
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
}

0 个答案:

没有答案