Exemplo n.º 1
0
 private void CheckUserInput(DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel checkBindingModel)
 {
     //Checks if user has pressed OK for saving the source directory of the ecnrypted file
     if (result == DialogResult.OK)
     {
         //Saves the encrypted file name
         fileName = decryptFileDialog.FileName;
         //Sets the file name of the binding model
         decryptFileBindingModel.FileName = Path.GetFileName(fileName);
         //Sets the source path for decrypting a file
         decryptFileBindingModel.FileSourcePath = decryptFileSource = Path.GetFullPath(fileName);
         //Check if a file that the user wants to be decrypted is encrypted
         CheckIfFileIsEncrypted(decryptFileBindingModel, checkBindingModel);
     }
 }
        public void AESDecryptFile(string fileName, string filePath, DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel passwordBindingModel)
        {
            //Checks from the database if the password and the IV that the user has given matches the password and the IV of the ecnrypted file
            if (passwordBindingModel.Password == SaveZoneDbService.GetEntity(filePath).encrypted_password&& passwordBindingModel.IV == SaveZoneDbService.GetEntity(filePath).encrypted_IV)
            {
                //Makes a byte[] of the given file so that it can be used for mixing information
                //It is used by the algorithm to mix its information with the key and IV information
                byte[] plainFile = File.ReadAllBytes(filePath);
                //Sets an AES algorithm for decrypting file. AES is used also in ecnrypting file
                using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider())
                {
                    //Sets the block information to 128 bits
                    AES.BlockSize = 128;
                    //Sets the key to 128 bits
                    AES.KeySize = 128;

                    //Uses the IV and the password that is used for encrypting the file for decrypting the file
                    AES.IV   = Encoding.UTF8.GetBytes(SaveZoneDbService.GetEntity(filePath).encrypted_IV);
                    AES.Key  = Encoding.UTF8.GetBytes(SaveZoneDbService.GetEntity(filePath).encrypted_password);
                    AES.Mode = CipherMode.CBC;

                    //Uses a memory stream to write to the memory. It relates to where the stream is stored
                    //It is used to write over a binary data with AES algorithm.
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        //Decrypts file with CryptoStream
                        CryptoStream cryptoStream = new CryptoStream(memStream, AES.CreateDecryptor(), CryptoStreamMode.Write);

                        cryptoStream.Write(plainFile, 0, plainFile.Length);
                        //Cleares the block in case there is binary left
                        cryptoStream.FlushFinalBlock();
                        File.WriteAllBytes(filePath, memStream.ToArray());
                    }
                }
                //Saves the decrypted file to the database
                SaveZoneDbService.AddDecryptFileEngine(filePath, filePath, SaveZoneDbService.GetEntity(filePath).encrypted_IV, SaveZoneDbService.GetEntity(filePath).encrypted_password);
                //Removes the encrypted file from the database
                SaveZoneDbService.RemoveFromEncrypted(filePath);
                SaveZoneDbService.AddDecryptFileInfo(fileName, filePath, true);
                decryptFileBindingModel.IsDecrypted = true;
                MessageBox.Show("File Decrypted.", "File", MessageBoxButtons.OK, MessageBoxIcon.None, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            else
            {
                decryptFileBindingModel.IsDecrypted = false;
                MessageBox.Show("Wrong password or IV", "Wrong Password or IV", MessageBoxButtons.OK, MessageBoxIcon.Warning, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
        private void CheckIfFileIsEncrypted(EncryptFileServicer encryptFileServicer, DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel checkBindingModel)
        {
            if (SaveZoneDbService.GetEntity(decryptFileBindingModel.FileSourcePath) != null)
            {
                if (SaveZoneDbService.GetEntity(decryptFileBindingModel.FileSourcePath).encrypted_name == decryptFileBindingModel.FileSourcePath)
                {
                    CheckPasswordForm.ShowDialog();

                    if (checkBindingModel.OKIsPressed == true)
                    {
                        decryptFile.AESDecryptFile(decryptFileBindingModel.FileName, decryptFileSource, decryptFileBindingModel, checkBindingModel);
                    }

                    CheckPasswordForm.Dispose();
                    CheckPasswordForm = new CheckPassword();

                    CheckIfFileIsDecrypted(decryptFileBindingModel);
                }
            }
            else
            {
                MessageBox.Show("File is not encrypted", "File not encrypted", MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
 private void CheckUserInput(EncryptFileServicer encryptFileServicer, DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel checkBindingModel)
 {
     if (result == DialogResult.OK)
     {
         fileName = decryptFileDialog.FileName;
         decryptFileBindingModel.FileName       = Path.GetFileName(fileName);
         decryptFileBindingModel.FileSourcePath = decryptFileSource = Path.GetFullPath(fileName);
         CheckIfFileIsEncrypted(encryptFileServicer, decryptFileBindingModel, checkBindingModel);
     }
 }
Exemplo n.º 5
0
        private void CheckIfFileIsEncrypted(DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel checkBindingModel)
        {
            //Checks from the database if the file is encrypted. If it is not the query will return null
            if (SaveZoneDbService.GetEntity(decryptFileBindingModel.FileSourcePath) != null)
            {
                //Checks from the database if the file the user wants to decrypt matches with the encrypted file name
                if (SaveZoneDbService.GetEntity(decryptFileBindingModel.FileSourcePath).encrypted_name == decryptFileBindingModel.FileSourcePath)
                {
                    //Shows the CheckPasswordForm for the user to input password and IV for decryption.
                    CheckPasswordForm.ShowDialog();

                    //When OK is pressed on CheckPasswordForm it tries to decrypt the file if everything is done correctlly
                    if (checkBindingModel.OKIsPressed == true)
                    {
                        decryptFile.AESDecryptFile(decryptFileBindingModel.FileName, decryptFileSource, decryptFileBindingModel, checkBindingModel);
                    }

                    //Resets the form for next use
                    CheckPasswordForm.Dispose();
                    CheckPasswordForm = new CheckPassword();

                    //Checks if the file has decrypted correctlly
                    CheckIfFileIsDecrypted(decryptFileBindingModel);
                }
            }
            else
            {
                MessageBox.Show("File is not encrypted", "File not encrypted", MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
        public void AESDecryptFile(string fileName, string filePath, DecryptFileBindingModel decryptFileBindingModel, CheckPasswordBindingModel passwordBindingModel)
        {
            if (passwordBindingModel.Password == SaveZoneDbService.GetEntity(filePath).encrypted_password&& passwordBindingModel.IV == SaveZoneDbService.GetEntity(filePath).encrypted_IV)
            {
                byte[] plainFile = File.ReadAllBytes(filePath);
                using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider())
                {
                    AES.BlockSize = 128;
                    AES.KeySize   = 128;

                    AES.IV   = Encoding.UTF8.GetBytes(SaveZoneDbService.GetEntity(filePath).encrypted_IV);
                    AES.Key  = Encoding.UTF8.GetBytes(SaveZoneDbService.GetEntity(filePath).encrypted_password);
                    AES.Mode = CipherMode.CBC;

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        CryptoStream cryptoStream = new CryptoStream(memStream, AES.CreateDecryptor(), CryptoStreamMode.Write);

                        cryptoStream.Write(plainFile, 0, plainFile.Length);
                        cryptoStream.FlushFinalBlock();
                        File.WriteAllBytes(filePath, memStream.ToArray());
                    }
                }
                SaveZoneDbService.AddDecryptFileEngine(filePath, filePath, SaveZoneDbService.GetEntity(filePath).encrypted_IV, SaveZoneDbService.GetEntity(filePath).encrypted_password);
                SaveZoneDbService.RemoveFromEncrypted(filePath);
                SaveZoneDbService.AddDecryptFileInfo(fileName, filePath, true);
                decryptFileBindingModel.IsDecrypted = true;
                MessageBox.Show("File Decrypted.", "File", MessageBoxButtons.OK, MessageBoxIcon.None, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            else
            {
                decryptFileBindingModel.IsDecrypted = false;
                MessageBox.Show("Wrong password or IV", "Wrong Password or IV", MessageBoxButtons.OK, MessageBoxIcon.Warning, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
 private static void SetCancelIsPressed(CheckPassword CheckForm, CheckPasswordBindingModel checkPasswordBindingModel)
 {
     checkPasswordBindingModel.CancelIsPressed = true;
     CheckForm.Close();
 }
 private static void SetCheckPasswordBindingModel(string textBoxText, string textBox2Text, CheckPasswordBindingModel checkPasswordBindingModel)
 {
     //Sets the password and the IV of the CheckPasswordBindingModel bindingModel.
     checkPasswordBindingModel.Password = textBoxText;
     checkPasswordBindingModel.IV       = textBox2Text;
 }
 private static void SetCheckFileBindingModelOKIsPressed(CheckPasswordBindingModel checkPasswordBindingModel)
 {
     //Uses CheckPassword Form to close the specific form.
     //Uses CheckPasswordBindingModel to set that cancel is pressed on that form.
     checkPasswordBindingModel.OKIsPressed = true;
 }
 //Syncronous method of SetCancelIsPressedAsync
 public static void SetCancelIsPressedNonAsync(CheckPassword CheckForm, CheckPasswordBindingModel checkPasswordBindingModel)
 {
     SetCancelIsPressed(CheckForm, checkPasswordBindingModel);
 }
 public static async void SetCancelIsPressedAsync(CheckPassword CheckForm, CheckPasswordBindingModel checkPasswordBindingModel)
 {
     //Uses CheckPassword Form to close the specific form.
     //Uses CheckPasswordBindingModel to set that cancel is pressed on that form.
     await Task.Run(() => SetCancelIsPressed(CheckForm, checkPasswordBindingModel));
 }
 //Syncronous method of CheckIfTextBoxIsEmptyAsync
 public static void CheckIfTextBoxIsEmptyNonAsync(string textBoxText, string textBox2Text, CheckPassword CheckForm, CheckPasswordBindingModel checkPasswordBindingModel)
 {
     if (textBoxText != "" && textBox2Text != "")
     {
         SetCheckPasswordBindingModel(textBoxText, textBox2Text, checkPasswordBindingModel);
         SetCheckFileBindingModelOKIsPressed(checkPasswordBindingModel);
         CheckForm.Close();
     }
     else
     {
         ShowMessageBox();
     }
 }
        public static async void CheckIfTextBoxIsEmptyAsync(string textBoxText, string textBox2Text, CheckPassword CheckForm, CheckPasswordBindingModel checkPasswordBindingModel)
        {
            //uses string textBoxText and textBox2Text to get user input for the password and the IV for decryption.
            //uses CheckPassword Form to close the specific Form
            //uses CheckPasswordBindingModel to set its properties.
            if (textBoxText != "" && textBox2Text != "")
            {
                await Task.Run(() => SetCheckPasswordBindingModel(textBoxText, textBox2Text, checkPasswordBindingModel));

                await Task.Run(() => SetCheckFileBindingModelOKIsPressed(checkPasswordBindingModel));

                CheckForm.Close();
            }
            else
            {
                await Task.Run(() => ShowMessageBox());
            }
        }