private void decryptButton_Click(object sender, EventArgs e) { string password = Interaction.InputBox("Enter password to decrypt file(s)", "Decrypt File(s)"); if (password == "") { return; } if (!this._db[this._id].checkPassphrase(password)) { MessageBox.Show("Wrong Password. Please re-enter your password.", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } List <string> successList = new List <string>(); List <string> failureList = new List <string>(); EventHandler decryptEvent = new EventHandler(delegate(object o, EventArgs a) { TreeNode node = (TreeNode)o; string path = node.FullPath; FileInfo fileInfo = new FileInfo(path); try { if (fileInfo.Extension != ".enc") { throw new Exception("Cannot decrypt because of wrong type of file."); } byte[] data = DataEncryption.decrypt(this._db[this._id], password, File.ReadAllBytes(path)); byte[] sig = data.Skip(1).Take(32).ToArray(); byte[] zipData = data.Skip(33).ToArray(); if (!SHA256.Create().ComputeHash(zipData).SequenceEqual(sig)) { throw new Exception("Cannot decrypt because of wrong key."); } if (data[0] == 0) { FileInfo newFile = new FileInfo(path.Substring(0, path.Length - fileInfo.Extension.Length)); if (newFile.Exists) { throw new Exception("Cannot decrypt the file because decryption file already exists."); } File.WriteAllBytes(newFile.FullName, zipData); node.Parent.Nodes.Add(newFile.Name); } else { DirectoryInfo newDir = new DirectoryInfo(path.Substring(0, path.Length - fileInfo.Extension.Length)); if (newDir.Exists) { throw new Exception("Cannot decrypt the file because decryption folder already exists."); } using (MemoryStream memStream = new MemoryStream(zipData)) using (ZipArchive archive = new ZipArchive(memStream)) archive.ExtractToDirectory(newDir.FullName); node.Parent.Nodes.Insert(0, new TreeNode(newDir.Name) { BackColor = Color.Yellow }); } successList.Add(path); } catch (Exception ex) { Console.WriteLine(ex.ToString()); failureList.Add(path); } }); doCheckedNodes(this.filesDirectoriesTreeView.Nodes, decryptEvent); string success = "Success: " + successList.Count.ToString() + " file(s)\n"; foreach (string s in successList) { success += s + "\n"; } string failure = "Failure: " + failureList.Count.ToString() + " file(s)\n"; foreach (string s in failureList) { failure += s + "\n"; } MessageBox.Show("Result:\n\n" + success + "\n" + failure, "Decrypt File(s)", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void encryptButton_Click(object sender, EventArgs e) { if (MessageBox.Show("Remember:\n" + "With Folder(s), Encryption Feature MUST compress it before encrypting.\n\n" + "Do you want to continue?", "Encrypt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } Account receiver = (Account)this.accountListBox.SelectedItem; List <string> successList = new List <string>(); List <string> failureList = new List <string>(); int mode = 0; mode += (this.compressCheckBox.Checked) ? 1 : 0; mode += (this.compressOneFileCheckBox.Checked) ? 1 : 0; EventHandler encryptEvent = null; switch (mode) { case 0: encryptEvent = new EventHandler(delegate(object o, EventArgs a) { TreeNode node = (TreeNode)o; string path = node.FullPath; try { if (File.Exists(path + ".enc")) { throw new Exception("Cannot encrypt because ENC file already exists."); } byte[] zipData = null; if (node.BackColor == Color.Yellow) { using (MemoryStream memStream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create)) compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive); zipData = memStream.ToArray(); } } else { zipData = File.ReadAllBytes(path); } byte[] data = new byte[zipData.Length + 33]; data[0] = (byte)((node.BackColor == Color.Yellow) ? 1 : 0); Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32); Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length); File.WriteAllBytes(path + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data)); successList.Add(path); node.Parent.Nodes.Add(node.Text + ".enc"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); failureList.Add(path); } }); doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent); break; case 1: encryptEvent = new EventHandler(delegate(object o, EventArgs a) { TreeNode node = (TreeNode)o; string path = node.FullPath; try { if (File.Exists(path + ".enc")) { throw new Exception("Cannot encrypt because ENC file already exists."); } byte[] zipData = null; if (node.BackColor == Color.Yellow) { using (MemoryStream memStream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create)) compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive); zipData = memStream.ToArray(); } } else { using (MemoryStream memStream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create)) { ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path)); using (BinaryWriter writer = new BinaryWriter(entry.Open())) writer.Write(File.ReadAllBytes(path)); } zipData = memStream.ToArray(); } } byte[] data = new byte[zipData.Length + 33]; data[0] = 1; Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32); Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length); File.WriteAllBytes(path + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data)); successList.Add(path); node.Parent.Nodes.Add(node.Text + ".enc"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); failureList.Add(path); } }); doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent); break; case 2: try { DirectoryInfo root = new DirectoryInfo(this.pathTextBox.Text); if (File.Exists(root.FullName + "\\" + root.Name + ".enc")) { throw new Exception("Cannot encrypt because ENC file already exists."); } byte[] zipData = null; using (MemoryStream memStream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create)) { encryptEvent = new EventHandler(delegate(object o, EventArgs a) { TreeNode node = (TreeNode)o; string path = node.FullPath; if (node.BackColor == Color.Yellow) { compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive); } else { ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path)); using (BinaryWriter writer = new BinaryWriter(entry.Open())) writer.Write(File.ReadAllBytes(path)); } }); doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent); } zipData = memStream.ToArray(); } byte[] data = new byte[zipData.Length + 33]; data[0] = 1; Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32); Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length); File.WriteAllBytes(root.FullName + "\\" + root.Name + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data)); this.filesDirectoriesTreeView.Nodes[0].Nodes.Add(root.Name + ".enc"); MessageBox.Show("Encrypt Success", "Encrypt", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; default: MessageBox.Show("Encrypt Failure", "Encrypt Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string success = "Success: " + successList.Count.ToString() + " file(s) or folder(s)\n"; foreach (string s in successList) { success += s + "\n"; } string failure = "Failure: " + failureList.Count.ToString() + " file(s) or folder(s)\n"; foreach (string s in failureList) { failure += s + "\n"; } MessageBox.Show("Result:\n\n" + success + "\n" + failure, "Encrypt", MessageBoxButtons.OK, MessageBoxIcon.Information); }