Ejemplo n.º 1
0
        /// <summary>
        /// 把指定的已经加密的并且可以通过Url传递的字符串解密
        /// </summary>
        /// <param name="encryptedUrlString"></param>
        /// <param name="validateTimestamp">是否验证时间戳</param>
        /// <param name="allowDelayTicks"></param>
        /// <returns></returns>
        private static string DecryptUrlString(string encryptedUrlString, bool validateTimestamp, long allowDelayTicks)
        {
            string result = HttpUtility.UrlDecode(encryptedUrlString);

            result = EncodingUtility.ToGb2312StringFromBase64(result);
            result = DecryptByTriple(result);

            //如果用户没有传入数据,则直接忽略
            if (result.Trim() == string.Empty)
            {
                return(result);
            }

            //现在都携带了时间戳,需要把时间戳去掉
            if (result.IndexOf("-", StringComparison.Ordinal) == -1)
            {
                throw new Exception("需要解密的字符串格式不正确");
            }
            string rawTicks = result.Substring(0, result.IndexOf("-", StringComparison.Ordinal));
            long   lRawTicks;

            if (!long.TryParse(rawTicks, out lRawTicks))
            {
                throw new Exception("需要解密的字符串格式不正确");
            }

            if (validateTimestamp && allowDelayTicks > 0)
            {
                long factDelayTicks = DateTime.Now.Ticks - lRawTicks;
                if (factDelayTicks > allowDelayTicks)
                {
                    throw new Exception("需要解密的字符串数据不正确");
                }
            }

            return(result.Remove(0, result.IndexOf("-", StringComparison.Ordinal) + 1));
        }