C# Base64 加密解密

2009-12-28 17:42

using System;
using System.Text;

namespace wgscd
{
    
    public sealed class Base64
    {
        ///


        /// Base64加密
        ///

        /// 加密采用的编码方式
        /// 待加密的明文
        ///
        public static string EncodeBase64(Encoding encode, string source)
        {
            byte[] bytes = encode.GetBytes(source);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }

        ///


        /// Base64加密,采用utf8编码方式加密
        ///

        /// 待加密的明文
        /// 加密后的字符串
        public static string EncodeBase64(string source)
        {
            return EncodeBase64(Encoding.UTF8, source);
        }

        ///


        /// Base64解密
        ///

        /// 解密采用的编码方式,注意和加密时采用的方式一致
        /// 待解密的密文
        /// 解密后的字符串
        public static string DecodeBase64(Encoding encode, string result)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encode.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }

        ///


        /// Base64解密,采用utf8编码方式解密
        ///

        /// 待解密的密文
        /// 解密后的字符串
        public static string DecodeBase64(string result)
        {
            return DecodeBase64(Encoding.UTF8, result);
        }
    }
}


你可能感兴趣的:(C# Base64 加密解密)