/// <summary> /// Register new user and saves all data in json file /// </summary> /// <param name="user"></param> /// <returns>0 - if registration went successful, /// 1 - if some error occured, /// 2 - if username is already used</returns> public static int Register(User user) { string hashedUsername = DataCryptography.SHA512(user.GetUsername()); if (IsUsernameNotTaken(hashedUsername)) { User userHashedData = new User(DataCryptography.SHA512(user.GetFirstName()), DataCryptography.SHA512(user.GetLastName()), hashedUsername, DataCryptography.SHA512(user.GetPassword()), DataCryptography.SHA512(user.GetBirthday()), user.GetQuestion(), DataCryptography.SHA512(user.GetAnswer().ToLower()), DataCryptography.SHA512(DataCryptography.GenerateAesKey(user.GetUsername(), user.GetPassword(), user.GetQuestion(), user.GetAnswer().ToLower()))); if (IOClass.SaveNewUser(userHashedData, user.GetUsername())) { return(0); } else { return(1); } } return(2); }
/// <summary> /// Deletes file paths that are no longer protected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteFilesBtn_Click(object sender, RoutedEventArgs e) { string selectedItem = listBoxFiles.SelectedItem.ToString(); Files.Remove(selectedItem); IOClass.SaveFilesList(Files, @"C:\temp\files.json"); listBoxFiles.ItemsSource = Files; }
/// <summary> /// Binds folder paths data to list boxes /// </summary> private void bindFoldersListBox() { Folders = IOClass.ReadFoldersList(this.UserFoldersFilepath); if (Folders.Count > 0) { listBoxFolders.ItemsSource = Folders; } }
/// <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> /// Deletes folder paths that are no longer protected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteFoldersBtn_Click(object sender, RoutedEventArgs e) { string selectedItem = listBoxFolders.SelectedItem.ToString(); ProcessDirectory(selectedItem, false); IOClass.SaveFilesList(Files, this.UserFilesFilepath); Folders.Remove(selectedItem); IOClass.SaveFoldersList(Folders, this.UserFoldersFilepath); listBoxFiles.ItemsSource = Files; listBoxFolders.ItemsSource = Folders; }
/// <summary> /// Checks if passed username is already used by another user /// </summary> /// <param name="username">Users username passed from register form</param> /// <returns>True - if username is not used, /// False - if username is used</returns> public static bool IsUsernameNotTaken(string username) { List <User> usersList = IOClass.ReadUsersList(); foreach (User user in usersList) { if (user.GetUsername().Equals(username)) { return(false); } } return(true); }
/// <summary> /// Binds file paths data to list boxes /// </summary> private void bindFilesListBox() { Files = IOClass.ReadFilesList(UserFilesFilepath); EncryptedFiles = IOClass.ReadFilesList(this.UserEncryptedFilesFilepath); if (Files.Count > 0) { listBoxFiles.ItemsSource = Files; } else if (EncryptedFiles.Count >= 0) { listBoxFiles.ItemsSource = EncryptedFiles; } }
/// <summary> /// Activates Ffle dialog and saves file paths that will be protected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addFilesBtn_Click(object sender, RoutedEventArgs e) { CommonOpenFileDialog fileDialog = new CommonOpenFileDialog { InitialDirectory = @"C:\", Multiselect = true }; if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok) { Files = IOClass.ReadFilesList(this.UserFilesFilepath); Files.AddRange(fileDialog.FileNames); listBoxFiles.ItemsSource = Files; IOClass.SaveFilesList(Files, this.UserFilesFilepath); } }
/// <summary> /// Checks user's credentials and allows to log in /// </summary> /// <param name="login"></param> /// <param name="password"></param> /// <returns>True - if credentials were ok, /// False - if credentials were wrong</returns> public static bool Login(string login, string password) { List <User> usersList = IOClass.ReadUsersList(); string loginHash = DataCryptography.SHA512(login); string passwordHash = DataCryptography.SHA512(password); foreach (User user in usersList) { if (user.GetUsername().Equals(loginHash)) { if (user.GetPassword().Equals(passwordHash)) { return(true); } } } return(false); }
/// <summary> /// Creates an authentication key from selected device and saves it to the list. /// Adds a private key to device and public key to user data. /// </summary> /// <param name="deviceData"></param> /// <returns>Returns true if procedure went well</returns> bool addTrustedDevice(string deviceData) { // Temporary limit of trusted devices due to lack of multi-private-key system if (TrustedDevices.Count.Equals(0)) { string[] deviceDataArray = deviceData.Split(':'); string deviceModel = deviceDataArray[0].Trim(); string deviceName = deviceDataArray[1].Trim(); foreach (var device in ConnectedDevices) { if (device.VolumeName.Equals(deviceName) && device.Model.Equals(deviceModel)) { TrustedDevices.Add(device); string publicKeyString, privateKeyString; (publicKeyString, privateKeyString) = DataCryptography.GenerateRsaKeys(); string aesKey = LoggedUser.GetAesKey(); string encryptedAesKey = DataCryptography.EncryptAESKey(aesKey, publicKeyString); if (IOClass.SaveTrustedDevicesList(TrustedDevices, this.UserKeyDataFilepath) && IOClass.SavePrivateKeyOnDevice(device.Path, privateKeyString)) { LoggedUser.SetPublicKeyXmlString(publicKeyString); LoggedUser.SetAesKey(encryptedAesKey); LoggedUser.SetKeysQuantity(LoggedUser.GetKeysQuantity() + 1); IOClass.UpdateUser(LoggedUser); break; } else { MessageBox.Show("Couldn't create an authentication key.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } return(true); } else { MessageBox.Show("You already have your trusted device. Couldn't create another one.", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return(false); } }
/// <summary> /// Deletes a device from trusted devices list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteTrustedDeviceBtn_Click(object sender, RoutedEventArgs e) { MessageBoxResult result1 = MessageBox.Show("Do you want to delete " + listBoxTrustedDevices.SelectedItem.ToString() + "?", "Delete an authentication key?", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result1.Equals(MessageBoxResult.Yes)) { if (Files.Count > 0) { MessageBoxResult result2 = MessageBox.Show("You want to delete a key with associated files with it. " + "They will no longer be protected! Are you sure?", "Associated files found!", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result2.Equals(MessageBoxResult.Yes)) { Files.Clear(); IOClass.SaveFilesList(Files, this.UserFilesFilepath); bindFilesListBox(); bindDeviceListBoxes(); if (deleteTrustedDevice(listBoxTrustedDevices.SelectedItem.ToString())) { MessageBox.Show("The authentication key has been deleted.", "Deleting successful", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Couldn't delete an authentication key.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } else { if (deleteTrustedDevice(listBoxTrustedDevices.SelectedItem.ToString())) { MessageBox.Show("The authentication key has been deleted.", "Deleting successful", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Couldn't delete an authentication key.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } }
/// <summary> /// Deletes an authentication key from the list /// </summary> /// <param name="deviceData"></param> /// <returns></returns> bool deleteTrustedDevice(string deviceData) { string[] deviceDataArray = deviceData.Split(':'); string deviceModel = deviceDataArray[0].Trim(); string deviceName = deviceDataArray[1].Trim(); foreach (var device in TrustedDevices) { if (device.VolumeName.Equals(deviceName) && device.Model.Equals(deviceModel)) { TrustedDevices.Remove(device); if (IOClass.RemovePrivateKeyFromDevice(device.Path) && IOClass.SaveTrustedDevicesList(TrustedDevices, this.UserKeyDataFilepath)) { LoggedUser.SetPublicKeyXmlString(null); LoggedUser.SetAesKey(String.Empty); LoggedUser.SetKeysQuantity(LoggedUser.GetKeysQuantity() - 1); IOClass.UpdateUser(LoggedUser); return(true); } } } return(false); }
/// <summary> /// Activates File Dialog and saves folder paths that will be protected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addFoldersBtn_Click(object sender, RoutedEventArgs e) { CommonOpenFileDialog folderDialog = new CommonOpenFileDialog { InitialDirectory = @"C:\", IsFolderPicker = true }; if (folderDialog.ShowDialog() == CommonFileDialogResult.Ok) { Folders = IOClass.ReadFoldersList(this.UserFoldersFilepath); Folders.AddRange(folderDialog.FileNames); foreach (string folderPath in folderDialog.FileNames) { ProcessDirectory(folderPath, true); } IOClass.SaveFilesList(Files, this.UserFilesFilepath); listBoxFolders.ItemsSource = Folders; listBoxFiles.ItemsSource = Files; IOClass.SaveFoldersList(Folders, this.UserFoldersFilepath); } }
public logonScreen() { InitializeComponent(); IOClass.CreateRootDirectory(); }
/// <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); } }
/* * Stworzyć lepszą walidację zaznaczonych elementów listboxów by operacje nie były dozwolone na obiektach null, które powodują NullReferenceException */ #region Constructor /// <summary> /// Sets default values and starts a new task which checks connected devices /// </summary> public MainWindow(string username) { InitializeComponent(); DidUserLoggedUnauthorized = true; DeviceListChanged = false; deleteTrustedDeviceBtn.IsEnabled = false; labelUsername.Content = "Hello " + username; RsaPrivateKey = string.Empty; AuthorizationStatus = false; this.Username = username; List <User> usersList = IOClass.ReadUsersList(); string hashedUsername = DataCryptography.SHA512(username); bindFilesListBox(); bindFoldersListBox(); foreach (User user in usersList) { if (user.GetUsername().Equals(hashedUsername)) { LoggedUser = user; } } labelSecurityQuestion.Content = LoggedUser.GetQuestion(); this.UserFilesFilepath = @"C:\PAAK\" + username + @"\files.json"; this.UserFoldersFilepath = @"C:\PAAK\" + username + @"\folders.json"; this.UserKeyDataFilepath = @"C:\PAAK\" + username + @"\data.json"; this.UserEncryptedFilesFilepath = @"C:\PAAK\" + username + @"\encryptedFiles.json"; var uiSyncContext = SynchronizationContext.Current; var loopTask = Task.Run(() => { while (true) { Task.Delay(1000); UpdateConnectedDevices(); if (DeviceListChanged) { if (ConnectedTrustedDevices != null) { if (ConnectedTrustedDevices.Count.Equals(1)) { AuthorizationStatus = true; if (RsaPrivateKey.Equals(string.Empty)) { RsaPrivateKey = IOClass.ReadPrivateKeyFromDeviceToString(ConnectedTrustedDevices[0].Path); } } else { AuthorizationStatus = false; } } uiSyncContext.Post((s) => { UpdateDevicesStatus(); }, null); } } }); }