using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; namespace ICSSoft.FromERP { public class SRMUser { static string conStr = ICSHelper.GetConnectString(); public static void User() { string sqlUser = ""; string NewGuid = GuId(); sqlUser = @"INSERT INTO dbo.Sys_SRM_User ( F_Id ,F_Account ,F_RealName ,F_NickName , F_RoleId ,F_IsAdministrator , F_EnabledMark , F_CreatorTime ,F_CreatorUserId ,F_Location , F_VenCode) SELECT NEWID(),a.VenCode,a.VenName,a.VenName, '2691AB91-3010-465F-8D92-60A97425A45E',0,1, GETDATE(),'9f2ec079-7d0f-4fe2-90ab-8b09a8302aba','6000','' FROM dbo.ICSVendor a Left join Sys_SRM_User b on a.VenCode=b.F_Account where a.VenCode is null "; try { ICSHelper.ExecuteDate(conStr,sqlUser); } catch (Exception ex) { throw new Exception(ex.Message); } string UserSecretkey = Md5.md5(CreateNo(), 16).ToLower(); string pwd = Md5.md5(DESEncrypt.Encrypt(Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower(); sqlUser = @"INSERT INTO dbo.Sys_SRM_UserLogOn ( F_Id ,F_UserId ,F_UserPassword ,F_UserSecretkey ) SELECT a.F_Id,a.F_Id,'" + pwd + "','" + UserSecretkey + "'from Sys_SRM_User a left join Sys_SRM_UserLogOn b on a.F_Id=b.F_Id where a.F_Id is null "; try { ICSHelper.ExecuteDate(conStr, sqlUser); } catch (Exception ex) { throw new Exception(ex.Message); } } #region 自动生成编号 /// /// 表示全局唯一标识符 (GUID)。 /// /// public static string GuId() { return Guid.NewGuid().ToString(); } #endregion /// /// 自动生成编号 201008251145409865 /// /// public static string CreateNo() { Random random = new Random(); string strRandom = random.Next(1000, 10000).ToString(); //生成编号 string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如 return code; } /// /// MD5加密 /// public class Md5 { /// /// MD5加密 /// /// 加密字符 /// 加密位数16/32 /// public static string md5(string str, int code) { string strEncrypt = string.Empty; if (code == 16) { strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16); } if (code == 32) { strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); } return strEncrypt; } } public class DESEncrypt { private static string DESKey = "nfine_desencrypt_2016"; #region ========加密======== /// /// 加密 /// /// /// public static string Encrypt(string Text) { return Encrypt(Text, DESKey); } /// /// 加密数据 /// /// /// /// public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.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(); } #endregion #region ========解密======== /// /// 解密 /// /// /// public static string Decrypt(string Text) { if (!string.IsNullOrEmpty(Text)) { return Decrypt(Text, DESKey); } else { return ""; } } /// /// 解密数据 /// /// /// /// public static string Decrypt(string Text, string sKey) { 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(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion } } }