Beispiel #1
0
        /// <summary>
        /// 网络登录
        /// </summary>
        /// <returns></returns>
        public bool WsSignIn(string home, string code, string name, string pass, XmlReader reader)
        {
            string data = null;
            if (reader.Name == "Data" || reader.ReadToFollowing("Data"))
            {
                data = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(data, "^[A-Za-z0-9+/=]{256,}$"))
            {
                return false;
            }

            string info = null;
            if (reader.Name == "Info" || reader.ReadToFollowing("Info"))
            {
                info = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(info, "^[A-Za-z0-9+/=]{44}$"))
            {
                return false;
            }

            string main = null;
            if (reader.Name == "Main" || reader.ReadToFollowing("Main"))
            {
                main = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(main, "^[A-Za-z0-9+/=]{108}$"))
            {
                return false;
            }

            string safe = null;
            if (reader.Name == "Safe" || reader.ReadToFollowing("Safe"))
            {
                safe = reader.ReadElementContentAsString();
            }

            _Data = Convert.FromBase64String(data);
            if (info != Digest(name, pass))
            {
                return false;
            }

            if (!Decrypt(name, code, pass, main))
            {
                return false;
            }

            this.DatHome = home;
            DFEngine prop = new DFEngine();
            prop.Set(CApp.AMON_CFG_NAME, name);
            prop.Set(CApp.AMON_CFG_CODE, code);
            prop.Set(CApp.AMON_CFG_DATA, data);
            prop.Set(CApp.AMON_CFG_INFO, info);
            prop.Set(CApp.AMON_CFG_LOCK, info);
            prop.Set(CApp.AMON_CFG_MAIN, main);
            prop.Set(CApp.AMON_CFG_SAFE, safe);
            prop.Save(Path.Combine(this.DatHome, CApp.AMON_CFG));

            this.Name = name;
            this.Code = code;
            _Info = info;
            _Lock = info;
            Look = "Default";
            Feel = "Default";
            return true;
        }
Beispiel #2
0
        public bool WsSignPk(string oldPass, string newPass, XmlReader reader)
        {
            string code = null;
            if (reader.Name == "Code" || reader.ReadToFollowing("Code"))
            {
                code = reader.ReadElementContentAsString();
            }
            if (!CharUtil.IsValidateCode(code))
            {
                return false;
            }

            string data = null;
            if (reader.Name == "Data" || reader.ReadToFollowing("Data"))
            {
                data = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(data, "^[A-Za-z0-9+/=]{256,}$"))
            {
                return false;
            }

            string info = null;
            if (reader.Name == "Info" || reader.ReadToFollowing("Info"))
            {
                info = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(info, "^[A-Za-z0-9+/=]{44}$"))
            {
                return false;
            }

            string main = null;
            if (reader.Name == "Main" || reader.ReadToFollowing("Main"))
            {
                main = reader.ReadElementContentAsString();
            }
            if (!Regex.IsMatch(main, "^[A-Za-z0-9+/=]{108}$"))
            {
                return false;
            }

            string safe = null;
            if (reader.Name == "Safe" || reader.ReadToFollowing("Safe"))
            {
                safe = reader.ReadElementContentAsString();
            }

            _Data = Convert.FromBase64String(data);
            if (info != Digest(oldPass, newPass))
            {
                return false;
            }

            if (!Decrypt(oldPass, code, newPass, main))
            {
                return false;
            }

            DFEngine prop = new DFEngine();
            prop.Load(Path.Combine(this.DatHome, CApp.AMON_CFG));
            prop.Set(CApp.AMON_CFG_NAME, oldPass);
            prop.Set(CApp.AMON_CFG_CODE, code);
            prop.Set(CApp.AMON_CFG_DATA, data);
            prop.Set(CApp.AMON_CFG_INFO, info);
            prop.Set(CApp.AMON_CFG_LOCK, _Lock);
            prop.Set(CApp.AMON_CFG_MAIN, main);
            prop.Set(CApp.AMON_CFG_SAFE, safe);
            prop.Save(Path.Combine(this.DatHome, CApp.AMON_CFG));

            return true;
        }
