Base64加密 解密

Posted jeely

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
  }
}

 

以上是关于Base64加密 解密的主要内容,如果未能解决你的问题,请参考以下文章

Java Base64 加密/解密

base64随机字符混淆加密解密-美拍视频地址解密,反推加密算法

Java android DES+Base64加密解密

常见的加密和解密算法—BASE64

JAVA RSA加密解密代码范例(Base64版)

Base64加密