Beispiel #1
0
        private async void UploadImage()
        {
            if (_imagePreview == null)
            {
                await App.Current.MainPage.DisplayAlert("Error", "You haven't picked an image yet", "Ok");
            }
            else
            {
                try
                {
                    //start actiity indicator
                    ActivityIndicator = true;

                    //disable buttons
                    BtnUploadImage = false;
                    BtnPickImage   = false;

                    string galleryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
                    string outputPath  = Path.Combine(galleryPath + "/Vault", Path.GetFileName(_file.Path) + ".aes");
                    FileEncrypt(_file.Path);
                    var user = await FirebaseHelper.GetUser(_email);

                    FileStream filestream = System.IO.File.OpenRead(outputPath);

                    await FirebaseHelper.UploadImage(filestream, Path.GetFileName(_file.Path), user.UserID);

                    var downloadurl = await FirebaseHelper.GetImage(Path.GetFileName(_file.Path), user.UserID);

                    await FirebaseHelper.UploadImageURL(Path.GetFileName(_file.Path), downloadurl.ToString(), user.UserID);

                    //delete encrypted file we create on device
                    if (File.Exists(outputPath))
                    {
                        File.Delete(outputPath);
                    }

                    //stop activity indicator
                    ActivityIndicator = false;

                    await App.Current.MainPage.DisplayAlert("Success", "Image has been uploaded", "OK");

                    ImagePreview = "";

                    //re-enable PickImage button
                    BtnPickImage = true;
                }
                catch
                {
                    await App.Current.MainPage.DisplayAlert("Error", "Error in uploading image, please try again", "Ok");

                    ImagePreview      = "";
                    ActivityIndicator = false;
                    BtnPickImage      = true;
                    BtnUploadImage    = false;
                }
            }
        }
Beispiel #2
0
        private async void Login()
        {
            ActivityIndicator = true;

            //check if email or password fields are null or empty
            if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
            {
                ActivityIndicator = false;
                await App.Current.MainPage.DisplayAlert("Empty Fields", "Email and Password field cannot be empty", "OK");
            }
            else
            {
                //call GetUser function from FirebaseHelper class
                var user = await FirebaseHelper.GetUser(Email);

                if (user != null)
                {
                    //get stored hashed password
                    byte[] hashBytes = Convert.FromBase64String(user.Password);

                    //hash entered password
                    byte[] salt = new byte[16];
                    Array.Copy(hashBytes, 0, salt, 0, 16);
                    var    pbkdf2 = new Rfc2898DeriveBytes(Password, salt, 10000);
                    byte[] hash   = pbkdf2.GetBytes(20);

                    //compare entered password to stored password
                    bool passwordMatches = true;
                    for (int i = 0; i < 20; i++)
                    {
                        if (hashBytes[i + 16] != hash[i])
                        {
                            passwordMatches = false;
                        }
                    }

                    if (Email == user.Email && passwordMatches == true)
                    {
                        ActivityIndicator = false;
                        await App.Current.MainPage.DisplayAlert("Login Successful", "Welcome to your Vault " + user.FirstName, "Ok");

                        //set welcome page as new navigation page instead of navigating to it so user can't press back button to come back to login page
                        App.Current.MainPage = new NavigationPage(new WelcomePage(Email));
                    }
                    else
                    {
                        ActivityIndicator = false;
                        await App.Current.MainPage.DisplayAlert("Login Failed", "Please enter correct password", "OK");
                    }
                }
                else
                {
                    ActivityIndicator = false;
                    await App.Current.MainPage.DisplayAlert("Login Failed", "Please enter correct email address", "OK");
                }
            }
        }
Beispiel #3
0
        private async void FileEncrypt(string inputFile)
        {
            try
            {
                string galleryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
                string outputPath  = Path.Combine(galleryPath + "/Vault", Path.GetFileName(_file.Path) + ".aes");
                var    user        = await FirebaseHelper.GetUser(_email);

                FileStream fsCrypt = new FileStream(outputPath, FileMode.Create);
                //Set Rijndael symmetric encryption algorithm
                RijndaelManaged AES = new RijndaelManaged();
                AES.KeySize   = 256;
                AES.BlockSize = 128;
                AES.Padding   = PaddingMode.PKCS7;
                var key = new Rfc2898DeriveBytes(user.Key, user.Salt, 50000);
                AES.Key  = key.GetBytes(AES.KeySize / 8);
                AES.IV   = key.GetBytes(AES.BlockSize / 8);
                AES.Mode = CipherMode.CFB;
                // write salt to the begining of the output file
                fsCrypt.Write(user.Salt, 0, user.Salt.Length);
                CryptoStream cs   = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
                FileStream   fsIn = new FileStream(inputFile, FileMode.Open);

                //create a buffer (1mb) so only this amount will allocate in the memory and not the whole file
                //1048576 is 1MB in binary
                byte[] buffer = new byte[1048576];
                int    read;
                try
                {
                    while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        cs.Write(buffer, 0, read);
                    }
                    fsIn.Close();
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Encryption Error", "Please try again", "Ok");
                }
                finally
                {
                    cs.Close();
                    fsCrypt.Close();
                }
            }
            catch
            {
                await App.Current.MainPage.DisplayAlert("Encryption Failed", "Error uploading image, please try again", "Ok");
            }
        }
