Exemplo n.º 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();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Save the data.
        /// </summary>
        /// <param name="e">The closing state.</param>
        /// <param name="closing">Is the application closing.</param>
        private void SaveData(FormClosingEventArgs e, bool closing = true)
        {
            // Get the current password.
            _currentPassword = txtPassword.Text.Trim();

            // If no file password has been supplied.
            if (String.IsNullOrEmpty(_currentPassword) && (_loggedIn))
            {
                // Cancel the close operation.
                if (closing)
                {
                    e.Cancel = true;
                }

                MessageBox.Show("A valid file password must be entered.");
            }
            else if (_loggedIn)
            {
                FileStream file = null;

                try
                {
                    // If data exists
                    if (richTextBoxData.Text.Length > 0)
                    {
                        // Create a new file stream
                        // truncate the file.
                        file = new FileStream(_currentFile, FileMode.Truncate,
                                              FileAccess.Write, FileShare.ReadWrite);

                        // Get the data in the data test.
                        string decryptedData      = richTextBoxData.Rtf;
                        byte[] decryptedDataBytes = Encoding.Default.GetBytes(decryptedData);

                        // Create a new cryptography instance.
                        using (AdvancedAES cryto = new AdvancedAES())
                        {
                            // Encrypt the data.
                            Byte[] encryptedData = cryto.EncryptToMemory(decryptedDataBytes, _currentPassword);

                            // if using the key.
                            if (!String.IsNullOrEmpty(_cryptoKey))
                            {
                                byte[] encrytedInt = new byte[0].Combine(encryptedData);
                                encryptedData = cryto.EncryptToMemory(encrytedInt, _cryptoKey);
                            }

                            // Write the data to the file.
                            file.Write(encryptedData, 0, encryptedData.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                    if (closing)
                    {
                        e.Cancel = true;
                    }
                }
                finally
                {
                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <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();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Cryto execute.
        /// </summary>
        /// <param name="decryptFile">The decrypt file.</param>
        /// <param name="encryptFile">The encrypt file.</param>
        /// <param name="password">The password file.</param>
        private async void CryptExecute(string decryptFile, string encryptFile, string password = "")
        {
            // Set the execute stop button.
            ExecuteStopCrypt();

            // Start a new task.
            await Nequeo.Threading.AsyncOperationResult <bool> .RunTask(() =>
            {
                string decryptFileInt = "";
                string encryptFileInt = "";
                bool ret = false;

                try
                {
                    // Create a new AES cryto.
                    using (AdvancedAES aes = new AdvancedAES())
                    {
                        decryptFileInt = decryptFile.Trim() + ".partial";
                        encryptFileInt = encryptFile.Trim() + ".partial";

                        // Get the cryptography operation.
                        switch (_cryto)
                        {
                        case CryptographyType.Encrypt:
                            // Cryto with the password.
                            if (String.IsNullOrEmpty(password))
                            {
                                ret = aes.EncryptFile(decryptFile, encryptFileInt);
                            }
                            else
                            {
                                ret = aes.EncryptFile(decryptFile, encryptFileInt, password);
                            }

                            // Encrypt with key.
                            if (ret)
                            {
                                ret = aes.EncryptFile(encryptFileInt.Trim(), encryptFile.Trim(), _cryptoKey);
                            }

                            // Encryption complete.
                            SetEncrypting(ret);
                            break;

                        case CryptographyType.Decrypt:
                            // Decrypt with key.
                            ret = aes.DecryptFile(decryptFileInt.Trim(), encryptFile.Trim(), _cryptoKey);

                            // Cryto with the password.
                            if (ret)
                            {
                                if (String.IsNullOrEmpty(password))
                                {
                                    ret = aes.DecryptFile(decryptFile, decryptFileInt);
                                }
                                else
                                {
                                    ret = aes.DecryptFile(decryptFile, decryptFileInt, password);
                                }
                            }

                            // Decryption complete.
                            SetDecrypting(ret);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Set the execution stop controls.
                    ExecuteStopCrypt();

                    // On error trigger the
                    // error event handler.
                    if (OnError != null)
                    {
                        OnError(this, new ClientCommandArgs("ERROR", ex.Message, 000));
                    }
                }
                finally
                {
                    try
                    {
                        // Delete the encrypted partial file.
                        if (System.IO.File.Exists(encryptFileInt))
                        {
                            System.IO.File.Delete(encryptFileInt);
                        }
                    }
                    catch { }

                    try
                    {
                        // Delete the decrypted partial file.
                        if (System.IO.File.Exists(decryptFileInt))
                        {
                            System.IO.File.Delete(decryptFileInt);
                        }
                    }
                    catch { }
                }
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Save the data.
        /// </summary>
        /// <param name="e">The closing state.</param>
        /// <param name="closing">Is the application closing.</param>
        private void SaveData(FormClosingEventArgs e, bool closing = true)
        {
            // Get the current user.
            _currentPassword = txtPasswordFile.Text.Trim();
            _currentFile     = txtFile.Text.Trim();

            // If no file password has been supplied.
            if ((String.IsNullOrEmpty(_currentPassword) || String.IsNullOrEmpty(_currentFile)) && (_loggedIn))
            {
                // Cancel the close operation.
                if (closing)
                {
                    e.Cancel = true;
                }

                MessageBox.Show("A valid file password must be entered.");
            }
            else if (_loggedIn)
            {
                FileStream file = null;

                try
                {
                    // If data exists
                    if (_manager != null)
                    {
                        // Assign all the current items.
                        _manager.item = _items.ToArray();

                        // Create a new file stream
                        // truncate the file.
                        file = new FileStream(_currentFile, FileMode.Truncate,
                                              FileAccess.Write, FileShare.ReadWrite);

                        // Create a new cryptography instance.
                        using (AdvancedAES cryto = new AdvancedAES())
                        {
                            // Deserialise the xml file.
                            GeneralSerialisation serial = new GeneralSerialisation();
                            byte[] decryptedData        = serial.Serialise(_manager, typeof(Nequeo.Forms.UI.Security.Data.passwordManager));

                            // Encrypt the data.
                            Byte[] encryptedData = cryto.EncryptToMemory(decryptedData, _currentPassword);

                            // if using the key.
                            if (!String.IsNullOrEmpty(_cryptoKey))
                            {
                                byte[] encrytedInt = new byte[0].Combine(encryptedData);
                                encryptedData = cryto.EncryptToMemory(encrytedInt, _cryptoKey);
                            }

                            // Write the data to the file.
                            file.Write(encryptedData, 0, encryptedData.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                    if (closing)
                    {
                        e.Cancel = true;
                    }
                }
                finally
                {
                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
        }