• Base64加密解密方法


    Base64加密解密方法

        public class Base64Helper
        {
            /// <summary>
            /// Base64加密
            /// </summary>
            /// <param name="encode">加密采用的编码方式</param>
            /// <param name="source">待加密的明文</param>
            /// <returns></returns>
            public static string EncodeBase64(Encoding encode, string source)
            {
                try
                {
                    byte[] bytes = encode.GetBytes(source);
                    var result = Convert.ToBase64String(bytes);
                    return result;
                }
                catch (Exception ex)
                {
                    Log4Helper.GetInstance().Error(ex);
                    return "编码错误";
                }
            }
    
            /// <summary>
            /// Base64加密,采用utf8编码方式加密
            /// </summary>
            /// <param name="source">待加密的明文</param>
            /// <returns>加密后的字符串</returns>
            public static string EncodeBase64(string source)
            {
                return EncodeBase64(Encoding.UTF8, source);
            }
    
            /// <summary>
            /// Base64解密
            /// </summary>
            /// <param name="encode">解密采用的编码方式,注意和加密时采用的方式一致</param>
            /// <param name="result">待解密的密文</param>
            /// <returns>解密后的字符串</returns>
            public static string DecodeBase64(Encoding encode, string result)
            {
                string decode = "";
                try
                {
                    byte[] bytes = Convert.FromBase64String(result);
                    decode = encode.GetString(bytes);
                }
                catch
                {
                    decode = result;
                }
                return decode;
            }
    
            /// <summary>
            /// Base64解密,采用utf8编码方式解密
            /// </summary>
            /// <param name="result">待解密的密文</param>
            /// <returns>解密后的字符串</returns>
            public static string DecodeBase64(string result)
            {
                return DecodeBase64(Encoding.UTF8, result);
            }
        }

  • 相关阅读:
    mysql 版本查看
    js 中文乱码
    浏览器内核
    Squid 代理服务器
    minicygwin
    firefox 插件开发
    ocx c++
    NetBeans
    android 虚拟机
    ExpandableListView
  • 原文地址:https://www.cnblogs.com/jiangqw/p/12121237.html
Copyright © 2020-2023  润新知