Beispiel #4
0
        private async void Submit()
        {
            var user = await FirebaseHelper.GetUser(_email);

            if (!(string.IsNullOrEmpty(_password) | string.IsNullOrEmpty(_passwordConfirmation)))
            {
                if (_password == _passwordConfirmation)
                {
                    bool PasswordValid = IsValidPassword(_password);

                    if (PasswordValid == true)
                    {
                        string hashedPassword = string.Empty;
                        hashedPassword = HashPassword(_password);

                        try
                        {
                            await FirebaseHelper.UpdateUser(user.UserID, user.FirstName, user.Surname, user.Email, hashedPassword, user.Key, user.Salt);

                            await App.Current.MainPage.DisplayAlert("Success", "Password has been changed", "Ok");

                            await App.Current.MainPage.Navigation.PopAsync();
                        }
                        catch (Exception e)
                        {
                            await App.Current.MainPage.DisplayAlert("Error", "Password change failed, please try again", "Ok");
                        }
                    }
                    else
                    {
                        await App.Current.MainPage.DisplayAlert("Invalid Password", "Please enter a password in line with requirements", "OK");
                    }
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Error", "Passwords must match", "Ok");
                }
            }
            else
            {
                await App.Current.MainPage.DisplayAlert("Empty Values", "Fields should not be empty", "Ok");
            }
        }
Beispiel #5
0
        private async void FileEncrypt(string inputFile)
        {
            try
            {
                string galleryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMovies).AbsolutePath;
                string outputPath  = Path.Combine(galleryPath + "/Vault", Path.GetFileName(_file.Path) + ".aes");
                var    user        = await FirebaseHelper.GetUser(_email);

                FileStream fsCrypt = new FileStream(outputPath, FileMode.Create);
                //Set Rijndael symmetric encryption algorithm
                RijndaelManaged AES = new RijndaelManaged();
                AES.KeySize   = 128;//set to 128 bit for videos to make faster
                AES.BlockSize = 128;
                AES.Padding   = PaddingMode.PKCS7;
                var key = new Rfc2898DeriveBytes(user.Key, user.Salt, 50000);
                AES.Key  = key.GetBytes(AES.KeySize / 8);
                AES.IV   = key.GetBytes(AES.BlockSize / 8);
                AES.Mode = CipherMode.CFB;
                // write salt to the begining of the output file
                fsCrypt.Write(user.Salt, 0, user.Salt.Length);
                CryptoStream cs   = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
                FileStream   fsIn = new FileStream(inputFile, FileMode.Open);

                //create a buffer (1mb) so only this amount will allocate in the memory and not the whole file
                //1048576 is 1MB in binary
                byte[] buffer = new byte[1048576];
                int    read;
                try
                {
                    while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        cs.Write(buffer, 0, read);
                    }
                    fsIn.Close();
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Encryption Error", " Please try again", "Ok");
                }
                finally
                {
                    cs.Close();
                    fsCrypt.Close();
                }
            }
            catch (Exception e)
            {
                await App.Current.MainPage.DisplayAlert("Encryption Failed", "Please try again", "Ok");
            }

            string galleryPath1 = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMovies).AbsolutePath;
            string outputPath1  = Path.Combine(galleryPath1 + "/Vault", Path.GetFileName(_file.Path) + ".aes");

            var user1 = await FirebaseHelper.GetUser(_email);

            FileStream filestream = File.OpenRead(outputPath1);

            await FirebaseHelper.UploadVideo(filestream, Path.GetFileName(_file.Path), user1.UserID);

            var downloadurl = await FirebaseHelper.GetVideo(Path.GetFileName(_file.Path), user1.UserID);

            await FirebaseHelper.UploadVideoURL(Path.GetFileName(_file.Path), downloadurl.ToString(), user1.UserID);

            //delete encrypted file we create on device
            if (File.Exists(outputPath1))
            {
                File.Delete(outputPath1);
            }

            //stop activity indicator
            ActivityIndicator = false;

            await App.Current.MainPage.DisplayAlert("Upload Success", "Video has been uploaded", "OK");

            Source = string.Empty;

            //re-enable PickImage button
            BtnPickVideo = true;
        }