Beispiel #3
0
        /// <summary>
        /// 设定安全口令
        /// </summary>
        /// <param name="oldPass"></param>
        /// <param name="secPass"></param>
        /// <returns></returns>
        public bool CaAuthSk(string oldPass, string secPass)
        {
            DFEngine prop = new DFEngine();
            prop.Load(Path.Combine(this.DatHome, CApp.AMON_CFG));

            // 已有口令校验
            string info = Digest(Name, oldPass);
            if (info != prop.Get(CApp.AMON_CFG_INFO))
            {
                return false;
            }

            // 生成加密密钥及字符空间
            byte[] t = new byte[72];
            byte[] a = Encoding.UTF8.GetBytes(Code);
            int i = 0;
            Array.Copy(a, 0, t, i, a.Length);
            i += a.Length;
            Array.Copy(_Salt, 0, t, i, _Salt.Length);
            i += _Salt.Length;
            Array.Copy(_Keys, 0, t, i, _Keys.Length);
            i += _Keys.Length;
            a = Encoding.UTF8.GetBytes(_Mask);
            Array.Copy(a, 0, t, i, a.Length);

            // 口令
            byte[] k = GenK(Name, Code, secPass);
            // 向量
            byte[] v = GenV(Name, Code, secPass);

            #region AES 加密
            AesManaged aes = new AesManaged();
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(k, v), CryptoStreamMode.Write))
                {
                    cStream.Write(t, 0, t.Length);
                    cStream.FlushFinalBlock();
                    t = mStream.ToArray();
                }
            }
            aes.Clear();
            #endregion

            prop.Set(CApp.AMON_CFG_SAFE, Convert.ToBase64String(t));
            prop.Save(Path.Combine(this.DatHome, CApp.AMON_CFG));
            return true;
        }
Beispiel #4
0
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public bool CaSignUp(string root, string code, string name, string pass)
        {
            byte[] k = GenK(name, code, pass);
            byte[] v = GenV(name, code, pass);

            Random r = new Random();
            _Salt = new byte[16];
            r.NextBytes(_Salt);
            _Keys = new byte[32];
            r.NextBytes(_Keys);
            _Mask = GenChar();

            byte[] t = new byte[72];
            byte[] a = Encoding.UTF8.GetBytes(code);
            int i = 0;
            Array.Copy(a, 0, t, i, a.Length);
            i += a.Length;
            Array.Copy(_Salt, 0, t, i, _Salt.Length);
            i += _Salt.Length;
            Array.Copy(_Keys, 0, t, i, _Keys.Length);
            i += _Keys.Length;
            a = Encoding.UTF8.GetBytes(_Mask);
            Array.Copy(a, 0, t, i, a.Length);

            #region AES 加密
            AesManaged aes = new AesManaged();
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(k, v), CryptoStreamMode.Write))
                {
                    cStream.Write(t, 0, t.Length);
                    cStream.FlushFinalBlock();
                    t = mStream.ToArray();
                }
            }
            aes.Clear();
            #endregion
            string main = Convert.ToBase64String(t);

            _Data = new byte[256];
            r.NextBytes(_Data);
            string info = Digest(name, pass);

            this.DatHome = Path.Combine(root, code);
            if (!Directory.Exists(this.DatHome))
            {
                Directory.CreateDirectory(this.DatHome);
            }
            DFEngine prop = new DFEngine();
            prop.Set(CApp.AMON_CFG_NAME, name);
            prop.Set(CApp.AMON_CFG_CODE, code);
            prop.Set(CApp.AMON_CFG_DATA, Convert.ToBase64String(_Data));
            prop.Set(CApp.AMON_CFG_INFO, info);
            prop.Set(CApp.AMON_CFG_LOCK, info);
            prop.Set(CApp.AMON_CFG_MAIN, main);
            prop.Set(CApp.AMON_CFG_SAFE, "");
            prop.Save(Path.Combine(this.DatHome, CApp.AMON_CFG));

            this.Name = name;
            this.Code = code;
            _Info = info;
            _Lock = info;
            Look = "Default";
            Feel = "Default";
            return true;
        }
Beispiel #5
0
        /// <summary>
        /// 设定锁屏口令
        /// </summary>
        /// <returns></returns>
        public bool CaAuthLk(string oldPass, string newPass)
        {
            if (_Lock != Digest(this.Name, oldPass))
            {
                return false;
            }

            _Lock = Digest(Name, newPass);

            DFEngine prop = new DFEngine();
            prop.Load(Path.Combine(this.DatHome, CApp.AMON_CFG));
            prop.Set(CApp.AMON_CFG_LOCK, _Lock);
            prop.Save(Path.Combine(this.DatHome, CApp.AMON_CFG));
            return true;
        }
