216 lines
5.8 KiB
C#
216 lines
5.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace MAX.Core.Cryptography;
|
|
|
|
public sealed class DESHelper
|
|
{
|
|
|
|
//默认密钥向量
|
|
private static readonly byte[] IVKey = { 36, 109, 97, 120, 111, 110, 120, 36 };
|
|
/// <summary>
|
|
/// DES加密字符串
|
|
/// </summary>
|
|
/// <param name="encryptString">待加密的字符串</param>
|
|
/// <param name="encryptKey">加密密钥,要求为8位</param>
|
|
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
|
|
public static string Encrypt(string encryptString, string encryptKey)
|
|
{
|
|
try
|
|
{
|
|
encryptKey = GetSubString(encryptKey, 8, "");
|
|
encryptKey = encryptKey.PadRight(8, ' ');
|
|
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
|
|
byte[] rgbIV = IVKey;
|
|
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
|
|
DES dCSP = DES.Create();
|
|
MemoryStream mStream = new();
|
|
CryptoStream cStream = new(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
|
cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cStream.FlushFinalBlock();
|
|
return Convert.ToBase64String(mStream.ToArray());
|
|
}
|
|
catch
|
|
{
|
|
return encryptString;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES解密字符串
|
|
/// </summary>
|
|
/// <param name="decryptString">待解密的字符串</param>
|
|
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
|
|
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
|
|
public static string Decrypt(string decryptString, string decryptKey)
|
|
{
|
|
try
|
|
{
|
|
decryptKey = GetSubString(decryptKey, 0, 8, "");
|
|
decryptKey = decryptKey.PadRight(8, ' ');
|
|
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
|
|
byte[] rgbIV = IVKey;
|
|
byte[] inputByteArray = Convert.FromBase64String(decryptString);
|
|
DES DCSP = DES.Create();
|
|
|
|
MemoryStream mStream = new();
|
|
CryptoStream cStream = new(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
|
cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cStream.FlushFinalBlock();
|
|
return Encoding.UTF8.GetString(mStream.ToArray());
|
|
}
|
|
catch
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
}
|
|
|
|
#region 截取字符串函数
|
|
|
|
/// <summary>
|
|
/// 截取字符串函数
|
|
/// </summary>
|
|
/// <param name="Str">所要截取的字符串</param>
|
|
/// <param name="Num">截取字符串的长度</param>
|
|
/// <returns></returns>
|
|
public static string GetSubString(string Str, int Num)
|
|
{
|
|
if (Str is null or "")
|
|
{
|
|
return "";
|
|
}
|
|
string outstr = string.Empty;
|
|
int n = 0;
|
|
foreach (char ch in Str)
|
|
{
|
|
n += Encoding.Default.GetByteCount(ch.ToString());
|
|
|
|
if (n > Num)
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
outstr += ch;
|
|
}
|
|
}
|
|
|
|
return outstr;
|
|
}
|
|
/// <summary>
|
|
/// 截取字符串函数,如果超过指定长度则将超出的部分用指定字符串代替
|
|
/// </summary>
|
|
/// <param name="Str">所要截取的字符串</param>
|
|
/// <param name="Num">截取字符串的长度</param>
|
|
/// <param name="LastStr">截取字符串后省略部分的字符串</param>
|
|
/// <returns></returns>
|
|
public static string GetSubString(string Str, int Num, string LastStr)
|
|
{
|
|
return GetSubString(Str, 0, Num, LastStr);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 截取字符串,超过部分用指定字符串替换
|
|
|
|
/* 宗波于 2010-6-12 日添加, 来源于DiscuzNt */
|
|
/// <summary>
|
|
/// 取指定长度的字符串
|
|
/// </summary>
|
|
/// <param name="p_SrcString">要检查的字符串</param>
|
|
/// <param name="p_StartIndex">起始位置</param>
|
|
/// <param name="p_Length">指定长度</param>
|
|
/// <param name="p_TailString">用于替换的字符串</param>
|
|
/// <returns>截取后的字符串</returns>
|
|
public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
|
|
{
|
|
string myResult = p_SrcString;
|
|
|
|
byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString);
|
|
foreach (char c in Encoding.UTF8.GetChars(bComments))
|
|
{ //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
|
|
if (c is (> '\u0800' and < '\u4e00') or (> '\xAC00' and < '\xD7A3'))
|
|
{
|
|
//if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
|
|
//当截取的起始位置超出字段串长度时
|
|
if (p_StartIndex >= p_SrcString.Length)
|
|
{
|
|
return "";
|
|
}
|
|
else
|
|
{
|
|
return p_SrcString.Substring(p_StartIndex,
|
|
p_Length + p_StartIndex > p_SrcString.Length ? p_SrcString.Length - p_StartIndex : p_Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (p_Length >= 0)
|
|
{
|
|
byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);
|
|
|
|
//当字符串长度大于起始位置
|
|
if (bsSrcString.Length > p_StartIndex)
|
|
{
|
|
int p_EndIndex = bsSrcString.Length;
|
|
|
|
//当要截取的长度在字符串的有效长度范围内
|
|
if (bsSrcString.Length > p_StartIndex + p_Length)
|
|
{
|
|
p_EndIndex = p_Length + p_StartIndex;
|
|
}
|
|
else
|
|
{ //当不在有效范围内时,只取到字符串的结尾
|
|
|
|
p_Length = bsSrcString.Length - p_StartIndex;
|
|
p_TailString = "";
|
|
}
|
|
|
|
int nRealLength = p_Length;
|
|
int[] anResultFlag = new int[p_Length];
|
|
byte[] bsResult = null;
|
|
|
|
int nFlag = 0;
|
|
for (int i = p_StartIndex; i < p_EndIndex; i++)
|
|
{
|
|
if (bsSrcString[i] > 127)
|
|
{
|
|
nFlag++;
|
|
if (nFlag == 3)
|
|
{
|
|
nFlag = 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
nFlag = 0;
|
|
}
|
|
|
|
anResultFlag[i] = nFlag;
|
|
}
|
|
|
|
if (bsSrcString[p_EndIndex - 1] > 127 && anResultFlag[p_Length - 1] == 1)
|
|
{
|
|
nRealLength = p_Length + 1;
|
|
}
|
|
|
|
bsResult = new byte[nRealLength];
|
|
|
|
Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);
|
|
|
|
myResult = Encoding.Default.GetString(bsResult);
|
|
myResult += p_TailString;
|
|
}
|
|
}
|
|
|
|
return myResult;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
}
|