Base64加密 解密

Base64加密 解密_第1张图片

 

 

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

namespace CommonLib {
  public class Decryptions {
    /// 
    /// Base64解密
    /// 
    /// 解密采用的编码方式,注意:其与加密时采用的编码方式一致
    /// 待解密的密文
    /// 
    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;
    }

    /// 
    /// Base64解密,采用utf8编码方式解密
    /// 
    /// 待解密的密文
    /// 解密后的明文
    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 {

    /// 
    /// Base64加密
    /// 
    /// 加密采用的编码方式
    /// 待加密的明文
    /// 
    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;
    }

    /// 
    /// Base64加密,采用utf8编码方式加密
    /// 
    /// 待加密的明文
    /// 
    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 {
    /// 
    /// 获取所有支持的加密解密方式
    /// 
    /// 
    public static List<string> GetModeList() {
      List<string> encriptTypeList = new List<string>() {
       "默认","utf-8","utf-7","ascii","unicode","utf-32"
      };
      return encriptTypeList;
    }

    /// 
    /// 根据字符串,获取加密解密方式
    /// 
    /// 
    /// 
    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;
    }
  }
}

 

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