Exemple #1
0
        /// <summary>
        /// Load the encrypted data.
        /// </summary>
        private void LoadData()
        {
            // Get the current user.
            _currentPassword = txtPassword.Text.Trim();
            _currentFile     = txtUsername.Text.Trim().ToLower() + ".txt";

            FileStream file = null;

            try
            {
                // if the file dose not
                // exist then create it.
                if (!File.Exists(_currentFile))
                {
                    file = File.Create(_currentFile);
                    file.Close();

                    // Enable the data container.
                    richTextBoxData.Enabled   = true;
                    toolStripRichText.Enabled = true;
                    btnLogin.Text             = "Re-Load";

                    // User has logged in.
                    _loggedIn         = true;
                    btnCancel.Enabled = false;
                }
                else
                {
                    // Open the file.
                    file = new FileStream(_currentFile, FileMode.Open,
                                          FileAccess.Read, FileShare.ReadWrite);

                    // If the file contains data.
                    if (file.Length > 0)
                    {
                        string decryptedData = "";

                        // Create a new cryptography instance.
                        using (AdvancedAES cryto = new AdvancedAES())
                        {
                            // Read the file data into
                            // the byte array.
                            Byte[] array = new byte[file.Length];
                            file.Read(array, 0, array.Length);

                            // if using the key.
                            if (!String.IsNullOrEmpty(_cryptoKey))
                            {
                                // Decrypt with key.
                                byte[] decryptedInt = cryto.DecryptFromMemory(array, _cryptoKey);
                                array = new byte[0].Combine(decryptedInt);
                            }

                            // Decrypt the data.
                            byte[] decryptedDataBytes = cryto.DecryptFromMemory(array, _currentPassword);
                            decryptedData = Encoding.Default.GetString(decryptedDataBytes);
                        }

                        // Was the data decrypted.
                        if (String.IsNullOrEmpty(decryptedData))
                        {
                            MessageBox.Show("Incorrect file password.");
                        }
                        else
                        {
                            // Enable the data container.
                            richTextBoxData.Enabled   = true;
                            toolStripRichText.Enabled = true;
                            btnLogin.Text             = "Re-Load";

                            // User has logged in.
                            _loggedIn         = true;
                            btnCancel.Enabled = false;

                            // Load the data into the rich text.
                            richTextBoxData.Rtf = decryptedData;
                        }
                    }
                    else
                    {
                        // Enable the data container.
                        richTextBoxData.Enabled   = true;
                        toolStripRichText.Enabled = true;
                        btnLogin.Text             = "Re-Load";

                        // User has logged in.
                        _loggedIn         = true;
                        btnCancel.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
        }
        /// <summary>
        /// Load the encrypted data.
        /// </summary>
        private void LoadData()
        {
            // Disable the group box.
            groupBoxItem.Enabled = false;
            _items.Clear();
            _manager = null;

            // Get the current user.
            _currentPassword = txtPasswordFile.Text.Trim();
            _currentFile     = txtFile.Text.Trim();

            FileStream file = null;

            try
            {
                // Remove all current items in the list.
                if (listBoxPasswordName.Items.Count > 0)
                {
                    listBoxPasswordName.Items.Clear();
                }

                // Open the file.
                file = new FileStream(_currentFile, FileMode.Open,
                                      FileAccess.Read, FileShare.ReadWrite);

                // If the file contains data.
                if (file.Length > 0)
                {
                    byte[] decryptedData = null;

                    // Create a new cryptography instance.
                    using (AdvancedAES cryto = new AdvancedAES())
                    {
                        // Read the file data into
                        // the byte array.
                        Byte[] array = new byte[file.Length];
                        file.Read(array, 0, array.Length);

                        // if using the key.
                        if (!String.IsNullOrEmpty(_cryptoKey))
                        {
                            // Decrypt with key.
                            byte[] decryptedInt = cryto.DecryptFromMemory(array, _cryptoKey);
                            array = new byte[0].Combine(decryptedInt);
                        }

                        // Decrypt the data.
                        decryptedData = cryto.DecryptFromMemory(array, _currentPassword);
                    }

                    // Was the data decrypted.
                    if (decryptedData == null)
                    {
                        MessageBox.Show("Incorrect file password.");
                    }
                    else
                    {
                        // Load the xml data.
                        // Deserialise the xml file.
                        GeneralSerialisation serial = new GeneralSerialisation();
                        _manager = ((Nequeo.Forms.UI.Security.Data.passwordManager)serial.Deserialise(typeof(Nequeo.Forms.UI.Security.Data.passwordManager), decryptedData));

                        // Create an empty manager.
                        if (_manager == null)
                        {
                            _manager = new Data.passwordManager();
                        }

                        // If items exist.
                        if (_manager != null && _manager.item != null)
                        {
                            // Add the items to the list.
                            _items.AddRange(_manager.item);

                            // For each item found.
                            foreach (Nequeo.Forms.UI.Security.Data.passwordManagerItem item in _manager.item)
                            {
                                // Load the names into the list box.
                                listBoxPasswordName.Items.Add(item.name);
                            }

                            // If no items exist.
                            if (listBoxPasswordName.Items.Count > 0)
                            {
                                btnRemoveItem.Enabled = true;
                            }
                        }

                        // Enable the data container.
                        listBoxPasswordName.Enabled = true;
                        btnAddItem.Enabled          = true;

                        // User has logged in.
                        _loggedIn            = true;
                        btnFile.Enabled      = false;
                        btnAuthenticate.Text = "Re-Load";
                    }
                }
                else
                {
                    // Create an empty manager.
                    _manager = new Data.passwordManager();

                    // Enable the data container.
                    listBoxPasswordName.Enabled = true;
                    btnAddItem.Enabled          = true;

                    // User has logged in.
                    _loggedIn            = true;
                    btnFile.Enabled      = false;
                    btnAuthenticate.Text = "Re-Load";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
        }