C#中Base64之编码,解码方法

编码
public 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 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;
}

 

简洁编码:

     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
     return Convert.ToBase64String(bytes);

简洁解码:

    byte[] bytes = Convert.FromBase64String(str);
    return System.Text.Encoding.UTF8.GetString(bytes);

 

 

 

 

MD5/Base64/DES是常用来对用户身份验证的算法,本人陆续公开了自己常用的这三种算法的源代码,不足之处,希望大家批评指正。
 
using System;
using System.Text;

namespace Common
{
         /// <summary>
         /// 实现Base64加密解密
         /// 作者:周公
         /// 时间:2007
         /// </summary>
         public sealed class Base64
        {
                 /// <summary>
                 /// Base64加密
                 /// </summary>
                 /// <param name="codeName">加密采用的编码方式</param>
                 /// <param name="source">待加密的明文</param>
                 /// <returns> </returns>
                 public static string EncodeBase64(Encoding encode, string source)
                {
                         byte[] bytes = encode.GetBytes(source);
                         try
                        {
                                encode = Convert.ToBase64String(bytes);
                        }
                         catch
                        {
                                encode = source;
                        }
                         return encode;
                }

                 /// <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="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
                 /// <param name="result">待解密的密文</param>
                 /// <returns>解密后的字符串</returns>
                 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;
                }

                 /// <summary>
                 /// Base64解密,采用utf8编码方式解密
                 /// </summary>
                 /// <param name="result">待解密的密文</param>
                 /// <returns>解密后的字符串</returns>
                 public static string DecodeBase64( string result)
                {
                         return DecodeBase64(Encoding.UTF8, result);
                }
        }
}

你可能感兴趣的:(base64)