private void btnExtract_Click(object sender, System.EventArgs e) { //extract the message from the carrier wave if (txtSrcFile.Text.Length == 0) { errorProvider.SetError(txtSrcFile, "You forgot to choose a carrier file."); } else if (txtKeyFile.Text.Length == 0) { errorProvider.SetError(txtKeyFile, "You forgot to choose a key file."); } else { this.Cursor = Cursors.WaitCursor; FileStream sourceStream = null; WaveStream audioStream = null; //create an empty stream to receive the extracted message MemoryStream messageStream = new MemoryStream(); //open the key file Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { //open the carrier file sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); audioStream = new WaveStream(sourceStream); WaveUtility utility = new WaveUtility(audioStream); //exctract the message from the carrier wave utility.Extract(messageStream, keyStream); messageStream.Seek(0, SeekOrigin.Begin); if (rdoMessageDstFile.Checked) //save result to a file { FileStream fs = new FileStream(txtMessageDstFile.Text, FileMode.Create); byte[] buffer = new byte[messageStream.Length]; messageStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); fs.Close(); } else //display result { txtExtractedMessage.Text = new StreamReader(messageStream).ReadToEnd(); } } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally{ if (keyStream != null) { keyStream.Close(); } if (messageStream != null) { messageStream.Close(); } if (audioStream != null) { audioStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } this.Cursor = Cursors.Default; } } }
private void btnHide_Click(object sender, System.EventArgs e) { //hide the message inside the carrier wave if (rdoSrcFile.Checked && (txtSrcFile.Text.Length == 0)) { errorProvider.SetError(txtSrcFile, "You forgot to choose a carrier file."); } else if (txtKeyFile.Text.Length == 0) { errorProvider.SetError(txtKeyFile, "You forgot to choose a key file."); } else if (txtDstFile.Text.Length == 0) { errorProvider.SetError(txtDstFile, "The resulting carrier file must be saved somewhere."); } else if (rdoMsgFile.Checked && (txtMsgFile.Text.Length == 0)) { errorProvider.SetError(txtMsgFile, "What am I supposed to hide?"); } else if (rdoMsgText.Checked && (txtMessage.Text.Length == 0)) { errorProvider.SetError(txtMessage, "What am I supposed to hide?"); } else { Stream sourceStream = null; FileStream destinationStream = null; WaveStream audioStream = null; //create a stream that contains the message, preceeded by its length Stream messageStream = GetMessageStream(); //open the key file Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { //how man samples do we need? long countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length); if (countSamplesRequired > Int32.MaxValue) { throw new Exception("Message too long, or bad key! This message/key combination requires " + countSamplesRequired + " samples, only " + Int32.MaxValue + " samples are allowed."); } if (rdoSrcFile.Checked) //use a .wav file as the carrier { sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); } else //record a carrier wave { frmRecorder recorder = new frmRecorder(countSamplesRequired); recorder.ShowDialog(this); sourceStream = recorder.RecordedStream; } this.Cursor = Cursors.WaitCursor; //create an empty file for the carrier wave destinationStream = new FileStream(txtDstFile.Text, FileMode.Create); //copy the carrier file's header audioStream = new WaveStream(sourceStream, destinationStream); if (audioStream.Length <= 0) { throw new Exception("Invalid WAV file"); } //are there enough samples in the carrier wave? if (countSamplesRequired > audioStream.CountSamples) { String errorReport = "The carrier file is too small for this message and key!\r\n" + "Samples available: " + audioStream.CountSamples + "\r\n" + "Samples needed: " + countSamplesRequired; throw new Exception(errorReport); } //hide the message WaveUtility utility = new WaveUtility(audioStream, destinationStream); utility.Hide(messageStream, keyStream); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally{ if (keyStream != null) { keyStream.Close(); } if (messageStream != null) { messageStream.Close(); } if (audioStream != null) { audioStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } if (destinationStream != null) { destinationStream.Close(); } this.Cursor = Cursors.Default; } } }
private void btnExtract_Click(object sender, System.EventArgs e) { if(txtSrcFile.Text.Length == 0){ errorProvider.SetError(txtSrcFile, "You forgot to choose a carrier file."); } else if(txtKeyFile.Text.Length == 0){ errorProvider.SetError(txtKeyFile, "You forgot to choose a key file."); } else{ this.Cursor = Cursors.WaitCursor; FileStream sourceStream = null; WaveStream audioStream = null; MemoryStream messageStream = new MemoryStream(); Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); audioStream = new WaveStream(sourceStream); WaveUtility utility = new WaveUtility(audioStream); utility.Extract(messageStream, keyStream); messageStream.Seek(0, SeekOrigin.Begin); if(rdoMessageDstFile.Checked){ //save result to a file FileStream fs = new FileStream(txtMessageDstFile.Text, FileMode.Create); byte[] buffer = new byte[messageStream.Length]; messageStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); fs.Close(); } else { txtExtractedMessage.Text = new StreamReader(messageStream).ReadToEnd(); } } catch(Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally{ if(keyStream != null){ keyStream.Close(); } if(messageStream != null){ messageStream.Close(); } if(audioStream != null){ audioStream.Close(); } if(sourceStream != null){ sourceStream.Close(); } this.Cursor = Cursors.Default; } } }
private void btnHide_Click(object sender, System.EventArgs e) { if (rdoSrcFile.Checked && (txtSrcFile.Text.Length == 0)) { errorProvider.SetError(txtSrcFile, "You forgot to choose a carrier file."); } else if (txtKeyFile.Text.Length == 0) { errorProvider.SetError(txtKeyFile, "You forgot to choose a key file."); } else if (txtDstFile.Text.Length == 0) { errorProvider.SetError(txtDstFile, "The resulting carrier file must be saved somewhere."); } else if (rdoMsgFile.Checked && (txtMsgFile.Text.Length == 0)) { errorProvider.SetError(txtMsgFile, "What am I supposed to hide?"); } else if (rdoMsgText.Checked && (txtMessage.Text.Length == 0)) { errorProvider.SetError(txtMessage, "What am I supposed to hide?"); } else { MessageBox.Show("alert"); Stream sourceStream = null; FileStream destinationStream = null; WaveStream audioStream = null; Stream messageStream = GetMessageStream(); Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { long countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length); if (countSamplesRequired > Int32.MaxValue) { throw new Exception("Message too long, or bad key! This message/key combination requires " + countSamplesRequired + " samples, only " + Int32.MaxValue + " samples are allowed."); } if (rdoSrcFile.Checked) { sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); } else { } this.Cursor = Cursors.WaitCursor; destinationStream = new FileStream(txtDstFile.Text, FileMode.Create); audioStream = new WaveStream(sourceStream, destinationStream); if (audioStream.Length <= 0) { throw new Exception("Invalid WAV file"); } if (countSamplesRequired > audioStream.CountSamples) { String errorReport = "The carrier file is too small for this message and key!\r\n" + "Samples available: " + audioStream.CountSamples + "\r\n" + "Samples needed: " + countSamplesRequired; throw new Exception(errorReport); } WaveUtility utility = new WaveUtility(audioStream, destinationStream); utility.Hide(messageStream, keyStream); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally{ if (keyStream != null) { keyStream.Close(); } if (messageStream != null) { messageStream.Close(); } if (audioStream != null) { audioStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } if (destinationStream != null) { destinationStream.Close(); } this.Cursor = Cursors.Default; } } }
private void btnHide_Click(object sender, System.EventArgs e) { if(rdoSrcFile.Checked && (txtSrcFile.Text.Length == 0)){ errorProvider.SetError(txtSrcFile, "You forgot to choose a carrier file."); } else if(txtKeyFile.Text.Length == 0){ errorProvider.SetError(txtKeyFile, "You forgot to choose a key file."); } else if(txtDstFile.Text.Length == 0){ errorProvider.SetError(txtDstFile, "The resulting carrier file must be saved somewhere."); } else if(rdoMsgFile.Checked && (txtMsgFile.Text.Length == 0)){ errorProvider.SetError(txtMsgFile, "What am I supposed to hide?"); } else if(rdoMsgText.Checked && (txtMessage.Text.Length == 0)){ errorProvider.SetError(txtMessage, "What am I supposed to hide?"); } else{ MessageBox.Show("alert"); Stream sourceStream = null; FileStream destinationStream = null; WaveStream audioStream = null; Stream messageStream = GetMessageStream(); Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { long countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length); if(countSamplesRequired > Int32.MaxValue){ throw new Exception("Message too long, or bad key! This message/key combination requires "+countSamplesRequired+" samples, only "+Int32.MaxValue+" samples are allowed."); } if(rdoSrcFile.Checked) { sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); } else { } this.Cursor = Cursors.WaitCursor; destinationStream = new FileStream(txtDstFile.Text, FileMode.Create); audioStream = new WaveStream(sourceStream, destinationStream); if (audioStream.Length <= 0){ throw new Exception("Invalid WAV file"); } if(countSamplesRequired > audioStream.CountSamples){ String errorReport = "The carrier file is too small for this message and key!\r\n" + "Samples available: " + audioStream.CountSamples + "\r\n" + "Samples needed: " + countSamplesRequired; throw new Exception(errorReport); } WaveUtility utility = new WaveUtility(audioStream, destinationStream); utility.Hide(messageStream, keyStream); } catch(Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally{ if(keyStream != null){ keyStream.Close(); } if(messageStream != null){ messageStream.Close(); } if(audioStream != null){ audioStream.Close(); } if(sourceStream != null){ sourceStream.Close(); } if(destinationStream != null){ destinationStream.Close(); } this.Cursor = Cursors.Default; } } }