i want to make audio steganography with kriptography (aes). audio cover using wav and midi, i have no problem with wav but really confused with midi. most of code i get from
http://codeproject.com..here is the code :
CODE
using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace StegAudio
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonnamafile_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
GetFileName(dlg, textBoxHideFile, false);
}
private void buttontempatfile_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
GetFileName(dlg, textBoxDestFile, true);
}
private void GetFileName(FileDialog dialog, TextBox control, bool useFilter)
{
if (useFilter) { dialog.Filter = "Wave Audio (*.wav)|*.wav|MIDI (*.mid)|*.mid";}
if (dialog.ShowDialog(this) == DialogResult.OK)
{
control.Text = dialog.FileName;
}
}
private string GetFileExtension(string filename)
{
return filename.Substring(filename.Length - 3, 3);
}
private void buttoncariaudio_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
GetFileName(dlg, textBoxHideAudio, true);
}
private void buttonfileakhir_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
GetFileName(dlg, textBoxExtDest, false);
}
private void buttonsembunyi_Click(object sender, EventArgs e)
{
if(textBoxHideAudio.Text.Length == 0)
{
errorProvider1.SetError(textBoxHideAudio, "You forgot to choose a carrier file."); //errorProvider blom didefinisikan sebelumnya, jadi ngga bisa dipake
}
else if(textBoxPWH.Text.Length == 0)
{
errorProvider1.SetError(textBoxPWH, "You forgot to write a key.");
}
else if(textBoxDestFile.Text.Length == 0)
{
errorProvider1.SetError(textBoxDestFile, "The resulting carrier file must be saved somewhere.");
}
else if (textBoxHideFile.Text.Length == 0)
{
errorProvider1.SetError(textBoxHideFile, "What am I supposed to hide?");
}
else
{
//before we hide the message into the wav file, we must encrypt it first
//in this case, we are using Rijndael encryption
MyEncryptor encryptor = new MyEncryptor(textBoxPWH.Text); //we use the key password as a secret phase
String encryptedMessage = encryptor.Encrypt(GetMessageString());
//reset all the stream first
Stream sourceStream = null;
FileStream destinationStream = null;
WaveStream audioStream = null;
Stream messageStream = GetMessageStream(encryptedMessage); //write the encrypted message into the stream
Stream keyStream = GetKeyStream();
string fileExtension; //fileextension indicating the format of audio file as the carrier of the secret message
fileExtension = GetFileExtension(textBoxHideAudio.Text);
if (fileExtension.Equals("wav"))
{
try
{
//how many samples do we need
long countSampleRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length);
if (countSampleRequired > Int32.MaxValue)
{
throw new Exception("Message too long, or bad key! This message/key combination requires " + countSampleRequired + " samples, only " + Int32.MaxValue + " samples are allowed.");
}
//use wav file as the carrier
sourceStream = new FileStream(textBoxHideAudio.Text, FileMode.Open);
this.Cursor = Cursors.WaitCursor;
//create empty file for the carrier wave
destinationStream = new FileStream(textBoxDestFile.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 (countSampleRequired > 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: " + countSampleRequired;
throw new Exception(errorReport);
}
//hide 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;
MessageBox.Show("file Telah Sukses Disisipkan");
}
}
else if (fileExtension.Equals("mid"))
{
//i'll make it simple, coz the method is slightly different in code than wav. Another reason is that i'm lazy enough not to port the code.
try
{
this.Cursor = Cursors.WaitCursor;
MidiUtility utility = new MidiUtility();
utility.HideMessage(textBoxHideAudio.Text, textBoxDestFile.Text, 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(); } //actually, these two streams aren't needed in this method
if (sourceStream != null) { sourceStream.Close(); } //but let's write them anyway...:)
if (destinationStream != null) { destinationStream.Close(); }
}
}
}
}
private Stream GetMessageStream()
{
BinaryWriter messageWriter = new BinaryWriter(new MemoryStream());
//open the file to be hidden
FileStream fs = new FileStream(textBoxHideFile.Text, FileMode.Open);
int filelength = (int)fs.Length;
messageWriter.Write(filelength); //save the length of file into binary stream
byte[] buffer = new byte[fs.Length]; //allocate the buffer as big as the file length
fs.Read(buffer, 0, filelength); //read the file into the buffer
messageWriter.Write(buffer); //save the buffer into binary stream
fs.Close(); //close the file when operation finished
messageWriter.Seek(0, SeekOrigin.Begin);
return messageWriter.BaseStream;
}
private Stream GetMessageStream(string message)
{
BinaryWriter messageWriter = new BinaryWriter(new MemoryStream());
messageWriter.Write(message.Length);
messageWriter.Write(Encoding.ASCII.GetBytes(message));
messageWriter.Seek(0, SeekOrigin.Begin);
return messageWriter.BaseStream;
}
private String GetMessageString()
{
FileStream fs = new FileStream(textBoxHideFile.Text, FileMode.Open);
StreamReader rd = new StreamReader(fs);
return rd.ReadToEnd();
}
private Stream GetKeyStream()
{
BinaryWriter keyWriter = new BinaryWriter(new MemoryStream());
//write the password to the binary stream
keyWriter.Write(Encoding.ASCII.GetBytes(textBoxPWH.Text));
keyWriter.Seek(0, SeekOrigin.Begin);
return keyWriter.BaseStream;
}
private void buttonekstract_Click(object sender, EventArgs e)
{
if (textBoxHideAudio.Text.Length == 0)
{
errorProvider1.SetError(textBoxHideAudio, "You forgot to choose a carrier file.");
}
else if (textBoxPWH.Text.Length == 0)
{
errorProvider1.SetError(textBoxPWH, "You forgot to write a key.");
}
else
{
this.Cursor = Cursors.WaitCursor;
FileStream sourceStream = null;
WaveStream audioStream = null;
//create an empty stream to receive the extracted message
MemoryStream messageStream = new MemoryStream();
Stream keyStream = GetKeyStream();
string fileExtension; //fileextension indicating the format of audio file as the carrier of the secret message
fileExtension = GetFileExtension(textBoxHideAudio.Text);
if (fileExtension.Equals("wav"))
{
try
{
//open the carrier file
sourceStream = new FileStream(textBoxHideAudio.Text, FileMode.Open);
audioStream = new WaveStream(sourceStream);
WaveUtility utility = new WaveUtility(audioStream);
//extract the message from the carrier wave
utility.Extract(messageStream, keyStream);
messageStream.Seek(0, SeekOrigin.Begin);
//save result to a file
FileStream fs = new FileStream(textBoxExtDest.Text, FileMode.Create);
//before the result message is saved in the destination file, decrypt it first
MyEncryptor decryptor = new MyEncryptor(textBoxPWH.Text); //we use the key password as a secret phase
StreamReader rd = new StreamReader(messageStream);
String decryptedMessage = decryptor.Decrypt(rd.ReadToEnd());
StreamWriter wr = new StreamWriter(fs);
wr.Write(decryptedMessage);
wr.Flush();
fs.Close();
}
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;
MessageBox.Show("Ekstraksi Pesan Telah Selesai");
}
}
else if (fileExtension.Equals("mid"))
{
try
{
MidiUtility utility = new MidiUtility();
utility.ExtractMessage(textBoxHideAudio.Text, null, messageStream, keyStream);
messageStream.Seek(0, SeekOrigin.Begin);
//save result to a file
FileStream fs = new FileStream(textBoxExtDest.Text, FileMode.Create);
//before the result message is saved in the destination file, decrypt it first
MyEncryptor decryptor = new MyEncryptor(textBoxPWH.Text); //we use the key password as a secret phase
StreamReader rd = new StreamReader(messageStream);
String decryptedMessage = decryptor.Decrypt(rd.ReadToEnd());
StreamWriter wr = new StreamWriter(fs);
wr.Write(decryptedMessage);
wr.Flush();
fs.Close();
}
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;
MessageBox.Show("Ekstraksi Pesan Telah Selesai");
}
}
}
}
}
}
please reply my post as soon as posible.
not forget i'm adding attachment for whole program
once more, when i use midi as the cover, it says "invalid file format"...
thanks before