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); } }
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 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(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); } }
private void CheckIfFileIsDecrypted(DecryptFileBindingModel decryptFileBindingModel) { if (decryptFileBindingModel.IsDecrypted == true) { sourcePath = decryptFileBindingModel.FileSourcePath; decryptedFile = $"File {decryptFileBindingModel.FileName} decrypted succesfully"; } else { sourcePath = ""; decryptedFile = ""; } }
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 CheckIfFileIsDecrypted(DecryptFileBindingModel decryptFileBindingModel) { //if a file is decrypted it is shown to the user. Else it is null. if (decryptFileBindingModel.IsDecrypted == true) { sourcePath = decryptFileBindingModel.FileSourcePath; decryptedFile = $"File {decryptFileBindingModel.FileName} decrypted succesfully"; } else { sourcePath = ""; decryptedFile = ""; } }
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) { 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); } }
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); } }
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); } }