C# Base64方式的编码与解码

编码与解码方法:

复制代码
        ///编码
        public static string EncodeBase64(string code_type, string code)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }
        ///解码
        public static string DecodeBase64(string code_type, string code)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }
复制代码

调用方法:

var val1 = EncodeBase64("utf-8", "<tr><td>你好啊</td></tr>");

var val2 = DecodeBase64("utf-8", "PHRyPjx0ZD7kvaDlpb3llYo8L3RkPjwvdHI+");

posted on 2019-03-23 17:22  心所欲  阅读(29381)  评论(0编辑  收藏  举报

导航