Beispiel #1
0
        /// <summary>
        /// 輸出加密文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExportEncryptFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter = "*.lic|";
            dialog.Title  = "Save Encrypted File";

            //預設檔案名稱和副檔名
            dialog.FileName   = "License";
            dialog.DefaultExt = "lic";

            //加密公鑰路徑
            string publicKey = $"{ this.textBoxEncryptKeyPath.Text}";

            var EncryptTxet = RSAKit.Encrypt(this.textBoxDecryptContent.Text, publicKey);

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(dialog.FileName))
                {
                    sw.WriteLineAsync(EncryptTxet);
                }

                //複製私鑰放到和License一起
                var CopyPrivateKey = Path.Combine(Path.GetDirectoryName(dialog.FileName), RSAKit.privateKeyFileName);
                File.Copy(this.textBoxEncryptKeyPath.Text, CopyPrivateKey, true);
            }
            MessageBox.Show("儲存完畢", "提示");
        }
Beispiel #2
0
        /// <summary>
        /// 預設公鑰及私鑰的位置
        /// </summary>
        private void SetDefaultKey()
        {
            this.textBoxEncryptKeyPath.Text = "RSA.Pub";
            this.textBoxDecryptKeyPath.Text = "RSA.Private";

            if (!File.Exists("RSA.Pub") || !File.Exists("RSA.Private"))
            {
                RSAKit.GenerateKeys();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 檔案放下時
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private new void DragDrop(object sender, DragEventArgs e)
        {
            string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);

            //解密私鑰路徑
            string privateKey = $"{ this.textBoxDecryptKeyPath.Text}";


            var DecryptTxet = RSAKit.Decrypt(File.ReadAllText(filePaths[0]), privateKey);

            this.textBoxDecryptContent.Text = DecryptTxet;
        }
        public IActionResult UpdatePassWord(LoginModel login)
        {
            login.SuccessCode = 200;
            RSAKit rSAKit = new RSAKit();
            Users  user   = _userRepository.GetUser(new Users
            {
                UniqueId = User.FindFirstValue(ClaimTypes.NameIdentifier)
            });

            if (user == null || string.IsNullOrEmpty(user.Name_Cn))
            {
                login.SuccessCode = 500;
                login.Msg         = "登录状态失效,请退出重新登录!";
            }
            if (login.SuccessCode == 200)
            {
                // Dick 2021-04-30 [ 验证历史密码是否正确 ]
                user.Password = rSAKit.RSADecrypt(user.PrivateKey, user.Password);
                if (!user.Password.Equals(login.OldPassWord))
                {
                    login.SuccessCode = 500;
                    login.Msg         = "密码错误,操作失败!";
                }
            }
            if (login.SuccessCode == 200)
            {
                // Dick 2021-04-30 [ 验证两次密码输入是否一致 ]
                if (!login.PassWord.Equals(login.VerifyPassword))
                {
                    login.SuccessCode = 500;
                    login.Msg         = "两次密码输入不一致,操作失败!";
                }
            }
            if (login.SuccessCode == 200)
            {
                // Dick 2021-04-30 [ 加密密码,Save ]
                string xmlPrivateKey, xmlPublicKey;
                rSAKit.RSAKey(out xmlPrivateKey, out xmlPublicKey);
                string pwd = rSAKit.RSAEncrypt(xmlPublicKey, login.PassWord);
                user.Password   = pwd;
                user.PrivateKey = xmlPrivateKey;
                user.UpdateTime = DateTime.Now;
                user.UpdateName = User.Identity.Name;
                _userRepository.UpdatePassWord(user);
            }
            ViewBag.HttpContext = HttpContext;
            return(View(login));
        }
        /// <summary>
        /// 颁发证书
        /// </summary>
        /// <remarks>
        /// <para/>Author   :  Dick
        /// <para/>Date     :  2021-04-28
        /// </remarks>
        /// <returns></returns>
        public async Task <IActionResult> Authenticate(LoginModel login)
        {
            Users user = _userRepository.GetUser(new Users
            {
                Name_Cn = login.UserName,
                Name_En = login.UserName,
                //Password = login.PassWord
            });

            if (user == null)
            {
                return(Redirect("/Login/Index?statuCode=404"));
            }
            if (string.IsNullOrEmpty(user.Password) ||
                string.IsNullOrEmpty(user.PrivateKey))
            {
                return(Redirect("/Login/Index?statuCode=500"));
            }
            string pwd = new RSAKit().RSADecrypt(user.PrivateKey, user.Password);

            if (!pwd.Equals(login.PassWord))
            {
                return(Redirect("/Login/Index?statuCode=500"));
            }

            // Dick 2021-04-29 [ 生成个人信息 ]
            var userClaims = new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, user.UniqueId),
                new Claim(ClaimTypes.Name, user.Name_Cn)
            };
            // Dick 2021-04-29 [  ClaimsIdentity,产生了一张证书 ]
            var userIdentity = new ClaimsIdentity(userClaims, "User Identity");
            // Dick 2021-04-29 [ ClaimsPrincipal,管理证书 ]
            var userPrincipa = new ClaimsPrincipal(userIdentity);
            // Dick 2021-04-29 [ 产生证书并且输入到前台Cookie,CookieAuth ]
            await HttpContext.SignInAsync("CookieAuth", userPrincipa,
                                          new AuthenticationProperties
            {
                ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(30), // 半小时有效期
                IsPersistent = true                                  // 持久化
            });

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #6
0
        public IActionResult Save(Users user)
        {
            user.UpdateTime = DateTime.Now;
            user.UpdateName = UserName;
            if (string.IsNullOrEmpty(user.UniqueId))
            {
                string xmlPrivateKey, xmlPublicKey;
                RSAKit rSAKit = new RSAKit();
                rSAKit.RSAKey(out xmlPrivateKey, out xmlPublicKey);
                string pwd = rSAKit.RSAEncrypt(xmlPublicKey, user.Password);
                user.Password   = pwd;
                user.PrivateKey = xmlPrivateKey;
                user.CreateTime = DateTime.Now;
                user.CreateName = UserName;
            }
            bool isSave = _userRepository.Save(user);

            if (isSave)
            {
                user.SuccessCode = 200;
            }
            return(View(user));
        }
 public void Test_Init()
 {
     kit = new RSAKit();
 }