Base64加密 解密

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLib {
  public class Decryptions {
    /// <summary>
    /// Base64解密
    /// </summary>
    /// <param name="encode">解密采用的编码方式,注意:其与加密时采用的编码方式一致</param>
    /// <param name="result">待解密的密文</param>
    /// <returns></returns>
    public static string DecodeBase64(Encoding encode, string result) {
      var decode = string.Empty;
      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);
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLib {
  public class Encryptions {

    /// <summary>
    /// Base64加密
    /// </summary>
    /// <param name="encode">加密采用的编码方式</param>
    /// <param name="source">待加密的明文</param>
    /// <returns></returns>
    public static string EncodeBase64(Encoding encode, string source) {
      var enStr = string.Empty;
      byte[] bytes = encode.GetBytes(source);
      try {
        enStr = Convert.ToBase64String(bytes);
      } catch {
        enStr = source;
      }
      return enStr;
    }

    /// <summary>
    /// Base64加密,采用utf8编码方式加密
    /// </summary>
    /// <param name="source">待加密的明文</param>
    /// <returns></returns>
    public static string EncodeBase64(string source) {
      return EncodeBase64(Encoding.UTF8, source);
    }

  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLib {
  public class Mode {
    /// <summary>
    /// 获取所有支持的加密解密方式
    /// </summary>
    /// <returns></returns>
    public static List<string> GetModeList() {
      List<string> encriptTypeList = new List<string>() {
       "默认","utf-8","utf-7","ascii","unicode","utf-32"
      };
      return encriptTypeList;
    }

    /// <summary>
    /// 根据字符串,获取加密解密方式
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    public static Encoding GetMode(string mode) {
      Encoding m = Encoding.UTF8;
      switch (mode) {
        case "默认":
        case "utf-8":
          m = Encoding.UTF8;
          break;
        case "utf-7":
          m = Encoding.UTF7;
          break;

        case "ascii":
          m = Encoding.ASCII;
          break;

        case "unicode":
          m = Encoding.Unicode;
          break;

        case "utf-32":
          m = Encoding.UTF32;
          break;
        default:
          return m;

      }
      return m;
    }
  }
}

 

posted @ 2019-10-22 17:51  DarJeely  阅读(293)  评论(0编辑  收藏  举报