private static void en_de_crypt(en_de_cryption_mode en_de_cryption_mode_current, string file_path_from, string file_path_to, bool delete_file_path_from, bool interactive) { Form form = new Form(); Label password_1_label = new Label(), password_2_label = new Label(); int label_width = 150, label_height = 20, textbox_width = 200, textbox_height = 20, button_width = 80, button_height = 40; Color label_forecolor = Color.LightGreen, password_backcolor = Color.Black, password_forecolor = Color.LightGreen, proceed_backcolor = Color.Black, proceed_forecolor = Color.LightGreen, cancel_backcolor = Color.Black, cancel_forecolor = Color.LightGreen, error_forecolor = Color.Red; ContentAlignment label_text_alignment = ContentAlignment.MiddleRight, button_text_alignment = ContentAlignment.MiddleCenter; TextBox password_1 = new TextBox(), password_2 = new TextBox(); char password_character = '*'; BorderStyle password_border_style = BorderStyle.Fixed3D; Font label_font = new Font(FontFamily.GenericMonospace, (float)10.0), password_font = new Font(FontFamily.GenericMonospace, (float)10.0), button_font = new Font(FontFamily.GenericMonospace, (float)10.0); Button proceed = new Button(), cancel = new Button(); form.StartPosition = FormStartPosition.CenterParent; form.Width = 400; form.Height = 180; form.BackColor = Color.Black; form.Text = "Please, provide the password for the " + (en_de_cryption_mode_current == en_de_cryption_mode.encrypt ? "encryption" : "decryption") + "."; password_1_label.Bounds = new Rectangle(10, 20, label_width, label_height); password_1_label.Font = label_font; password_1_label.ForeColor = label_forecolor; password_1_label.TextAlign = label_text_alignment; password_1_label.Text = "Password:"******"Confirm password:"******"OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(cancel, new object[] { null }); if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) { if (!string.IsNullOrEmpty(password_1.Text)) { password_2.Enabled = true; } else { proceed.Enabled = false; password_2.Text = ""; if (password_2.Enabled) password_2.Enabled = false; } } else { if (!string.IsNullOrEmpty(password_1.Text)) proceed.Enabled = true; else proceed.Enabled = false; } if ((Keys)e.KeyValue == Keys.Enter && !string.IsNullOrEmpty(password_1.Text)) { if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) password_2.Focus(); else proceed.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(proceed, new object[] { new KeyEventArgs(Keys.Enter) }); } }; password_2.Bounds = new Rectangle(160, 50, textbox_width, textbox_height); password_2.BackColor = password_backcolor; password_2.ForeColor = password_forecolor; password_2.BorderStyle = password_border_style; password_2.PasswordChar = password_character; password_2.Font = password_font; password_2.Multiline = false; password_2.Enabled = false; if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) password_2.KeyUp += (sender, e) => { if ((Keys)e.KeyValue == Keys.Escape) cancel.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(cancel, new object[] { null }); if (string.IsNullOrEmpty(password_2.Text) || password_1.Text.Equals(password_2.Text)) { if (!string.IsNullOrEmpty(password_2.Text)) proceed.Enabled = true; password_2.ForeColor = password_forecolor; if ((Keys)e.KeyValue == Keys.Enter && !string.IsNullOrEmpty(password_2.Text)) proceed.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(proceed, new object[] { new KeyEventArgs(Keys.Enter) }); } else { proceed.Enabled = false; password_2.ForeColor = error_forecolor; } }; proceed.Bounds = new Rectangle(290, 100, button_width, button_height); proceed.BackColor = proceed_backcolor; proceed.ForeColor = proceed_forecolor; proceed.Font = button_font; proceed.TextAlign = button_text_alignment; proceed.Text = "Proceed"; proceed.Enabled = false; proceed.Click += (sender, e) => { form.Close(); byte[] key_string_bytes = Encoding.UTF8.GetBytes(password_1.Text), key = new byte[64]; password_1.Text = ""; password_2.Text = ""; FileStream file_from = null, file_to = null; try { WhirlpoolDigest whirlpool_digest = new WhirlpoolDigest(); whirlpool_digest.BlockUpdate(key_string_bytes, 0, key_string_bytes.Length); whirlpool_digest.DoFinal(key, 0); SerpentEngine serpent_engine = new SerpentEngine(); Pkcs7Padding pkcs7_padding = new Pkcs7Padding(); pkcs7_padding.Init(new SecureRandom(key)); PaddedBufferedBlockCipher padded_buffered_block_cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(serpent_engine), pkcs7_padding); byte[] iv = new byte[padded_buffered_block_cipher.GetBlockSize()]; Array.Copy(key, key.Length - iv.Length, iv, 0, iv.Length); padded_buffered_block_cipher.Init((en_de_cryption_mode_current == en_de_cryption_mode.encrypt), new ParametersWithIV(new KeyParameter(key, 31, 32), iv)); int plaintext_buffer_size = 1024 * padded_buffered_block_cipher.GetBlockSize(), ciphertext_buffer_size = padded_buffered_block_cipher.GetBlockSize() + plaintext_buffer_size, bytes_read_count, bytes_en_de_crypted_count; byte[] buffer_from, buffer_to; if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) { buffer_from = new byte[plaintext_buffer_size]; buffer_to = new byte[ciphertext_buffer_size]; } else { buffer_from = new byte[ciphertext_buffer_size]; buffer_to = new byte[plaintext_buffer_size]; } file_from = new FileStream(file_path_from, FileMode.Open); file_to = new FileStream(file_path_to, FileMode.Create); while (true) { bytes_read_count = file_from.Read(buffer_from, 0, buffer_from.Length); if (bytes_read_count == 0) { break; } bytes_en_de_crypted_count = padded_buffered_block_cipher.DoFinal(buffer_from, 0, bytes_read_count, buffer_to, 0); file_to.Write(buffer_to, 0, bytes_en_de_crypted_count); } file_from.Close(); file_to.Close(); if (delete_file_path_from) File.Delete(file_path_from); for (int i = 0; i < key_string_bytes.Length; i += 1) key_string_bytes[i] = 0; for (int i = 0; i < key.Length; i += 1) key[i] = 0; whirlpool_digest.Reset(); serpent_engine.Reset(); pkcs7_padding = null; padded_buffered_block_cipher.Reset(); for (int i = 0; i < iv.Length; i += 1) iv[i] = 0; for (int i = 0; i < buffer_from.Length; i += 1) buffer_from[i] = 0; for (int i = 0; i < buffer_to.Length; i += 1) buffer_to[i] = 0; } catch(Exception exception) { string message; if (exception.Message.Contains("pad block corrupted")) { if (file_from != null) { file_from.Close(); } if (file_to != null) { file_to.Close(); } try { File.Delete(file_path_to); } catch(Exception) {} message = "A wrong decryption key has been provided."; } else { message = exception.Message; } alert(message, interactive); } }; cancel.Bounds = new Rectangle(200, 100, button_width, button_height); cancel.BackColor = cancel_backcolor; cancel.ForeColor = cancel_forecolor; cancel.Font = button_font; cancel.TextAlign = button_text_alignment; cancel.Text = "Cancel"; cancel.Click += (sender, e) => { form.Close(); password_1.Text = ""; password_2.Text = ""; }; form.Controls.Add(password_1_label); form.Controls.Add(password_2_label); form.Controls.Add(password_1); form.Controls.Add(password_2); form.Controls.Add(proceed); form.Controls.Add(cancel); form.ShowDialog(); }