138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Competition.Common.Util
|
|
{
|
|
public class EncryptionAndDecryption
|
|
{
|
|
#region ========加密========
|
|
|
|
/// <summary>
|
|
/// md5密码加密
|
|
/// </summary>
|
|
/// <param name="passWord"></param>
|
|
/// <returns></returns>
|
|
public static string GetMD5(string passWord)
|
|
{
|
|
MD5 md5 = new MD5CryptoServiceProvider();
|
|
byte[] bt = Encoding.Default.GetBytes(passWord);//将待加密字符转为 字节型数组
|
|
byte[] resualt = md5.ComputeHash(bt);//将字节数组转为加密的字节数组
|
|
string pwds = BitConverter.ToString(resualt).Replace("-", "");
|
|
passWord = pwds;
|
|
return passWord;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="Text"></param>
|
|
/// <returns></returns>
|
|
public static string Encrypt(string Text)
|
|
{
|
|
return Encrypt(Text, string.Format("NJ-{0}-ZGD", DateTime.Now.ToString("yyyy-MM-dd-HH")));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="Text"></param>
|
|
/// <returns></returns>
|
|
public static string EncryptByLgzn(string Text)
|
|
{
|
|
return Encrypt(Text, "LGZN");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="Text"></param>
|
|
/// <returns></returns>
|
|
public static string DecryptByLgzn(string Text)
|
|
{
|
|
return Decrypt(Text, "LGZN");
|
|
}
|
|
/// <summary>
|
|
/// 加密数据
|
|
/// </summary>
|
|
/// <param name="Text">明文</param>
|
|
/// <param name="sKey">密钥</param>
|
|
/// <returns></returns>
|
|
public static string Encrypt(string Text, string sKey)
|
|
{
|
|
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
|
byte[] inputByteArray;
|
|
inputByteArray = Encoding.Default.GetBytes(Text);
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(GetMD5(sKey).Substring(0, 8));
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(GetMD5(sKey).Substring(0, 8));
|
|
using (var ms = new MemoryStream())
|
|
using (var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
StringBuilder ret = new StringBuilder();
|
|
foreach (byte b in ms.ToArray())
|
|
{
|
|
ret.AppendFormat("{0:X2}", b);
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ========解密========
|
|
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="Text"></param>
|
|
/// <returns></returns>
|
|
public static string Decrypt(string Text)
|
|
{
|
|
return Decrypt(Text, string.Format("NJ-{0}-ZGD", DateTime.Now.ToString("yyyy-MM-dd-HH")));
|
|
}
|
|
/// <summary>
|
|
/// 解密数据
|
|
/// </summary>
|
|
/// <param name="Text">密文</param>
|
|
/// <param name="sKey">密钥</param>
|
|
/// <returns></returns>
|
|
public static string Decrypt(string Text, string sKey)
|
|
{
|
|
try
|
|
{
|
|
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
|
int len;
|
|
len = Text.Length / 2;
|
|
byte[] inputByteArray = new byte[len];
|
|
int x, i;
|
|
for (x = 0; x < len; x++)
|
|
{
|
|
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
|
|
inputByteArray[x] = (byte)i;
|
|
}
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(GetMD5(sKey).Substring(0, 8));
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(GetMD5(sKey).Substring(0, 8));
|
|
using (var ms = new System.IO.MemoryStream())
|
|
using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
return Encoding.Default.GetString(ms.ToArray());
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|