Beispiel #1
0
        public void AESEncryptFile(string filePath, EncryptFileBindingModel encryptBindingModel, DecryptFileBindingModel decryptBindingModel)
        {
            if (SaveZoneDbService.GetAllEntitiesWithName(filePath).Count == 0)
            {
                decryptBindingModel.EncryptedFiles.Add(encryptBindingModel.FileName);
                GenerateKey(encryptBindingModel, decryptBindingModel);
                GenerateIV(encryptBindingModel, decryptBindingModel);
                encryptBindingModel.FileSourcePath = filePath;

                EncryptFile(filePath);
                CheckUserInput(encryptBindingModel);

                IV       = "";
                password = "";
                encryptBindingModel.IsEncrypted = true;
                SaveZoneDbService.AddEncryptFileInfo(encryptBindingModel.FileName, encryptBindingModel.FileSourcePath, true);
            }
            else
            {
                encryptBindingModel.IsEncrypted = false;
                SaveZoneDbService.AddEncryptFileInfo(encryptBindingModel.FileName, encryptBindingModel.FileSourcePath, false);
                MessageBox.Show("File already encrypted!", "File Already Encrypted", MessageBoxButtons.OK, MessageBoxIcon.Error, 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);
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        public void AESEncryptFile(string filePath, EncryptFileBindingModel encryptBindingModel, DecryptFileBindingModel decryptBindingModel)
        {
            //Checks from the database if the file choosen is already encrypted. If it is stored in the database
            //as an encrypted file it says that it is already encrypted.
            if (SaveZoneDbService.GetAllEntitiesWithName(filePath).Count == 0)
            {
                //Adds to a list the file name of the file choosen.
                decryptBindingModel.EncryptedFiles.Add(encryptBindingModel.FileName);
                //Generates Key
                GenerateKey(encryptBindingModel, decryptBindingModel);
                //Generates IV
                GenerateIV(encryptBindingModel, decryptBindingModel);
                //Sets the source path to the binding model
                encryptBindingModel.FileSourcePath = filePath;

                //Encrypts the file choosen
                EncryptFile(filePath);
                //Checks if the user wants to save the password and the IV in a file.
                CheckUserInput(encryptBindingModel);

                //Resets the IV and password for next use
                IV       = "";
                password = "";
                encryptBindingModel.IsEncrypted = true;
                //Adds encrypted file to the database
                SaveZoneDbService.AddEncryptFileInfo(encryptBindingModel.FileName, encryptBindingModel.FileSourcePath, true);
            }
            else
            {
                encryptBindingModel.IsEncrypted = false;
                SaveZoneDbService.AddEncryptFileInfo(encryptBindingModel.FileName, encryptBindingModel.FileSourcePath, false);
                MessageBox.Show("File already encrypted!", "File Already Encrypted", MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
        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);
            }
        }
Beispiel #6
0
        //Checks if user want to save the Password and the IV anywhere.
        private void CheckUserInput(EncryptFileBindingModel encryptBindingModel)
        {
            var msgBox = MessageBox.Show($"File {encryptBindingModel.FileName} encrypted succesfully. \r\nPassword: {password} \r\nIV: {IV} \r\n\r\nDo you want to save the password and the IV in a new text file ?",
                                         "File Encrypted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, 0,
                                         MessageBoxOptions.DefaultDesktopOnly);

            //It is used if the user wants to save the password and the IV in a text file
            if (msgBox == DialogResult.Yes)
            {
                string        myDocPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\GNA\EncryptedFiles";
                DirectoryInfo di        = Directory.CreateDirectory(myDocPath);
                using (StreamWriter sw = new StreamWriter(Path.Combine(myDocPath, $"{encryptBindingModel.FileName + "-password"}.txt")))
                {
                    sw.WriteLine($"Password: {password}");
                    sw.WriteLine($"IV: {IV}");
                }
                SaveZoneDbService.AddEncryptFileEngine(encryptBindingModel.FileSourcePath, password, IV);
                MessageBox.Show($"Password and IV saved with the name {encryptBindingModel.FileName + " - password"}.txt on your startup folder of the program",
                                "Saved", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            //It is used when the user wants to copy the IV and the Password in the clipboard
            else
            {
                SaveZoneDbService.AddEncryptFileEngine(encryptBindingModel.FileSourcePath, password, IV);
                var saveMsgBox = MessageBox.Show("Do you want to copy password and the IV in the clipboard?",
                                                 "Copy to clipboard", MessageBoxButtons.YesNo, MessageBoxIcon.Question, 0,
                                                 MessageBoxOptions.DefaultDesktopOnly);

                if (saveMsgBox == DialogResult.Yes)
                {
                    { Clipboard.SetText($"Password: {password} \r\nIV: {IV}"); }
                    MessageBox.Show("Password and IV saved to clipboard!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 0,
                                    MessageBoxOptions.DefaultDesktopOnly);
                }
                else
                {
                    MessageBox.Show("Password and IV not saved", "Not saved", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 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);
            }
        }