You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using Infrastructure.Helpers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Infrastructure.Utilities
|
|
{
|
|
public static class DESEncrypt
|
|
{
|
|
private static readonly string key = "hopetry###***";
|
|
|
|
//
|
|
// 摘要:
|
|
// 加密
|
|
//
|
|
// 参数:
|
|
// Text:
|
|
// 需要加密的内容
|
|
public static string Encrypt(string Text)
|
|
{
|
|
return Encrypt(Text, key);
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 加密数据
|
|
//
|
|
// 参数:
|
|
// Text:
|
|
// 需要加密的内容
|
|
//
|
|
// sKey:
|
|
// 秘钥
|
|
public static string Encrypt(string Text, string sKey)
|
|
{
|
|
DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
|
|
byte[] bytes = Encoding.Default.GetBytes(Text);
|
|
dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Md5Helper.Hash(sKey).ToUpper().Substring(0, 8));
|
|
dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Md5Helper.Hash(sKey).ToUpper().Substring(0, 8));
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
|
|
cryptoStream.Write(bytes, 0, bytes.Length);
|
|
cryptoStream.FlushFinalBlock();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
byte[] array = memoryStream.ToArray();
|
|
foreach (byte b in array)
|
|
{
|
|
stringBuilder.AppendFormat("{0:X2}", b);
|
|
}
|
|
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 解密
|
|
//
|
|
// 参数:
|
|
// Text:
|
|
// 需要解密的内容
|
|
public static string Decrypt(string Text)
|
|
{
|
|
if (!string.IsNullOrEmpty(Text))
|
|
{
|
|
return Decrypt(Text, key);
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 解密数据
|
|
//
|
|
// 参数:
|
|
// Text:
|
|
// 需要解密的内容
|
|
//
|
|
// sKey:
|
|
// 秘钥
|
|
public static string Decrypt(string Text, string sKey)
|
|
{
|
|
DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
|
|
int num = Text.Length / 2;
|
|
byte[] array = new byte[num];
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
|
|
array[i] = (byte)num2;
|
|
}
|
|
|
|
dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Md5Helper.Hash(sKey).ToUpper().Substring(0, 8));
|
|
dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Md5Helper.Hash(sKey).ToUpper().Substring(0, 8));
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
|
|
cryptoStream.Write(array, 0, array.Length);
|
|
cryptoStream.FlushFinalBlock();
|
|
return Encoding.Default.GetString(memoryStream.ToArray());
|
|
}
|
|
}
|
|
}
|