简单的加密解密(为了方便客户手动输入,不考虑安全性)

客户有时候需要手动输入激活码等,如果用base64加密会非常难以输入,所以设计成这样。

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetEncrypt(string source)
        {
    
    
            var bytes = Encoding.UTF8.GetBytes(source).Reverse();
            StringBuilder sb = new StringBuilder();
            foreach (var item in bytes)
            {
    
    
                string hexStr = Convert.ToString(item, 16);
                string hexStrRev = new string(hexStr.Reverse().ToArray());
                sb.AppendFormat(hexStrRev.ToUpper());
            }
            return sb.ToString();
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetDecrypt(string source)
        {
    
    
            int itemCount = 2;
            byte[] bytes = new byte[source.Length / itemCount];
            for (int i = 0; i < source.Length; i++)
            {
    
    
                if (i % itemCount == 0)
                {
    
    
                    string recStr = new string(source.Substring(i, itemCount).Reverse().ToArray());
                    bytes[i / itemCount] = Convert.ToByte(recStr, 16);
                }
            }
            bytes = bytes.Reverse().ToArray();
            return Encoding.UTF8.GetString(bytes);
        }

猜你喜欢

转载自blog.csdn.net/u010197227/article/details/133811849
今日推荐