private void btnDecrypt_Click(object sender, EventArgs e) { if (tbInputKey.Text.Trim().Length < 8) { MessageBox.Show("Minimum secret passphrase length is 8 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { lblStatus.Text = "Processing..."; var passphrase = tbInputKey.Text.Trim(); // Retrieve the Id block as string var retrievedIdBlockString = Encoding.ASCII.GetString(ByteCipher.Decrypt(stegoFileBytes.Skip(stegoFileBytes.Length - 112).ToArray(), passphrase)).Split('|'); if (retrievedIdBlockString.Length != 3) { throw new Exception("File is corrupted or invalid"); } var retrievedHash = retrievedIdBlockString[0]; var hiddenFileType = retrievedIdBlockString[1].ToLower(); var keyindex = Convert.ToInt32(retrievedIdBlockString[2]); var retrievedCipherBytesWithIdBlock = stegoFileBytes.Skip(keyindex).ToArray(); var retrievedCipherBytes = retrievedCipherBytesWithIdBlock.Take(retrievedCipherBytesWithIdBlock.Length - 112).ToArray(); // Attempt to decrypt var retrievedMessageBytes = ByteCipher.Decrypt(retrievedCipherBytes, passphrase); if (retrievedHash != HashGenerator.GetHash(retrievedMessageBytes)) { throw new Exception("File has been modified"); } // Save the retrieved message file saveExtractedFileDialog.Filter = String.Format("{0} file (*.{1})|*.{1}", hiddenFileType.ToUpper(), hiddenFileType); DialogResult saveExtractedFileDialogResult = saveExtractedFileDialog.ShowDialog(); if (saveExtractedFileDialogResult == DialogResult.OK) { File.WriteAllBytes(saveExtractedFileDialog.FileName, retrievedMessageBytes); DialogResult successDialog = MessageBox.Show("File extracted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); if (successDialog == DialogResult.OK && cbOpenRecoveredFile.Checked) { Process.Start(saveExtractedFileDialog.FileName); } } }catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Reset the form visual lblStatus.Text = "Ready"; lblFileNameStego.Text = "No file chosen"; lblStegoFileSize.Text = lblStegoFileType.Text = "-"; btnDecrypt.Enabled = false; } } }
private void btnEncrypt_Click(object sender, EventArgs e) { if (tbGeneratedKey.Text.Trim().Length < 8) { MessageBox.Show("Minimum secret passphrase length is 8 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { lblStatus.Text = "Processing..."; saveStegoFileDialog.Filter = String.Format("{1} file (*.{0})|*.{0}", coverFileType.ToLower(), coverFileType); DialogResult saveStegoFileDialogResult = saveStegoFileDialog.ShowDialog(); if (saveStegoFileDialogResult == DialogResult.OK) { try { //Stopwatch sw = Stopwatch.StartNew(); // Stopwatch for benchmarking var cipherBytes = ByteCipher.Encrypt(messageFileBytes, tbGeneratedKey.Text.Trim()); //sw.Stop(); // Stop the sw and write the elapsed time //Debug.WriteLine("Total encryption time: "+ sw.ElapsedMilliseconds + " ms"); var cipherBytesWithIdBlock = cipherBytes.Concat(ByteCipher.Encrypt(generatedIdBlock, tbGeneratedKey.Text.Trim())).ToArray(); //sw.Restart(); // Restart the stopwatch File.WriteAllBytes(saveStegoFileDialog.FileName, coverFileBytes.Concat(cipherBytesWithIdBlock).ToArray()); //sw.Stop(); //Debug.WriteLine("Total hiding time: " + sw.ElapsedMilliseconds + " ms"); DialogResult successDialog = MessageBox.Show("File encrypted and hid successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); if (successDialog == DialogResult.OK && cbOpenStegoFile.Checked) { Process.Start(saveStegoFileDialog.FileName); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Reset form visual lblStatus.Text = "Ready"; lblFileNameCover.Text = lblFileNameMessage.Text = "No file chosen"; lblCoverFileSize.Text = lblCoverFileType.Text = lblMessageFileSize.Text = lblMessageFileType.Text = "-"; coverFileExists = messageFileExists = false; btnEncrypt.Enabled = false; } } else { lblStatus.Text = "Ready"; } } }