C# Base64解码小工具编写

1 界面如下

2 关键代码如下

var reqStr = txtInput.Text.Trim();//输入字符串
            var resStr = "";//输出结果
            var errMsg = "";
            if (reqStr.Length > 0)
            {
                try
                {
                    //针对复杂实体对象或Dictionary<int,List<int,int>>这样键值对动态解密
                    var callback = reqStr.Deserialize<dynamic>();
                    resStr = Newtonsoft.Json.JsonConvert.SerializeObject(callback);
                }
                catch (Exception ex)
                {
                    try
                    {
                        //默认通用字符串解密
                        byte[] bytes = Convert.FromBase64String(reqStr);
                        resStr = Encoding.Default.GetString(bytes);
                    }
                    catch (Exception ex2)
                    {
                        errMsg = ex2.Message;
                    }
                }
            }
View Code

说明:

2.1 reqStr输入字符串 先针对复杂实体对象或Dictionary<int,List<int,int>>这样键值对动态解密

2 .2默认通用字符串Base64解密

 

3 Deserialize解密方法如下

 public static T Deserialize<T>(this string source)
        {
            T val = default(T);
            IFormatter formatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                byte[] array = Convert.FromBase64String(source);
                memoryStream.Write(array, 0, array.Length);
                memoryStream.Seek(0L, SeekOrigin.Begin);
                return (T)formatter.Deserialize(memoryStream);
            }
        }
View Code

 

4 源码下载链接 https://download.csdn.net/download/u013716500/13092088

posted @ 2020-11-06 15:33  Mark1997  阅读(184)  评论(0编辑  收藏  举报