private void encryptFile(Object key) { try { string filePath = txtFileToEncrypt.Text; FileStream f = new FileStream(filePath, FileMode.Open); // Encrypt file fileCrypto fc = new fileCrypto(); Byte[] bytFile = new Byte[f.Length]; f.Read(bytFile, 0, bytFile.Length); byte[] cipherBytes = fc.encrypt(bytFile, key.ToString(), 128); // Create the encrypted file FileStream outStream = File.Create(filePath + ".crypted"); outStream.Write(cipherBytes, 0, cipherBytes.Length); outStream.Close(); f.Close(); MessageBox.Show("Done!", "File Encryption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show("An error occurred during the file encryption.", "Oups!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void decryptFile(Object key) { try { string filePath = txtFileToDecrypt.Text; FileStream f = new FileStream(filePath, FileMode.Open); // Encrypt file fileCrypto fc = new fileCrypto(); Byte[] bytFile = new Byte[f.Length]; f.Read(bytFile, 0, bytFile.Length); byte[] clearBytes = fc.decrypt(bytFile, key.ToString(), 128); // Create the encrypted file string newFilePath = filePath.Replace(".crypted", ""); string ext = Path.GetExtension(newFilePath); FileStream outStream = File.Create(newFilePath.Replace(ext, ".decrypted" + ext)); outStream.Write(clearBytes, 0, clearBytes.Length); outStream.Close(); f.Close(); MessageBox.Show("Done!", "File Decryption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex){ MessageBox.Show("An error occurred during the file decryption. Please, be sure to have the right key.", "Oups!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }