Ejemplo n.º 1
0
        /// <summary>
        /// Handles login.
        /// </summary>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (loginCount >= 3)
                {
                    lblLoginMessage.Content = "Logins exceeded, exit!";
                    Application.Current.Shutdown();
                }

                loginCount++;

                if (tbPassword.Password.Length > 0)
                {
                    ProgramData.ReadPwHash();

                    byte[] storedHash = ProgramData.GetPwHash();
                    byte[] salt       = ProgramData.GetPwSalt();


                    var password = new SecureString();

                    foreach (char ch in tbPassword.Password.ToCharArray())
                    {
                        password.AppendChar(ch);
                    }

                    // Securestring to string marshaling (example from: http://www.csharpdeveloping.net/Snippet/how_to_convert_securestring_to_string)
                    var    rfc2898 = new Rfc2898DeriveBytes(Marshal.PtrToStringBSTR(Marshal.SecureStringToBSTR(password)), salt, 10000);
                    byte[] pwHash  = rfc2898.GetBytes(16);

                    byte[] hashBytes = new byte[32];
                    Array.Copy(salt, 0, hashBytes, 0, 16);
                    Array.Copy(pwHash, 0, hashBytes, 16, 16);

                    if (ProgramData.ComparePwd(hashBytes))
                    {
                        lblLoginMessage.Content = "Access OK";
                        this.Close();
                    }
                    else
                    {
                        lblLoginMessage.Content = "Please enter a valid password!";
                    }
                }
                else
                {
                    lblLoginMessage.Content = "Please enter a valid password!";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message + "\n\n" + ex.StackTrace);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Stores the password data to a file.
        /// </summary>
        public void StorePwData()
        {
            try
            {
                List <string> passdata = new List <string>();

                foreach (CredentialInfo item in CredentialList)
                {
                    string siteName = item.Site;

                    // Remove unwanted characters from the names
                    if (siteName.Contains("ä"))
                    {
                        siteName = siteName.Replace('ä', 'a');
                    }
                    if (siteName.Contains("ö"))
                    {
                        siteName = siteName.Replace('ö', 'o');
                    }
                    if (siteName.Contains("å"))
                    {
                        siteName = siteName.Replace('å', 'a');
                    }

                    passdata.Add(siteName + "\t" + item.Username + "\t" + item.Password + "\n");
                }

                List <byte[]> passBytes = new List <byte[]>();

                foreach (string text in passdata)
                {
                    byte[]       byteArr;
                    UTF8Encoding enc = new UTF8Encoding();
                    byteArr = enc.GetBytes(text);
                    passBytes.Add(byteArr);
                }

                // Get salt+key
                byte[] salt = ProgramData.GetPwSalt();
                byte[] key  = ProgramData.GetPwHash();

                string cryptedFile = @".\passdata.dat";

                // Write and encrypt file.
                FileStream      fsCryptedfile = new FileStream(cryptedFile, FileMode.Create);
                RijndaelManaged RMCrypto      = new RijndaelManaged();
                CryptoStream    cryptoStream  = new CryptoStream(fsCryptedfile, RMCrypto.CreateEncryptor(key, salt), CryptoStreamMode.Write);

                foreach (byte[] data in passBytes)
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        cryptoStream.WriteByte(data[i]);
                    }
                }

                cryptoStream.Close();
                fsCryptedfile.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:\n" + e.Message, "Error");
            }
        }
Ejemplo n.º 3
0
        private void bgWorkerLoadPwData_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                int progressPct = 1;

                this.Dispatcher.BeginInvoke((Action) delegate { spProgressIndicator.Visibility = Visibility.Visible; });
                ((BackgroundWorker)sender).ReportProgress(progressPct);

                // Get salt+key
                byte[] salt = ProgramData.GetPwSalt();
                byte[] key  = ProgramData.GetPwHash();

                FileStream      fsDecrypt = new FileStream(@".\passdata.dat", FileMode.Open);
                RijndaelManaged RMDeCrypt = new RijndaelManaged();

                CryptoStream deCryptStream = new CryptoStream(fsDecrypt, RMDeCrypt.CreateDecryptor(key, salt), CryptoStreamMode.Read);

                int character;

                List <string> pwList = new List <string>();
                StringBuilder sbLine = new StringBuilder();

                // Read from decrypt stream
                while ((character = deCryptStream.ReadByte()) != -1)
                {
                    Console.Write(char.ConvertFromUtf32(character));

                    if (char.ConvertFromUtf32(character).Equals("\n"))
                    {
                        pwList.Add(sbLine.ToString());
                        sbLine.Clear();
                        ((BackgroundWorker)sender).ReportProgress((progressPct < 100) ? progressPct++ : progressPct);
                    }
                    else
                    {
                        sbLine.Append(char.ConvertFromUtf32(character));
                    }
                }


                deCryptStream.Close();

                // Update Credential list
                this.Dispatcher.BeginInvoke((Action) delegate { CredentialList.Clear(); });

                foreach (string entry in pwList)
                {
                    string[] content = entry.Split(new char[] { '\t' });
                    this.Dispatcher.BeginInvoke((Action) delegate
                    {
                        CredentialList.Add(new CredentialInfo {
                            Site = content[0], Username = content[1], Password = content[2]
                        });
                    });
                    ((BackgroundWorker)sender).ReportProgress((progressPct < 100) ? progressPct++ : progressPct);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:\n" + ex.Message, "Error");
            }
            finally
            {
                this.Dispatcher.BeginInvoke((Action) delegate { spProgressIndicator.Visibility = Visibility.Collapsed; });
            }
        }