/// <summary> /// Provides files recovery feature /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRecoverFiles_Click(object sender, RoutedEventArgs e) { string securityAnswer = textBoxSecurityAnswer.Text.ToLower(); string password = textBoxRecoveryPassword.Password; if (EncryptedFiles.Count.Equals(0)) { MessageBox.Show("You have no files to recover.", "No encrypted files found", MessageBoxButton.OK, MessageBoxImage.Information); } else { if (DataCryptography.SHA512(securityAnswer).Equals(LoggedUser.GetAnswer()) && DataCryptography.SHA512(password).Equals(LoggedUser.GetPassword())) { MessageBoxResult result = MessageBox.Show("Do you want to recover your files? All your program settings and keys will be deleted.", "Files recovery system", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result.Equals(MessageBoxResult.Yes)) { string aesKey = DataCryptography.SHA512(DataCryptography.GenerateAesKey(this.Username, password, LoggedUser.GetQuestion(), securityAnswer)); List <string> tempEncryptedFiles = new List <string>(EncryptedFiles); EncryptedFiles.Clear(); Files.Clear(); Folders.Clear(); TrustedDevices.Clear(); var decryptionTask = Task.Run(() => { foreach (string encryptedFilePath in tempEncryptedFiles) { string filePath = DataCryptography.FileDecrypt(encryptedFilePath, aesKey); } }); decryptionTask.Wait(); LoggedUser.SetPublicKeyXmlString(String.Empty); LoggedUser.SetAesKey(aesKey); IOClass.SaveFilesList(Files, this.UserFilesFilepath); IOClass.SaveFilesList(EncryptedFiles, this.UserEncryptedFilesFilepath); IOClass.SaveFoldersList(Folders, this.UserFoldersFilepath); IOClass.SaveTrustedDevicesList(TrustedDevices, this.UserKeyDataFilepath); IOClass.UpdateUser(LoggedUser); bindFilesListBox(); bindFoldersListBox(); MessageBox.Show("Your files are decrypted now.", "Files recovery system", MessageBoxButton.OK, MessageBoxImage.Information); } } else { MessageBox.Show("Security answer or password incorrect!", "Files recovery system", MessageBoxButton.OK, MessageBoxImage.Warning); } } }
/// <summary> /// Updates all data about devices and sets authorization status /// </summary> private void UpdateDevicesStatus() { TrustedDevices = IOClass.ReadTrustedDevicesList(this.UserKeyDataFilepath); UpdateConnectedTrustedDevices(); bindDeviceListBoxes(); labelKeysCounter.Content = TrustedDevices.Count; if (Files.Count > 0) { labelFilesCounter.Content = Files.Count; } else if (EncryptedFiles.Count >= 0) { labelFilesCounter.Content = EncryptedFiles.Count; } var uiSyncContext = SynchronizationContext.Current; // Decrypt all data and inform a user about authorization status if (AuthorizationStatus) { DidUserLoggedUnauthorized = false; if (EncryptedFiles.Count > 0) { // New Task to decrypt all files var decryptionTask = Task.Run(() => { string password = DataCryptography.DecryptAESKey(LoggedUser.GetAesKey(), RsaPrivateKey); GCHandle gCHandle = GCHandle.Alloc(password, GCHandleType.Pinned); foreach (string encryptedFilePath in EncryptedFiles) { string filePath = DataCryptography.FileDecrypt(encryptedFilePath, password); Files.Add(filePath); } DataCryptography.ZeroMemory(gCHandle.AddrOfPinnedObject(), password.Length * 2); gCHandle.Free(); }); decryptionTask.Wait(); EncryptedFiles.Clear(); IOClass.SaveFilesList(Files, this.UserFilesFilepath); IOClass.SaveFilesList(EncryptedFiles, this.UserEncryptedFilesFilepath); } uiSyncContext.Post((s) => { labelStatus.Foreground = new SolidColorBrush(Colors.GreenYellow); labelStatus.Content = "Authorized"; IconLock.Kind = MahApps.Metro.IconPacks.PackIconMaterialKind.LockOpen; IconLock.Foreground = new SolidColorBrush(Colors.GreenYellow); bindFilesListBox(); }, null); } else // Encrypt all data and inform a user about authorization status { // Prevents from double encryption if user logs in unauthorized if (!DidUserLoggedUnauthorized && Files.Count > 0) { // New Task to encrypt all files var encryptionTask = Task.Run(() => { string password = DataCryptography.DecryptAESKey(LoggedUser.GetAesKey(), RsaPrivateKey); GCHandle gCHandle = GCHandle.Alloc(password, GCHandleType.Pinned); foreach (string filePath in Files) { string encryptedFilePath = DataCryptography.FileEncrypt(filePath, password); EncryptedFiles.Add(encryptedFilePath); } DataCryptography.ZeroMemory(gCHandle.AddrOfPinnedObject(), password.Length * 2); gCHandle.Free(); }); encryptionTask.Wait(); Files.Clear(); IOClass.SaveFilesList(Files, this.UserFilesFilepath); IOClass.SaveFilesList(EncryptedFiles, this.UserEncryptedFilesFilepath); } uiSyncContext.Post((s) => { labelStatus.Foreground = new SolidColorBrush(Colors.OrangeRed); labelStatus.Content = "Unauthorized"; IconLock.Kind = MahApps.Metro.IconPacks.PackIconMaterialKind.Lock; IconLock.Foreground = new SolidColorBrush(Colors.OrangeRed); bindFilesListBox(); }, null); } }