System.EncryptHelper.DecryptStr C# (CSharp) Method

DecryptStr() public static method

解密字符串
public static DecryptStr ( this str, string key = "s?]8!sj;d" ) : string
str this this
key string 秘钥
return string
        public static string DecryptStr(this string str, string key = "s?]8!sj;d")
        {
            var des = DES.Create();
            var inputBytes = new byte[str.Length / 2];
            for (var x = 0; x < str.Length / 2; x++)
            {
                inputBytes[x] = (byte)System.Convert.ToInt32(str.Substring(x * 2, 2), 16);
            }
            var keyByteArray = Encoding.UTF8.GetBytes(key);
            var ha = new SHA1Managed();
            var hb = ha.ComputeHash(keyByteArray);
            var sKey = new byte[8];
            var sIv = new byte[8];
            for (var i = 0; i < 8; i++)
                sKey[i] = hb[i];
            for (var i = 8; i < 16; i++)
                sIv[i - 8] = hb[i];
            des.Key = sKey;
            des.IV = sIv;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputBytes, 0, inputBytes.Length);
                    cs.FlushFinalBlock();
                    return ClearUp(Encoding.UTF8.GetString(ms.ToArray()));
                }
            }
        }