예제 #1
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");
            }
        }
예제 #2
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; });
            }
        }