Exemple #1
0
        /// <summary>
        /// Saves the file to the given path.
        /// </summary>
        private bool Save(string path)
        {
            PasswordDialog dialog = new PasswordDialog();

            dialog.Owner            = this;
            dialog.NeedsVerifcation = true;

            if (dialog.ShowDialog() == true)
            {
                string text      = textEditor.Text;
                byte[] textBytes = dialog.TextEncoding.GetBytes(text);

                SecureString password = dialog.Password;
                byte[]       key      = new byte[dialog.EncryptionConfig.KeySize / 8];
                using (SecureStringBytes pwBytes = new SecureStringBytes(password, Encoding.UTF8))
                {
                    password.Dispose();
                    dialog.EncryptionConfig.KeyAlgo.GenerateKey(pwBytes.Bytes, key);
                }

                try
                {
                    SymmetricAlgorithm algo = dialog.EncryptionConfig.Algo.GetImplementation();
                    algo.KeySize = dialog.EncryptionConfig.KeySize;
                    algo.Key     = key;

                    ICryptoTransform encryptor = algo.CreateEncryptor();

                    using (FileStream outStream = new FileStream(path, FileMode.Create))
                    {
                        outStream.Write(algo.IV, 0, algo.IV.Length);
                        using (CryptoStream cryptoStream = new CryptoStream(outStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(textBytes, 0, textBytes.Length);
                        }
                    }

                    FilePath    = path;
                    fileChanged = false;
                    return(true);
                }
                catch (Exception e)
                {
                    MessageBox.Show(this,
                                    string.Format(Properties.Resources.DialogSaveErrorTitle, e.Message),
                                    Properties.Resources.DialogSaveErrorTitle,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    key.Fill <byte>(0, 0, key.Length);
                }
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Loads a file from the given path.
        /// </summary>
        private bool Load(string path)
        {
            PasswordDialog dialog = new PasswordDialog();

            dialog.NeedsVerifcation = false;

            // If we are opening a file on startup, the main window has not been visible yet, and
            // thus is not a valid owner at this point.
            if (IsVisible)
            {
                dialog.Owner = this;
            }

            if (dialog.ShowDialog() == true)
            {
                SecureString password = dialog.Password;
                byte[]       key      = new byte[dialog.EncryptionConfig.KeySize / 8];
                using (SecureStringBytes pwBytes = new SecureStringBytes(password, Encoding.UTF8))
                {
                    password.Dispose();
                    dialog.EncryptionConfig.KeyAlgo.GenerateKey(pwBytes.Bytes, key);
                }

                string text;

                try
                {
                    SymmetricAlgorithm algo = dialog.EncryptionConfig.Algo.GetImplementation();
                    algo.KeySize = dialog.EncryptionConfig.KeySize;
                    algo.Key     = key;

                    using (FileStream inStream = new FileStream(path, FileMode.Open))
                    {
                        byte[] iv = new byte[algo.IV.Length];
                        for (int i = 0; i < algo.IV.Length; i++)
                        {
                            iv[i] = (byte)inStream.ReadByte();
                        }
                        algo.IV = iv;

                        ICryptoTransform encryptor = algo.CreateDecryptor();
                        using (CryptoStream cryptoStream = new CryptoStream(inStream, encryptor, CryptoStreamMode.Read))
                        {
                            StreamReader reader = new StreamReader(cryptoStream, dialog.TextEncoding);
                            text = reader.ReadToEnd();
                        }
                    }

                    NewHistory(() => textEditor.Text = text);
                    FilePath = path;

                    return(true);
                }
                catch (CryptographicException e)
                {
                    MessageBox.Show(this,
                                    string.Format(Properties.Resources.DialogReadCryptoErrorText, e.Message),
                                    Properties.Resources.DialogReadCryptoErrorTitle,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (Exception e)
                {
                    MessageBox.Show(this,
                                    string.Format(Properties.Resources.DialogReadErrorTitle, e.Message),
                                    Properties.Resources.DialogReadErrorTitle,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    key.Fill <byte>(0, 0, key.Length);
                }
            }

            return(false);
        }