Faruk Naim

Faruk Naim

  • NA
  • 7
  • 391

I have Change some wav byte and rewrite it now it's wav is corrupted

Apr 13 2022 11:16 AM
using System.Media;
using System.Text;

namespace Testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
                //Take input string
                string str = textBox2.Text;
                char[] cha = str.ToCharArray();
                MessageBox.Show(cha[2].ToString());
                //Convert char to binary
                string[] bin1 = new string[cha.Length];
                for (int s = 0; s < cha.Length; s++)
                {
                    bin1[s] = Convert.ToString(cha[s], 2);
                }
                MessageBox.Show(bin1[3]);
                // Convert bin to byte
                byte[] abar = new byte[bin1.Length];
                for (int p = 0; p < bin1.Length; p++)
                {
                    abar[p] = Convert.ToByte(bin1[p], 2);
                }
                // Show message
                char[] chars = System.Text.Encoding.UTF8.GetString(abar).ToCharArray();

                string msge = string.Empty;
                for (int q = 0; q < chars.Length; q++)
                {
                    msge += Convert.ToString(chars[q]);
                }
                //Making binary to char array
                char[] bin2 = string.Join(string.Empty, bin1).ToCharArray();
                MessageBox.Show(bin2[9].ToString());
                MessageBox.Show(bin1[3]);
                MessageBox.Show(abar[3].ToString());
                MessageBox.Show(chars[3].ToString());
                textBox3.Text = msge;
                msge = msge.Remove(msge.Length - 1);
                MessageBox.Show(msge);
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Wav File|*.wav";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    ofd.Filter = "Wav File|*.wav";

                    byte[] data = File.ReadAllBytes(ofd.FileName);
                    MessageBox.Show(data[6].ToString());
                    var path = "stego14.wav";
                    //audio byte to binary
                    string[] bina = new string[data.Length];
                    for (int i = 0; i < data.Length; i++)
                    {
                        bina[i] = Convert.ToString(data[i], 2);
                    }
                    //removing the last char of binary
                    string[] nbina = new string[bin2.Length];
                    for (int h = 0; h < bin2.Length; h++)
                    {
                        nbina[h] = bina[h].Remove(bina[h].Length - 1);
                    }
                    //adding message bytes in last bit audio binary
                    string[] nbina1 = new string[bin2.Length];
                    for (int w = 0; w < nbina1.Length; w++)
                    {
                        string d = string.Empty;
                        d = nbina[w];
                        d = d + bin2[w];
                        nbina1[w] = d;
                    }
                    byte[] au = new byte[nbina1.Length];
                    for (int g = 0; g < nbina1.Length; g++)
                    {
                        au[g] = Convert.ToByte(nbina1[g], 2);
                    }
                    //replacing array element data byte array
                    for (int y = 0; y < au.Length; y++)
                    {
                        data[y] = au[y];
                    }
                    File.WriteAllBytes(path, data);
                    MessageBox.Show("Data written");
                }
            }
        }
    }
}

Here is my code. Here the wav file first converted to byte then the byte is converted in binary then the binarys last bit is replaced by the char binary bit. Now i can get a new wav file byt it's not playable. Please help me.