Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <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);
                }
            }
        }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        /*
         * 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);
                    }
                }
            });
        }