/*** Private Methods ***************************************************************************************************/ /// <summary> /// Try to setup User's password. If success, then initial variables as /// needed and navigate to page HomePage. Otherwise ask for new password. /// </summary> /// <returns></returns> private void PasswordTryCreate() { try { if (PwBoxPw2.Password.Length >= mainPage.uintPasswordLengthMinimum) // Double check that password meets minimum length requirement. { mainPage.cryptographicKeyAppPassword = LibAES.CryptographicKeyPassword(PwBoxPw2.Password); if (mainPage.cryptographicKeyAppPassword != null) { string stringLockerPathGet = mainPage.DataStoreLockerPath(true, mainPage.stringLockerPath); if (stringLockerPathGet != null) { mainPage.ShowPageHomePage(); // Everything as expected so navigate to page HomePage. return; // Successful completion so return. } else { ErrorOccurred(mainPage.resourceLoader.GetString("SP_Error_Password_Setup")); // Could not save locker setup data. Please try different password. } } else { ErrorOccurred(mainPage.resourceLoader.GetString("UMP_Error_Password_Hash")); // Could not hash password. Try different password. } } else { ErrorOccurred(mainPage.resourceLoader.GetString("SP_Error_Password_Length")); // Password must have at least four characters. Please try different password. } } catch (Exception ex) { LibMPC.OutputMsgError(TblkResult, mainPage.UnhandledExceptionMessage("SetupPassword.PasswordTryCreate()", ex.GetType())); } }
/// <summary> /// Create sample file. Return file if found or created. Skip if encrypted version of file exists. /// </summary> /// <param name="storageFolderParent">Parent folder.</param> /// <param name="stringFilename">Filename of sample file including extension.</param> /// <param name="stringFileContent">Content to place in sample file.</param> /// <returns></returns> private async Task <StorageFile> CreateSampleFileAsync(StorageFolder storageFolderParent, string stringFilename, string stringFileContent) { try { IStorageItem iStorageItem = await storageFolderParent.TryGetItemAsync(stringFilename); if (iStorageItem != null) { if (iStorageItem.IsOfType(StorageItemTypes.File)) { return((StorageFile)iStorageItem); // File exist. } } iStorageItem = await storageFolderParent.TryGetItemAsync($"{stringFilename}{mainPage.stringExtensionNoArchive}"); if (iStorageItem != null) { return(null); // Encrypted version of file exists with FileType ".aes". } iStorageItem = await storageFolderParent.TryGetItemAsync($"{stringFilename}{mainPage.stringExtensionArchive}"); if (iStorageItem != null) { return(null); // Encrypted version of file exists with FileType ".arc". } // Encrypted version of file does not exist so create sample file. StorageFile storageFileSample = await storageFolderParent.CreateFileAsync(stringFilename); if (storageFileSample != null) { // Write content to file. IBuffer iBufferFileContent = LibAES.IBufferFromString($"{stringFilename}{Environment.NewLine}{Environment.NewLine}{stringFileContent}{Environment.NewLine}"); if (iBufferFileContent != null) { await FileIO.WriteBufferAsync(storageFileSample, iBufferFileContent); mainPage.boolSamplesCreated = true; // Debug.WriteLine($"SetupFolder.CreateSampleFolderAsync(): Created file {storageFileSample.Path}"); return(storageFileSample); } } return(null); } catch (Exception ex) { LibMPC.OutputMsgError(TblkResult, mainPage.UnhandledExceptionMessage("SetupFolder.CreateSampleFileAsync()", ex.GetType())); return(null); throw; } }
/*** Private Methods ***************************************************************************************************/ /// <summary> /// This method checks if entered password is valid. Otherwise, wrong password was entered so request user to enter another one. /// If password is valid then navigate to page HomePage. /// </summary> private void PasswordVerify() { mainPage.cryptographicKeyAppPassword = LibAES.CryptographicKeyPassword(PwBoxPw.Password); if (mainPage.cryptographicKeyAppPassword != null) { string stringStoreValue = mainPage.DataStoreLockerPath(false, null); if (stringStoreValue != null) { if (stringStoreValue.Equals(mainPage.stringLockerPath, StringComparison.OrdinalIgnoreCase)) { // Password is valid if decrypted path using entered password is same as locker folder path. // Debug.WriteLine($"PasswordVerify(): Password valid since {stringStoreValue}={mainPage.stringLockerPath}"); mainPage.ShowPageHomePage(); // Everything as expected so navigate to page HomePage. return; // Success. Correct password was entered. } else { // To test this error path, negate value in 'if' above and then run App. Then enter correct password. boolAppResetRequired = true; PwBoxPw.Visibility = Visibility.Collapsed; LibMPC.OutputMsgError(TblkPageMsg, LibMPC.JoinListString(Translate.TRS_EP_Error_PasswordVerify, EnumStringSeparator.TwoSpaces)); // Do not assemble string until needed to save memory. TblkResult.Text = string.Empty; TblkLockerResetMsg.Visibility = Visibility.Collapsed; LibMPC.ButtonVisibility(ButLockerReset, false); // Hide button so User is forced to click Continue to start Locker Reset. } } else { PasswordWrong(); } } else { ErrorOccurred(mainPage.resourceLoader.GetString("UMP_Error_Password_Hash")); // Could not hash password. Try different password. } }
/// <summary> /// Read and write encrypted Locker path setting ds_StringLockerPath to and from data store. Return path string if successful, null otherwise. /// Password is valid if decrypted path using entered password is same as locker folder path. /// If parameter boolPathEncrypt is false, then parameter stringPathToLocker is not used and can be set to null. /// </summary> /// <param name="boolPathEncrypt">If true then encrypt Locker path. Otherwise decrypt locker path and return it.</param> /// <param name="stringPathToLocker">Path to current Locker or null if reading path from data store.</param> /// <returns></returns> public string DataStoreLockerPath(bool boolPathEncrypt, string stringPathToLocker = null) { if (boolPathEncrypt) // Encrypt Locker folder path using RandomIV and save to data store. { // Encrypt Locker folder path using RandomIV. string stringStoreValueEncrypted = LibAES.StringEncrypt(stringPathToLocker, cryptographicKeyAppPassword, LibAES.IBufferIVRandom(), EnumModeIV.EmbedIV); if (stringStoreValueEncrypted != null) { applicationDataContainer.Values[ds_StringEncryptedLockerPath] = stringStoreValueEncrypted; // Save encrypted path to data store. // Debug.WriteLine($"MainPage.DataStoreLockerPath(): Saved RandomIV encrypted Locker path to data store value stringStoreValueEncrypted: {stringStoreValueEncrypted}"); } return(stringStoreValueEncrypted); // Return useless encrypted string so calling method can check if not null. } else // Read and decrypt Locker folder path from data store and then return it. { if (applicationDataContainer.Values.ContainsKey(ds_StringEncryptedLockerPath)) { // Decrypt Locker folder path using embeded RandomIV and return. // Debug.WriteLine("MainPage.DataStoreLockerPath(): Decrypted Locker folder path using embeded RandomIV."); return(LibAES.StringDecrypt((string)applicationDataContainer.Values[ds_StringEncryptedLockerPath], cryptographicKeyAppPassword, null, EnumModeIV.EmbedIV)); // Return value or null. } } return(null); }