Beispiel #6
0
        private async void Submit()
        {
            if (string.IsNullOrEmpty(_email) || string.IsNullOrEmpty(_emailConfirmation))
            {
                await App.Current.MainPage.DisplayAlert("Empty values error", "Both Email and Email Confirmation must not be empty", "Ok");
            }
            else
            {
                if (_email != _emailConfirmation)
                {
                    await App.Current.MainPage.DisplayAlert("Matching values error", "Both Email and Email Confirmation must match", "Ok");
                }
                else
                {
                    var user = await FirebaseHelper.GetUser(_email);

                    if (user == null)
                    {
                        await App.Current.MainPage.DisplayAlert("Email address not found", "Please ensure you have provided the correct email address used with your account", "Ok");
                    }
                    else
                    {
                        //reset users password to random password
                        try
                        {
                            string password       = "******";
                            string hashedPassword = string.Empty;
                            hashedPassword = HashPassword(password);
                            await FirebaseHelper.UpdateUser(user.UserID, user.FirstName, user.Surname, user.Email, hashedPassword, user.Key, user.Salt);
                        }
                        catch
                        {
                            await App.Current.MainPage.DisplayAlert("Password reset error", "Failed to reset password, please try again", "Ok");
                        }

                        //send user an email telling them new password and advise to reset it straight away after logging in
                        try
                        {
                            MailMessage mail       = new MailMessage();
                            SmtpClient  SmtpServer = new SmtpClient("smtp.gmail.com");
                            mail.From = new MailAddress("*****@*****.**");
                            mail.To.Add(user.Email);
                            mail.Subject = "Your Temporary Vault Password";
                            mail.Body    = "Dear " + user.FirstName + ", \n Your new temporary password is Cherry123. Please login with your email and this temporary password and change your password immediately after logging in. \n  Kind Regards \n Vault Management Team";

                            SmtpServer.Port                  = 587;
                            SmtpServer.Host                  = "smtp.gmail.com";
                            SmtpServer.EnableSsl             = true;
                            SmtpServer.UseDefaultCredentials = false;
                            SmtpServer.Credentials           = new System.Net.NetworkCredential("", "");//insert creds when want to use
                            SmtpServer.Send(mail);

                            await App.Current.MainPage.DisplayAlert("Password reset success", "Please check your email inbox or junk folder for the email sent out to you", "Ok");

                            await App.Current.MainPage.Navigation.PopAsync();
                        }
                        catch
                        {
                            await App.Current.MainPage.DisplayAlert("Email sending error", "Failed to send email, please try again", "Ok");
                        }
                    }
                }
            }
        }
Beispiel #7
0
        private async void SetupListViewItems()
        {
            User user = await FirebaseHelper.GetUser(_email);

            ImagesList = await FirebaseHelper.GetUsersImageObjects(user.UserID);
        }
Beispiel #8
0
        private async void FileDecrypt(string inputFile, string outputFile)
        {
            try
            {
                var user = await FirebaseHelper.GetUser(_email);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
                fsCrypt.Read(user.Salt, 0, user.Salt.Length);
                RijndaelManaged AES = new RijndaelManaged();
                AES.KeySize   = 128;
                AES.BlockSize = 128;
                AES.Padding   = PaddingMode.PKCS7;
                var key = new Rfc2898DeriveBytes(user.Key, user.Salt, 50000);
                AES.Key  = key.GetBytes(AES.KeySize / 8);
                AES.IV   = key.GetBytes(AES.BlockSize / 8);
                AES.Mode = CipherMode.CFB;
                CryptoStream cs    = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
                FileStream   fsOut = new FileStream(outputFile, FileMode.Create);
                int          read;
                byte[]       buffer = new byte[1048576];
                try
                {
                    while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fsOut.Write(buffer, 0, read);
                    }
                }
                catch (CryptographicException ex_CryptographicException)
                {
                    await App.Current.MainPage.DisplayAlert("Error", "Decryption error, please try again", "Ok");

                    await App.Current.MainPage.Navigation.PopAsync();
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Error", "Decryption error, please try again", "Ok");

                    await App.Current.MainPage.Navigation.PopAsync();
                }
                try
                {
                    cs.Close();
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Error", "Decryption error, please try again", "Ok");

                    await App.Current.MainPage.Navigation.PopAsync();
                }
                finally
                {
                    fsOut.Close();
                    fsCrypt.Close();
                }

                //to show videos in users gallery
                MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] { outputFile }, new string[] { "video / mp4", "video/ mp3" }, null);

                //display video in video preview
                Source = outputFile;

                //delete encrypted file we downloaded before
                if (File.Exists(inputFile))
                {
                    File.Delete(inputFile);
                }

                ActivityIndicator = false;
                BtnDownload       = true;
                BtnDelete         = true;
            }
            catch
            {
                await App.Current.MainPage.DisplayAlert("Decryption Error", "Please try again", "Ok");

                await App.Current.MainPage.Navigation.PopAsync();
            }
        }