码迷,mamicode.com
首页 > 其他好文 > 详细

Base64加密 解密

时间:2019-10-22 18:23:48      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:efault   thread   switch   enc   image   根据   ima   cas   decrypt   

技术图片

 

 

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;
    }
  }
}

 

Base64加密 解密

标签:efault   thread   switch   enc   image   根据   ima   cas   decrypt   

原文地址:https://www.cnblogs.com/Jeely/p/11721306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!