Пример #1
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();
                    }
                }
            }
        }
Пример #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 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();
                    }
                }
            }
        }