Beispiel #6
0
        /// <summary>
        /// 打开(&O)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdOpen = Main.OpenFileDialog;
            fdOpen.Filter = "阿木密码箱配置文件|*.cfg";
            fdOpen.FileName = CApp.AMON_CFG;
            fdOpen.InitialDirectory = _UserModel.SysHome;
            fdOpen.Multiselect = false;
            if (DialogResult.OK != fdOpen.ShowDialog(_Main))
            {
                return;
            }

            string path = fdOpen.FileName;
            if (!File.Exists(path))
            {
                Main.ShowAlert("无法访问您选择的文件!");
                return;
            }

            string file = (Path.GetFileName(path) ?? "").ToLower();
            if (CApp.AMON_CFG.ToLower() != file)
            {
                Main.ShowAlert(string.Format("请确认您选择的文件名是否为:{0}!", CApp.AMON_CFG));
                return;
            }

            DFEngine prop = new DFEngine();
            prop.Load(path);
            string name = prop.Get(CApp.AMON_CFG_NAME);
            string code = prop.Get(CApp.AMON_CFG_CODE);
            if (!CharUtil.IsValidateCode(code) || !CharUtil.IsValidate(name))
            {
                Main.ShowAlert("请确认您选择的数据路径是否正确!");
                return;
            }

            prop.Clear();
            string sysFile = Path.Combine(_UserModel.SysHome, CApp.AMON_SYS);
            prop.Load(sysFile);
            prop.Set(string.Format(CApp.AMON_SYS_CODE, name), code);
            path = Path.GetDirectoryName(path);
            if (path.StartsWith(Application.StartupPath))
            {
                path = path.Substring(Application.StartupPath.Length + 1);
            }
            prop.Set(string.Format(CApp.AMON_SYS_HOME, name), path);
            prop.Save(sysFile);
        }
Beispiel #7
0
        public void DoSignAc()
        {
            #region 用户判断
            string name = TbName.Text;
            if (string.IsNullOrEmpty(name))
            {
                Main.ShowAlert("请输入【登录用户】!");
                TbName.Focus();
                return;
            }
            #endregion

            #region 登录口令
            string pass = TbPass.Text;
            if (string.IsNullOrEmpty(pass))
            {
                Main.ShowAlert("请输入【登录口令】!");
                TbPass.Focus();
                return;
            }
            if (pass.Length < 4)
            {
                Main.ShowAlert("【登录口令】不能少于 4 个字符!");
                TbPass.Focus();
                return;
            }
            #endregion

            #region 路径判断
            string path = TbPath.Text;
            if (string.IsNullOrEmpty(path))
            {
                path = CApp.DIR_DATA;
            }
            if (!Directory.Exists(path))
            {
                Main.ShowAlert("无法访问路径!");
                BtPath.Focus();
                return;
            }
            #endregion

            #region 认证信息
            string text = TbText.Text;
            if (string.IsNullOrEmpty(text))
            {
                Main.ShowAlert("请输入认证数据!");
                TbPath.Focus();
                return;
            }
            #endregion

            try
            {
                using (XmlReader reader = XmlReader.Create(new StringReader(text)))
                {
                    if (text.IndexOf("<Error>") > 0)
                    {
                        _SignAc.HideWaiting();
                        reader.ReadToFollowing("Error");
                        Main.ShowAlert(reader.ReadElementContentAsString());
                        return;
                    }

                    string code = null;
                    if (reader.Name == "Code" || reader.ReadToFollowing("Code"))
                    {
                        code = reader.ReadElementContentAsString();
                    }
                    if (!CharUtil.IsValidateCode(code))
                    {
                        _SignAc.HideWaiting();
                        Main.ShowAlert("注册用户失败,请稍后重试!");
                        return;
                    }
                    path = Path.Combine(path, code);
                    if (Directory.Exists(path))
                    {
                        _SignAc.HideWaiting();
                        Main.ShowAlert(string.Format("指定路径下已存在名为 {0} 的目录!", code));
                        return;
                    }
                    Directory.CreateDirectory(path);

                    if (!_UserModel.WsSignIn(path, code, name, pass, reader))
                    {
                        Main.ShowAlert("请确认您输入的令牌数据是否有效!");
                        TbPath.Focus();
                        return;
                    }
                }

                string sysFile = Path.Combine(_UserModel.SysHome, CApp.AMON_SYS);
                DFEngine prop = new DFEngine();
                prop.Load(sysFile);
                prop.Set(string.Format(CApp.AMON_SYS_CODE, name), _UserModel.Code);
                path = _UserModel.DatHome;
                if (path.StartsWith(Application.StartupPath))
                {
                    path = path.Substring(Application.StartupPath.Length + 1);
                }
                prop.Set(string.Format(CApp.AMON_SYS_HOME, name), _UserModel.DatHome);
                prop.Save(sysFile);

                InitDat();
            }
            catch (Exception exp)
            {
                _SignAc.HideWaiting();
                Main.ShowAlert(exp.Message);
                TbPath.Focus();
                return;
            }
        }
