init commit
This commit is contained in:
103
20220330_Vote/Ewide.RoadFlow/Utility/Encryption.cs
Normal file
103
20220330_Vote/Ewide.RoadFlow/Utility/Encryption.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 加解密类
|
||||
/// </summary>
|
||||
public class Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串MD5加密
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string MD5(string str)
|
||||
{
|
||||
var md5 = System.Security.Cryptography.MD5.Create();
|
||||
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||||
var strResult = BitConverter.ToString(result);
|
||||
return strResult.Replace("-", "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串SHA1加密
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string SHA1(string str)
|
||||
{
|
||||
var sha1 = System.Security.Cryptography.SHA1.Create();
|
||||
var result = sha1.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||||
var strResult = BitConverter.ToString(result);
|
||||
return strResult.Replace("-", "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加密KEY
|
||||
/// </summary>
|
||||
private static readonly string encryptKey = "4h!@w$rn";
|
||||
|
||||
/// <summary>
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="encryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string DESEncrypt(string encryptString)
|
||||
{
|
||||
try
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
byte[] inputByteArray;
|
||||
inputByteArray = Encoding.Default.GetBytes(encryptString);
|
||||
string md5SKey = encryptKey;
|
||||
des.Key = Encoding.ASCII.GetBytes(md5SKey);
|
||||
des.IV = Encoding.ASCII.GetBytes(md5SKey);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream 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();
|
||||
}
|
||||
catch { return ""; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 解密
|
||||
/// </summary>
|
||||
/// <param name="decryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string DESDecrypt(string decryptString)
|
||||
{
|
||||
try
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
int len;
|
||||
len = decryptString.Length / 2;
|
||||
byte[] inputByteArray = new byte[len];
|
||||
int x, i;
|
||||
for (x = 0; x < len; x++)
|
||||
{
|
||||
i = Convert.ToInt32(decryptString.Substring(x * 2, 2), 16);
|
||||
inputByteArray[x] = (byte)i;
|
||||
}
|
||||
string md5SKey = encryptKey;
|
||||
des.Key = Encoding.ASCII.GetBytes(md5SKey);
|
||||
des.IV = Encoding.ASCII.GetBytes(md5SKey);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cs.FlushFinalBlock();
|
||||
return Encoding.Default.GetString(ms.ToArray());
|
||||
}
|
||||
catch { return ""; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user