using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using STEE.SaudiMJCCTV.VideoApplication.Utility;
using System.Collections;
using System.Windows.Forms;
namespace STEE.SaudiMJCCTV.VideoApplication
{
    public class userListFormParameters
    {
        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string userMac;
        public string UserMac
        {
            get { return userMac; }
            set { userMac = value; }
        }
        private string userLevel;
        public string UserLevel
        {
            get { return userLevel; }
            set { userLevel = value; }
        }
        public userListFormParameters(string userName, string userMac, string userLevel)
        {
            this.userName = userName;
            this.userMac = userMac;
            this.userLevel = userLevel;
        }
    }
    class userInfoToXml
    {
        static String XmlPath = Application.StartupPath + "\\" + System.Configuration.ConfigurationManager.AppSettings["UserXmlPath"];
        /// <summary>
        ///得到用户列表
        /// </summary>
        /// <returns></returns>
        public static List<userListFormParameters> getParmList()
        {
            try
            {
                List<userListFormParameters> parmList = new List<userListFormParameters>();
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList nodelist = root.SelectNodes("userInfo");
                foreach (XmlNode node in nodelist)
                {
                    string userName = node.SelectSingleNode("userName").InnerText.ToString();
                    string userMac = node.SelectSingleNode("userMac").InnerText.ToString();
                    string userLevel = node.SelectSingleNode("userLevel").InnerText.ToString();
                    parmList.Add(new userListFormParameters(userName, userMac, userLevel));
                }
                return parmList;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 检查用户名是否存在,存在则反回用户节点
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static XmlNode checkuser(string userName)
        {
            try
            {
                XmlNode userNode = null;
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList nodelist = root.SelectNodes("userInfo");
                foreach (XmlNode node in nodelist)
                {
                    if (node.SelectSingleNode("userName").InnerText.Equals(userName))
                        userNode = node;
                }
                return userNode;
            }
            catch (Exception)
            {
                return null;
            }
        }
       /// <summary>
        /// 删除用户
       /// </summary>
       /// <param name="userName"></param>
        static public void deleteUser(string userName)
        {
            try
            {
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList nodelist = root.SelectNodes("userInfo");
                foreach (XmlNode node in nodelist)
                {
                    if (node.SelectSingleNode("userName").InnerText.Equals(userName))
                    {
                        root.RemoveChild(node);
                        Xmlroot.Save(XmlPath);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
       /// <summary>
        /// 编辑用户
       /// </summary>
       /// <param name="userName"></param>
       /// <param name="pwd"></param>
       /// <param name="Mac"></param>
        static public void editUser(string userName, string pwd, string Mac)
        {
            try
            {
                string Md5Pwd = Key.MD5Encrypt(pwd);
                string saveAesPwd = Key.AESEncrypt(Md5Pwd, "key");
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList nodelist = root.SelectNodes("userInfo");
                foreach (XmlNode node in nodelist)
                {
                    if (node.SelectSingleNode("userName").InnerText.Equals(userName))
                    {
                        string[] list = STEE.SaudiMJCCTV.Admin.SetTing.rithtList;
                        foreach (string str in list)
                        {
                            string savepwd = userInfoToXml.getUserPwdByName(userName);
                            if (userInfoToXml.checkPermissions(userName, savepwd, str))
                            {
                                node.SelectSingleNode(str).InnerText = GenerationPermissions(userName, saveAesPwd, str);
                            }
                        }
                        node.SelectSingleNode("userName").InnerText = userName;
                        node.SelectSingleNode("userPwd").InnerText = saveAesPwd;
                        node.SelectSingleNode("userMac").InnerText = Mac;
                        Xmlroot.Save(XmlPath);
                    }
                }
            }
            catch (Exception)
            {
                
            }
        }
        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="chk232"></param>
        /// <param name="chkAlarm"></param>
        /// <param name="chkChannel"></param>
        /// <param name="chkCommon"></param>
        /// <param name="chkDiskManage"></param>
        /// <param name="chkException"></param>
        /// <param name="chkHelp"></param>
        /// <param name="chkLocalLogQuery"></param>
        /// <param name="chkMaintenance"></param>
        /// <param name="chkManualAlarm"></param>
        /// <param name="chkNetwork"></param>
        /// <param name="chkPreview"></param>
        /// <param name="chkShutdown"></param>
        /// <param name="chkSuperAdmin"></param>
        /// <param name="chkTrain"></param>
        /// <param name="chkVideoBackup"></param>
        /// <param name="chkVideoSearch"></param>
        /// <param name="chkVideo"></param>
        /// <param name="chkcfgManage"></param>
        /// <param name="btnShutdownCctv"></param>
        static public void setRight(string userName, bool chk232,
            bool chkAlarm, bool chkChannel, bool chkCommon,
            bool chkDiskManage, bool chkException, bool chkHelp,
            bool chkLocalLogQuery, bool chkMaintenance, bool chkManualAlarm,
            bool chkNetwork, bool chkPreview, bool chkShutdown,
            bool chkSuperAdmin, bool chkTrain, bool chkVideoBackup,
            bool chkVideoSearch, bool chkVideo, bool chkcfgManage,
            bool btnShutdownCctv)
        {
            try
            {
             
                //权限得到权限map
                Hashtable map = userInfoToXml.getMap(chk232, chkAlarm, chkChannel, chkCommon,
                 chkDiskManage, chkException, chkHelp, chkLocalLogQuery, chkMaintenance,
                 chkManualAlarm, chkNetwork, chkPreview, chkShutdown, chkSuperAdmin,
                 chkTrain, chkVideoBackup, chkVideoSearch, chkVideo, chkcfgManage, btnShutdownCctv);
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList nodelist = root.SelectNodes("userInfo");
                foreach (XmlNode node in nodelist)
                {
                    if (node.SelectSingleNode("userName").InnerText.Equals(userName))
                    {
                        //给所有权限斌空值
                        string[] list = STEE.SaudiMJCCTV.Admin.SetTing.rithtList;
                        foreach (string str in list)
                        {
                            node.SelectSingleNode(str).InnerText = "";
                        }
                        //遍历Map,给所有选中权限斌值
                        foreach (DictionaryEntry de in map)
                        {
                            if (null != de.Value)
                            {
                                string pwd = node.SelectSingleNode("userPwd").InnerText;
                                node.SelectSingleNode(de.Key.ToString()).InnerText = GenerationPermissions(userName, pwd, de.Value.ToString());
                            }
                        }
                        Xmlroot.Save(XmlPath);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
       /// <summary>
        /// 检查密码是否正确
       /// </summary>
       /// <param name="userName"></param>
       /// <param name="pwd"></param>
       /// <returns></returns>
        static public bool checkPwd(string userName, string pwd)
        {
            try
            {
                string Md5Pwd = Key.MD5Encrypt(pwd);
                XmlNode userNode = checkuser(userName);
                string saveAesPwd = userNode.SelectSingleNode("userPwd").InnerText;
                string saveMd5Pwd = Key.AESDecrypt(saveAesPwd, "key");
                if (null != saveMd5Pwd && Md5Pwd != null && saveMd5Pwd.Equals(Md5Pwd))
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        static public void createUser(string userName, string pwd)
        {
            try
            {
                string Md5Pwd = Key.MD5Encrypt(pwd);
                string saveAesPwd = Key.AESEncrypt(Md5Pwd, "key");
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNode userNode = root.SelectSingleNode("userInfo").CloneNode(true);
                //置空所有节点
                foreach (XmlNode n in userNode.ChildNodes)
                {
                    n.InnerText = "";
                }
                userNode.SelectSingleNode("userName").InnerText = userName;
                userNode.SelectSingleNode("userPwd").InnerText = saveAesPwd;
                root.AppendChild(userNode);//加入用户信息节点
                Xmlroot.Save(XmlPath);
            }
            catch (Exception)
            {
            }
        }
        static public string readPermissions(XmlNode userNode, string PermissionsName)
        {
            try
            {
                return userNode.SelectSingleNode(PermissionsName).InnerText;
            }
            catch (Exception)
            {
                return null;
            }
        }
        static public string setPermissions(XmlNode userNode, string PermissionsName, string value)
        {
            try
            {
                return userNode.SelectSingleNode(PermissionsName).InnerText = value;
            }
            catch (Exception)
            {
                return null;
            }
        }
       /// <summary>
        /// 根据用户名+密码+权限名后缀 生成一个(唯一)的权限字符串
       /// </summary>
       /// <param name="userName"></param>
       /// <param name="pwd"></param>
       /// <param name="PermissionsName"></param>
       /// <returns></returns>
        static public string GenerationPermissions(string userName, string pwd, string PermissionsName)
        {
            try
            {
                string str = userName + pwd + PermissionsName;
                string Md5Pwd = Key.MD5Encrypt(str);
                return Key.AESEncrypt(Md5Pwd, "key");
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 验证用户是否存在该权限
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <param name="PermissionsName"></param>
        /// <returns></returns>
        static public bool checkPermissions(string userName, string pwd, string PermissionsName)
        {
            try
            {
                string PermissionsValue;
                XmlNode userInfo = userInfoToXml.checkuser(userName);
                PermissionsValue = userInfo.SelectSingleNode(PermissionsName).InnerText;
                string str = userName + pwd + PermissionsName;
                string Md5Pwd = Key.MD5Encrypt(str);
                string saveMd5Pwd = Key.AESDecrypt(PermissionsValue, "key");
                if (null != saveMd5Pwd && Md5Pwd != null && saveMd5Pwd.Equals(Md5Pwd))
                {
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据用户名得到MD5加密后的密码
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        static public string getUserPwdByName(string userName)
        {
            try
            {
                string str = null;
                XmlDocument Xmlroot = new XmlDocument();
                Xmlroot.Load(XmlPath);
                XmlNode root = Xmlroot.SelectSingleNode("root");
                XmlNodeList users = root.SelectNodes("userInfo");
                foreach (XmlNode user in users)
                {
                    if (null != user.SelectSingleNode("userName").InnerText.ToString())
                    {
                        //根据名字查找节点
                        if (user.SelectSingleNode("userName").InnerText.ToString().Equals(userName))
                        {
                            //得到节点MD%加密后密码
                            str = user.SelectSingleNode("userPwd").InnerText;
                        }
}
                }
                return str;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        /// <summary>
        /// 根据权限信息产生一个Map,Key是权限对应的节点名value权限名(用于生成与用户对应的唯一权限密钥)
        /// </summary>
        /// <param name="chk232"></param>
        /// <param name="chkAlarm"></param>
        /// <param name="chkChannel"></param>
        /// <param name="chkCommon"></param>
        /// <param name="chkDiskManage"></param>
        /// <param name="chkException"></param>
        /// <param name="chkHelp"></param>
        /// <param name="chkLocalLogQuery"></param>
        /// <param name="chkMaintenance"></param>
        /// <param name="chkManualAlarm"></param>
        /// <param name="chkNetwork"></param>
        /// <param name="chkPreview"></param>
        /// <param name="chkShutdown"></param>
        /// <param name="chkSuperAdmin"></param>
        /// <param name="chkTrain"></param>
        /// <param name="chkVideoBackup"></param>
        /// <param name="chkVideoSearch"></param>
        /// <param name="chkVideo"></param>
        /// <param name="chkcfgManage"></param>
        /// <param name="btnShutdownCctv"></param>
        /// <returns></returns>
        static private Hashtable getMap(bool chk232, bool chkAlarm, bool chkChannel, bool chkCommon,
            bool chkDiskManage, bool chkException, bool chkHelp, bool chkLocalLogQuery, bool chkMaintenance,
            bool chkManualAlarm, bool chkNetwork, bool chkPreview, bool chkShutdown, bool chkSuperAdmin,
            bool chkTrain, bool chkVideoBackup, bool chkVideoSearch, bool chkVideo, bool chkcfgManage,
            bool btnShutdownCctv)
        {
            Hashtable map = new Hashtable();
            if (chk232)
            {
                map.Add("chk232", "chk232");
            }
            if (chkAlarm)
            {
                map.Add("chkAlarm", "chkAlarm");
            }
            if (chkChannel)
            {
                map.Add("chkChannel", "chkChannel");
            }
            if (chkCommon)
            {
                map.Add("chkCommon", "chkCommon");
            }
            if (chkDiskManage)
            {
                map.Add("chkDiskManage", "chkDiskManage");
            }
            if (chkException)
            {
                map.Add("chkException", "chkException");
            }
            if (chkHelp)
            {
                map.Add("chkHelp", "chkHelp");
            }
            if (chkLocalLogQuery)
            {
                map.Add("chkLocalLogQuery", "chkLocalLogQuery");
            }
            if (chkMaintenance)
            {
                map.Add("chkMaintenance", "chkMaintenance");
            }
            if (chkManualAlarm)
            {
                map.Add("chkManualAlarm", "chkManualAlarm");
            }
            if (chkNetwork)
            {
                map.Add("chkNetwork", "chkNetwork");
            }
            if (chkPreview)
            {
                map.Add("chkPreview", "chkPreview");
            }
            if (chkShutdown)
            {
                map.Add("chkShutdown", "chkShutdown");
            }
            if (chkSuperAdmin)
            {
                map.Add("chkSuperAdmin", "chkSuperAdmin");
            }
            if (chkTrain)
            {
                map.Add("chkTrain", "chkTrain");
            }
            if (chkVideoBackup)
            {
                map.Add("chkVideoBackup", "chkVideoBackup");
            };
            if (chkVideoSearch)
            {
                map.Add("chkVideoSearch", "chkVideoSearch");
            }
            if (chkVideo)
            {
                map.Add("chkVideo", "chkVideo");
            };
            if (chkcfgManage)
            {
                map.Add("chkcfgManage", "chkcfgManage");
            }
            if (btnShutdownCctv)
            {
                map.Add("chkShutdownCctv", "chkShutdownCctv");
            }
            return map;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace STEE.SaudiMJCCTV.VideoApplication.Utility
{
    public static class Key
    {
        //默认密钥向量
        private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptString">明文</param>
        /// <param name="encryptKey">密钥</param>
        /// <returns>密文</returns>
        public static string AESEncrypt(string encryptString, string encryptKey)
        {
            encryptKey = StringHelper.GetCut(encryptKey, 32, "");
            encryptKey = encryptKey.PadRight(32, ' ');
            RijndaelManaged rijndaelProvider = new RijndaelManaged();
            rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
            rijndaelProvider.IV = Keys;
            ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
            byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
            byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
            return Convert.ToBase64String(encryptedData);
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptString">密文</param>
        /// <param name="decryptKey">密钥</param>
        /// <returns>明文</returns>
        public static string AESDecrypt(string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = StringHelper.GetCut(decryptKey, 32, "");
                decryptKey = decryptKey.PadRight(32, ' ');
                RijndaelManaged rijndaelProvider = new RijndaelManaged();
                rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
                rijndaelProvider.IV = Keys;
                ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
                byte[] inputData = Convert.FromBase64String(decryptString);
                byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
                return Encoding.UTF8.GetString(decryptedData);
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 进行MD5加密
        /// </summary>
        /// <param name="inputString">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5Encrypt(string inputString)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString();
        }
    }
}
 
                     
                    
                 
                    
                 

 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号