Beispiel #8
0
        private void SignIn_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                _SignAc.HideWaiting();
                Main.ShowAlert(e.Error.Message);
                return;
            }

            string xml = e.Result;

            using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
            {
                if (xml.IndexOf("<Error>") > 0)
                {
                    _SignAc.HideWaiting();
                    reader.ReadToFollowing("Error");
                    Main.ShowAlert(reader.ReadElementContentAsString());
                    return;
                }

                string code = null;
                if (reader.Name == "Code" || reader.ReadToFollowing("Code"))
                {
                    code = reader.ReadElementContentAsString();
                }
                if (!CharUtil.IsValidateCode(code))
                {
                    _SignAc.HideWaiting();
                    Main.ShowAlert("注册用户失败,请稍后重试!");
                    return;
                }
                _Home = Path.Combine(_Home, code);
                if (!Directory.Exists(_Home))
                {
                    Directory.CreateDirectory(_Home);
                }

                if (!_UserModel.WsSignIn(_Home, code, _Name, _Pass, reader))
                {
                    _SignAc.HideWaiting();
                    Main.ShowAlert("请确认您输入的登录用户及登录口令是否正确!");
                    TbName.Focus();
                }
                else
                {
                    string sysFile = Path.Combine(_UserModel.SysHome, CApp.AMON_SYS);
                    DFEngine prop = new DFEngine();
                    prop.Load(sysFile);
                    prop.Set(string.Format(CApp.AMON_SYS_HOME, _Name), _Home);
                    prop.Set(string.Format(CApp.AMON_SYS_CODE, _Name), code);
                    prop.Save(sysFile);

                    InitDat();
                }
            }

            _Pass = null;
        }
Beispiel #9
0
        public void DoSignAc()
        {
            #region 用户判断
            string name = TbName.Text;
            if (string.IsNullOrEmpty(name))
            {
                Main.ShowAlert("请输入用户名!");
                TbName.Focus();
                return;
            }

            if (name.Length < 5)
            {
                Main.ShowAlert("用户名不能少于5个字符!");
                TbName.Focus();
                return;
            }

            if (!Regex.IsMatch(name, "^\\w{5,}$"))
            {
                Main.ShowAlert("用户名中含有非法字符!");
                TbName.Focus();
                return;
            }

            name = name.ToLower();
            string sysFile = Path.Combine(_UserModel.SysHome, CApp.AMON_SYS);
            _Prop = new DFEngine();
            _Prop.Load(sysFile);
            string home = _Prop.Get(string.Format(CApp.AMON_SYS_HOME, name));
            if (!string.IsNullOrEmpty(home))
            {
                Main.ShowAlert(string.Format("已存在名为 {0} 的用户,请尝试其它用户名!", name));
                return;
            }
            #endregion

            #region 口令判断
            string pass = TbPass1.Text;
            TbPass1.Text = "";
            if (string.IsNullOrEmpty(pass))
            {
                Main.ShowAlert("请输入登录口令!");
                TbPass1.Focus();
                return;
            }

            if (pass.Length < 4)
            {
                Main.ShowAlert("登录口令不能少于4个字符!");
                TbPass1.Text = "";
                TbPass2.Text = "";
                TbPass1.Focus();
                return;
            }

            if (pass != TbPass2.Text)
            {
                TbPass2.Text = "";
                Main.ShowAlert("您两次输入的口令不一致!");
                TbPass1.Focus();
                return;
            }
            TbPass2.Text = "";
            #endregion

            #region 路径判断
            home = TbPath.Text;
            if (string.IsNullOrEmpty(home))
            {
                Main.ShowAlert("请选择您的数据存放目录!");
                BtPath.Focus();
                return;
            }
            #endregion

            #region 代码
            string code = CApp.USER_AMON;
            if (Directory.Exists(Path.Combine(home, code)))
            {
                Main.ShowAlert(string.Format("指定路径下已存在名为 {0} 的目录!", code));
                return;
            }
            #endregion

            _SignAc.ShowWaiting();

            try
            {
                // 本地注册
                if (!_UserModel.CaSignUp(home, code, name, pass))
                {
                    pass = null;
                    _SignAc.HideWaiting();
                    Main.ShowAlert("系统异常,请稍后重试!");
                    return;
                }

                _Prop.Set(string.Format(CApp.AMON_SYS_CODE, name), _UserModel.Code);
                home = _UserModel.DatHome;
                if (home.StartsWith(Application.StartupPath))
                {
                    home = home.Substring(Application.StartupPath.Length + 1);
                }
                _Prop.Set(string.Format(CApp.AMON_SYS_HOME, name), home);
                _Prop.Save(sysFile);

                InitDat();
            }
            catch (Exception exp)
            {
                _SignAc.HideWaiting();
                Main.ShowAlert(exp.Message);
                return;
            }
        }