工具封装
public class DESEncrypt
{
public static string DESEnCode(string pToEncrypt)
{
return DESEncrypt.DESEnCode(pToEncrypt, "New-HackerHK");
}
//加密
public static string DESEnCode(string pToEncrypt, string Keys)
{
string result;
try
{
DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
descryptoServiceProvider.Mode = CipherMode.ECB;
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(pToEncrypt);
descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Keys.Substring(0, 8));
descryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Keys.Substring(0, 8));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
result = DESEncrypt.byteArr2HexStr(memoryStream.ToArray());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return result;
}
public static string byteArr2HexStr(byte[] arrB)
{
int num = arrB.Length;
StringBuilder stringBuilder = new StringBuilder(num * 2);
for (int i = 0; i < num; i++)
{
int j;
for (j = (int)arrB[i]; j < 0; j += 256)
{
}
if (j < 16)
{
stringBuilder.Append("0");
}
stringBuilder.Append(Convert.ToString(j, 16));
}
return stringBuilder.ToString();
}
public static string DESDeCode(string pToDecrypt)
{
return DESEncrypt.DESDeCode(pToDecrypt, "New-HackerHK");
}
//解密
public static string DESDeCode(string pToDecrypt, string Keys)
{
string @string;
try
{
DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
descryptoServiceProvider.Mode = CipherMode.ECB;
descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Keys.Substring(0, 8));
descryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Keys.Substring(0, 8));
byte[] array = DESEncrypt.hexStr2ByteArr(pToDecrypt);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(array, 0, array.Length);
cryptoStream.FlushFinalBlock();
memoryStream.Close();
@string = Encoding.GetEncoding("GB2312").GetString(memoryStream.ToArray());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return @string;
}
public static byte[] hexStr2ByteArr(string strIn)
{
int num = strIn.Length / 2;
byte[] array = new byte[num];
for (int i = 0; i < num; i++)
{
array[i] = (byte)Convert.ToInt32(strIn.Substring(i * 2, 2), 16);
}
return array;
}
public static string DES加密(string s原始字符串, bool b是否加密 = true)
{
string result;
if (!b是否加密)
{
result = s原始字符串;
}
else
{
try
{
string text = "";
int length = s原始字符串.Length;
for (int i = 0; i < length; i++)
{
text += DESEncrypt.DESEnCode(s原始字符串.Substring(i, 1));
}
result = text;
}
catch (Exception)
{
result = s原始字符串;
}
}
return result;
}
public static string DES解密(string s加密字符串)
{
string result;
try
{
if (s加密字符串.Length < 16)
{
result = s加密字符串;
}
else
{
string text = "";
int num = s加密字符串.Length / 16;
for (int i = 0; i < num; i++)
{
text += DESEncrypt.DESDeCode(s加密字符串.Substring(i * 16, 16));
}
result = text;
}
}
catch (Exception)
{
result = s加密字符串;
}
return result;
}
public static string GetChineseSpell(string strText)
{
int length = strText.Length;
string text = "";
for (int i = 0; i < length; i++)
{
text += DESEncrypt.getSpell(strText.Substring(i, 1));
}
return text;
}
public static string getSpell(string cnChar)
{
byte[] bytes = Encoding.Default.GetBytes(cnChar);
string result;
if (bytes.Length > 1)
{
int num = (int)bytes[0];
int num2 = (int)bytes[1];
int num3 = (num << 8) + num2;
int[] array = new int[]
{
45217,
45253,
45761,
46318,
46826,
47010,
47297,
47614,
48119,
48119,
49062,
49324,
49896,
50371,
50614,
50622,
50906,
51387,
51446,
52218,
52698,
52698,
52698,
52980,
53689,
54481
};
for (int i = 0; i < 26; i++)
{
int num4 = 55290;
if (i != 25)
{
num4 = array[i + 1];
}
if (array[i] <= num3 && num3 < num4)
{
return Encoding.Default.GetString(new byte[]
{
(byte)(65 + i)
});
}
}
result = "*";
}
else
{
result = cnChar;
}
return result;
}
public static string GetPYChar(string c)
{
byte[] array = new byte[2];
array = Encoding.Default.GetBytes(c);
int num = (int)((short)array[0] * 256 + (short)array[1]);
string result;
if (num < 45217)
{
result = "*";
}
else if (num < 45253)
{
result = "A";
}
else if (num < 45761)
{
result = "B";
}
else if (num < 46318)
{
result = "C";
}
else if (num < 46826)
{
result = "D";
}
else if (num < 47010)
{
result = "E";
}
else if (num < 47297)
{
result = "F";
}
else if (num < 47614)
{
result = "G";
}
else if (num < 48119)
{
result = "H";
}
else if (num < 49062)
{
result = "J";
}
else if (num < 49324)
{
result = "K";
}
else if (num < 49896)
{
result = "L";
}
else if (num < 50371)
{
result = "M";
}
else if (num < 50614)
{
result = "N";
}
else if (num < 50622)
{
result = "O";
}
else if (num < 50906)
{
result = "P";
}
else if (num < 51387)
{
result = "Q";
}
else if (num < 51446)
{
result = "R";
}
else if (num < 52218)
{
result = "S";
}
else if (num < 52698)
{
result = "T";
}
else if (num < 52980)
{
result = "W";
}
else if (num < 53689)
{
result = "X";
}
else if (num < 54481)
{
result = "Y";
}
else if (num < 55290)
{
result = "Z";
}
else
{
result = "*";
}
return result;
}
public const string THE_KEY = "New-HackerHK";
public const string IV_KEY = "231f4101b2a51b47";
}