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;
}
}
///
/// 算法偏移量
///
const string m_IV = "12345678";
///
/// 功能描述:根据输入的密钥生成8位密钥
/// 作 者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:25:26
///
/// strkey
/// 8位密钥
private static string GetKey(string strkey)
{
if (string.IsNullOrEmpty(strkey))
{
strkey = "InfoColl";
}
if (strkey.Length % 8 == 0)
{
return strkey;
}
else
{
return GetKey(strkey + "0");
}
}
///
/// 功能描述:加密字符串
/// 作 者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:18:31
/// 任务编号:
///
/// 原字符串
/// 密钥
/// 加密后的字符串
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;
}
}
}
///
/// 功能描述:解密字符串
/// 作 者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:18:49
/// 任务编号:
///
/// 原字符串
/// 密钥
/// 解密后的字符串
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;
}
}
}
///
/// DataTable转List
/// lx 2017-06-27
///
///
///
///
public static IList ConvertTo(DataTable table)
{
if (table == null)
{
return null;
}
List rows = new List();
foreach (DataRow row in table.Rows)
{
rows.Add(row);
}
return ConvertTo(rows);
}
public static IList ConvertTo(IList rows)
{
IList list = null;
if (rows != null)
{
list = new List();
foreach (DataRow row in rows)
{
T item = CreateItem(row);
list.Add(item);
}
}
return list;
}
public static T CreateItem(DataRow row)
{
T obj = default(T);
if (row != null)
{
obj = Activator.CreateInstance();
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(S s)
{
D d = Activator.CreateInstance();
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;
}
}
///
/// 序列化扩展帮助类
///
public static class SerializerHelper
{
///
/// List 类型序列化扩展
///
///
///
///
public static string ToSerializer(this List ls)
{
return JSONTools.ScriptSerialize(ls);
}
///
/// JsonDataModel 类型序列化扩展
///
///
///
///
public static string ToSerializer(this JsonDataModel v) where T : class, new()
{
return JSONTools.ScriptSerialize(v);
}
}
}