using System; using System.Collections; 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); if (prop == null) continue; try { object value = row[column.ColumnName]; object convertedValue = ConvertDbNullToDefaultValue(value, prop.PropertyType); prop.SetValue(obj, convertedValue, null); //prop.SetValue(obj, value, null); } catch(Exception ex) { //You can log something here //throw; } } } return obj; } public static object ConvertDbNullToDefaultValue(object value, Type targetType) { if (value == DBNull.Value) { if (targetType.IsValueType) { return Activator.CreateInstance(targetType); // 对于值类型返回默认值 } return null; // 对于引用类型返回null } return value; } 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 DataTable ListToDataTable(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { //获取类型 Type colType = pi.PropertyType; //当类型为Nullable<>时 if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } result.Columns.Add(pi.Name, colType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } } }