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 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 GenerateIV(EncryptFileBindingModel encryptBindingModel, DecryptFileBindingModel decryptBindingModel) { for (int i = 0; i < 16; i++) { IV += letters[random.Next(0, 68)]; } encryptBindingModel.IV = IV; decryptBindingModel.IV = IV; }
private void GenerateKey(EncryptFileBindingModel encryptBindingModel, DecryptFileBindingModel decryptBindingModel) { for (int i = 0; i < 16; i++) { password += letters[random.Next(0, 68)]; } encryptBindingModel.Pasword = password; decryptBindingModel.Password = password; }
private void CheckUserInput(EncryptFileBindingModel encryptFileBindingModel) { if (result == DialogResult.OK) { fileName = encryptFileDialog.FileName; encryptFileBindingModel.FileName = Path.GetFileName(fileName); encryptFileSource = Path.GetFullPath(fileName); encryptorFile.AESEncryptFile(encryptFileSource, encryptFileBindingModel, decryptFileBindingModel); CheckIfEncrypted(encryptFileBindingModel); } }
private void CheckIfEncrypted(EncryptFileBindingModel encryptFileBindingModel) { if (encryptFileBindingModel.IsEncrypted == true) { encryptedFile = $"File {encryptFileBindingModel.FileName} was encrypted successfully"; sourcePath = encryptFileSource; } else { encryptedFile = ""; sourcePath = ""; } }
private void CheckIfEncrypted(EncryptFileBindingModel encryptFileBindingModel) { //if a file is encrypted it is shown to the user. Else it is null. if (encryptFileBindingModel.IsEncrypted == true) { encryptedFile = $"File {encryptFileBindingModel.FileName} was encrypted successfully"; sourcePath = encryptFileSource; } else { encryptedFile = ""; sourcePath = ""; } }
private void CheckUserInput(EncryptFileBindingModel encryptFileBindingModel) { //Checks if user has pressed OK for saving the source directory if (result == DialogResult.OK) { //Saves the file name fileName = encryptFileDialog.FileName; //Sets the file name of the binding model encryptFileBindingModel.FileName = Path.GetFileName(fileName); //Sets the source path encryptFileSource = Path.GetFullPath(fileName); //Tries to encrypt the file encryptorFile.AESEncryptFile(encryptFileSource, encryptFileBindingModel, decryptFileBindingModel); //Checks if the method has encrypted the file CheckIfEncrypted(encryptFileBindingModel); } }
//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); } } }