示例#1
0
        /// <summary>
        ///     Asks user for a name and creates a new safe
        /// </summary>
        private void NewSafe()
        {
            //Gets new name
            string newName = DialogBox.TextInputDialogBox("Please enter the name for your new Safe:", "Create", "Cancel",
                this);

            if (string.IsNullOrEmpty(newName)) return;

            //Checks that that file name is valid
            if (newName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
            {
                DialogBox.MessageDialogBox(
                    "A file's name cannot contain any of the following characters:\n\\/:*?\"<>|", this);
                return;
            }

            //Checks that safe name isn't already being used
            string[] files = Directory.GetFiles(@"Safes", "*.safe");

            //Takes the name of each json file e.g. C:/Users/John/Documents/Safe/test.safe => test
            files = files.Select(x => x.Split('\\').Last().Split('.')[0]).ToArray();

            if (files.Any(x => x == newName) &&
                !DialogBox.QuestionDialogBox(
                    "A file with that name already exists, are you sure you want to override it?", false, this))
                return;

            //Gets password
            string password;
            if (!LoginWindow.GetAndConfirmNewPassword(out password, this)) return;

            //Creates new RootObject
            string versionNumber = string.Join(".",
                Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(2));
            RootObject rootObject = new RootObject
            {
                Folders = new List<Folder>(),
                Accounts = new List<Account>(),
                VersionNumber = versionNumber
            };

            //Turns it into json and encrypts it
            string jsonText = JsonConvert.SerializeObject(rootObject);
            string encryptedText = AESThenHMAC.SimpleEncryptWithPassword(jsonText, password);

            //Creates file
            File.Create($"Safes\\{newName}.safe").Close();
            File.WriteAllText($"Safes\\{newName}.safe", encryptedText);

            //Create MainWindow
            MainWindow mainWindow = new MainWindow($"{newName}.safe", rootObject, password);
            try
            {
                mainWindow.Show();
            }
            catch (InvalidOperationException)
            {
                return;
            }

            Close();
        }
        /// <summary>
        ///     Logs into the password safe
        /// </summary>
        private void LoginOnClick(object sender, RoutedEventArgs e)
        {
            //Checks a password has been entered
            if (PasswordInput.Password.Length == 0)
            {
                DialogBox.MessageDialogBox("Please enter a password.", this);
                return;
            }

            string decryptedContent;
            bool loadingBackup = false;

            int result = TryLoadAndDecrypt($"Safes\\{SafeSelector.SelectedValue}.safe", out decryptedContent);
            if (result == 2)
            {
                result = TryLoadAndDecrypt($"Safes\\{SafeSelector.SelectedValue}.safe.bak", out decryptedContent);

                if (result != 0)
                {
                    DialogBox.MessageDialogBox(
                        "This safe appears to be corrupted and a backup could not be recovered.", this);
                    return;
                }
                loadingBackup = true;
            }
            else if (result == 1)
            {
                DialogBox.MessageDialogBox("There was an error trying to load that safe.", this);
                return;
            }

            //Checks password was correct
            if (decryptedContent == null)
            {
                DialogBox.MessageDialogBox("That password is incorrect, please try again.", this);
                return;
            }

            //Create SafeData
            RootObject safeData;
            try
            {
                safeData = JsonConvert.DeserializeObject<RootObject>(decryptedContent);
            }
            catch (JsonReaderException)
            {
                DialogBox.MessageDialogBox("That safe is not a valid file format.", this);
                return;
            }

            //Create MainWindow
            MainWindow mainWindow = new MainWindow($"{SafeSelector.SelectedValue}.safe", safeData,
                PasswordInput.Password);
            try
            {
                mainWindow.Show();
            }
            catch (InvalidOperationException)
            {
                return;
            }
            if (loadingBackup)
                DialogBox.MessageDialogBox("The safe appears to be corrupted and an older version has been loaded",
                    mainWindow);

            MainWindow.Profile.SetValue("General", "LastSafeOpened", SafeSelector.SelectedValue);

            Close();
        }