134 lines
4.1 KiB
C#
134 lines
4.1 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Linq;
|
|
|
|
namespace AllBase
|
|
{
|
|
|
|
/// <summary>
|
|
/// 实现Base64加密解密
|
|
/// 作者:周公
|
|
/// 时间:2007
|
|
/// </summary>
|
|
public static class Base64Encode
|
|
{
|
|
/// <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
|
|
{
|
|
string code = Convert.ToBase64String(bytes);
|
|
return code;
|
|
}
|
|
catch
|
|
{
|
|
return source;
|
|
}
|
|
|
|
}
|
|
|
|
/// <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 = "";
|
|
try
|
|
{
|
|
byte[] bytes = Convert.FromBase64String(result);
|
|
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);
|
|
}
|
|
|
|
|
|
#region 判断是否是base64
|
|
/// <summary>
|
|
/// 是否base64字符串
|
|
/// </summary>
|
|
/// <param name="base64Str">要判断的字符串</param>
|
|
/// <returns></returns>
|
|
public static bool IsBase64(string base64Str)
|
|
{
|
|
byte[] bytes = null;
|
|
return IsBase64(base64Str, out bytes);
|
|
}
|
|
|
|
|
|
private static char[] base64CodeArray = new char[]
|
|
{
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '='
|
|
};
|
|
|
|
|
|
/// <summary>
|
|
/// 是否base64字符串
|
|
/// </summary>
|
|
/// <param name="base64Str">要判断的字符串</param>
|
|
/// <param name="bytes">字符串转换成的字节数组</param>
|
|
/// <returns></returns>
|
|
public static bool IsBase64(string base64Str, out byte[] bytes)
|
|
{
|
|
//string strRegex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
|
|
bytes = null;
|
|
if (string.IsNullOrEmpty(base64Str))
|
|
return false;
|
|
else
|
|
{
|
|
if (base64Str.Contains(","))
|
|
base64Str = base64Str.Split(',')[1];
|
|
if (base64Str.Length % 4 != 0)
|
|
return false;
|
|
if (base64Str.Any(c => !base64CodeArray.Contains(c)))
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
bytes = Convert.FromBase64String(base64Str);
|
|
return true;
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|