You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
270 lines
7.8 KiB
270 lines
7.8 KiB
2 months ago
|
using MESClassLibrary.Model;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Data;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
using System.Security.Cryptography;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace MESClassLibrary
|
||
|
{
|
||
|
public class Tool
|
||
|
{
|
||
|
public static string MD5encryption(string password)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (!string.IsNullOrWhiteSpace(password))
|
||
|
{
|
||
|
byte[] result = Encoding.Default.GetBytes(password); //tbPass为输入密码的文本框
|
||
|
MD5 md5 = new MD5CryptoServiceProvider();
|
||
|
byte[] output = md5.ComputeHash(result);
|
||
|
return BitConverter.ToString(output).Replace("-", "");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 算法偏移量
|
||
|
/// </summary>
|
||
|
const string m_IV = "12345678";
|
||
|
|
||
|
/// <summary>
|
||
|
/// 功能描述:根据输入的密钥生成8位密钥
|
||
|
/// 作 者: 爱给模板网 2gei.cn
|
||
|
/// 创建日期:2015-07-20 17:25:26
|
||
|
/// </summary>
|
||
|
/// <param name="strkey">strkey</param>
|
||
|
/// <returns>8位密钥</returns>
|
||
|
private static string GetKey(string strkey)
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(strkey))
|
||
|
{
|
||
|
strkey = "InfoColl";
|
||
|
}
|
||
|
if (strkey.Length % 8 == 0)
|
||
|
{
|
||
|
return strkey;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return GetKey(strkey + "0");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 功能描述:加密字符串
|
||
|
/// 作 者: 爱给模板网 2gei.cn
|
||
|
/// 创建日期:2015-07-20 17:18:31
|
||
|
/// 任务编号:
|
||
|
/// </summary>
|
||
|
/// <param name="strSourceString">原字符串</param>
|
||
|
/// <param name="strKey">密钥</param>
|
||
|
/// <returns>加密后的字符串</returns>
|
||
|
public static string Encrypt(string strSourceString, string strKey)
|
||
|
{
|
||
|
strKey = GetKey(strKey);
|
||
|
byte[] btKey = Encoding.UTF8.GetBytes(strKey);
|
||
|
|
||
|
byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
|
||
|
|
||
|
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||
|
|
||
|
using (MemoryStream ms = new MemoryStream())
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
byte[] inData = Encoding.UTF8.GetBytes(strSourceString);
|
||
|
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
|
||
|
{
|
||
|
cs.Write(inData, 0, inData.Length);
|
||
|
|
||
|
cs.FlushFinalBlock();
|
||
|
}
|
||
|
|
||
|
return Convert.ToBase64String(ms.ToArray());
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
return strSourceString;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 功能描述:解密字符串
|
||
|
/// 作 者: 爱给模板网 2gei.cn
|
||
|
/// 创建日期:2015-07-20 17:18:49
|
||
|
/// 任务编号:
|
||
|
/// </summary>
|
||
|
/// <param name="strEncryptedString">原字符串</param>
|
||
|
/// <param name="strKey">密钥</param>
|
||
|
/// <returns>解密后的字符串</returns>
|
||
|
public static string Decrypt(string strEncryptedString, string strKey)
|
||
|
{
|
||
|
strKey = GetKey(strKey);
|
||
|
byte[] btKey = Encoding.UTF8.GetBytes(strKey);
|
||
|
|
||
|
byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
|
||
|
|
||
|
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||
|
|
||
|
using (MemoryStream ms = new MemoryStream())
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
byte[] inData = Convert.FromBase64String(strEncryptedString);
|
||
|
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
|
||
|
{
|
||
|
cs.Write(inData, 0, inData.Length);
|
||
|
|
||
|
cs.FlushFinalBlock();
|
||
|
}
|
||
|
|
||
|
return Encoding.UTF8.GetString(ms.ToArray());
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
return strEncryptedString;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// DataTable转List
|
||
|
/// lx 2017-06-27
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
/// <param name="table"></param>
|
||
|
/// <returns></returns>
|
||
|
public static IList<T> ConvertTo<T>(DataTable table)
|
||
|
{
|
||
|
if (table == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
List<DataRow> rows = new List<DataRow>();
|
||
|
|
||
|
foreach (DataRow row in table.Rows)
|
||
|
{
|
||
|
rows.Add(row);
|
||
|
}
|
||
|
|
||
|
return ConvertTo<T>(rows);
|
||
|
}
|
||
|
|
||
|
public static IList<T> ConvertTo<T>(IList<DataRow> rows)
|
||
|
{
|
||
|
IList<T> list = null;
|
||
|
|
||
|
if (rows != null)
|
||
|
{
|
||
|
list = new List<T>();
|
||
|
|
||
|
foreach (DataRow row in rows)
|
||
|
{
|
||
|
T item = CreateItem<T>(row);
|
||
|
list.Add(item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
public static T CreateItem<T>(DataRow row)
|
||
|
{
|
||
|
T obj = default(T);
|
||
|
if (row != null)
|
||
|
{
|
||
|
obj = Activator.CreateInstance<T>();
|
||
|
|
||
|
foreach (DataColumn column in row.Table.Columns)
|
||
|
{
|
||
|
PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
|
||
|
try
|
||
|
{
|
||
|
object value = row[column.ColumnName];
|
||
|
prop.SetValue(obj, value, null);
|
||
|
}
|
||
|
catch
|
||
|
{ //You can log something here
|
||
|
//throw;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return obj;
|
||
|
}
|
||
|
public static D Mapper<D, S>(S s)
|
||
|
{
|
||
|
D d = Activator.CreateInstance<D>();
|
||
|
try
|
||
|
{
|
||
|
var Types = s.GetType();//获得类型
|
||
|
var Typed = typeof(D);
|
||
|
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
|
||
|
{
|
||
|
foreach (PropertyInfo dp in Typed.GetProperties())
|
||
|
{
|
||
|
if (dp.Name == sp.Name)//判断属性名是否相同
|
||
|
{
|
||
|
dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
return d;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 序列化扩展帮助类
|
||
|
/// </summary>
|
||
|
public static class SerializerHelper
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// List 类型序列化扩展
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
/// <param name="ls"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string ToSerializer<T>(this List<T> ls)
|
||
|
{
|
||
|
return JSONTools.ScriptSerialize(ls);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// JsonDataModel 类型序列化扩展
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
/// <param name="v"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string ToSerializer<T>(this JsonDataModel<T> v) where T : class, new()
|
||
|
{
|
||
|
return JSONTools.ScriptSerialize(v);
|
||
|
}
|
||
|
}
|
||
|
}
|