-4

I am reading a data from .wav file and then converting it into binary format. and then I write those binaries and create a new .wav file. I want that after getting binary format of .wav file I should do little modifications in its LSB's and then write the file from those modified bits.

How should i implement this? I am not getting any way. Please help me as I want to perform stenography through audio file.

import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    public class FiletoArrayofBytes

    {

        public static void main( String[] args )
        {
            FileInputStream fileInputStream=null;
            FileOutputStream fop = null;

            File file = new File("C:\\file.wav");

            byte[] bFile = new byte[(int) file.length()];

            try {
                File fileo = new File("c:/newfile.wav");
        fop = new FileOutputStream(fileo);
            // if file doesnt exists, then create it
                if (!fileo.exists()) {
                    fileo.createNewFile();
                }
                //convert file into array of bytes
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();

            for (int i = 0; i < bFile.length; i++) {
                System.out.println(Integer.toBinaryString(0x100 + (bFile[i])).substring(1));
                  //String  a =(Integer.toBinaryString(0x100 + (bFile[i])).substring(1));
                  int a=bFile[i];
                  fop.write(a);
                  System.out.println("\t i am a: " +a);
                }
                fop.flush();
                fop.close();
            System.out.println("Done");

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What's the question? Do you need code refactored or do you need help with implementation? – Chris Leyva Feb 05 '13 at 14:05
  • i need help in its implementation after getting binary bits i want to modify 1byte's lsb by my other data which will also be in binary and then i want to write the new file with those modified bits i want a piece of code which should be embedded in this code – Maira Alvi Feb 05 '13 at 14:11
  • @joergl What on Earth possessed you to remove the steganography tag? – Andrew Thompson Feb 05 '13 at 14:34
  • @Andrew Thompson: The fact that the question is mainly about reading and writing wavs. Imho, the fact that the OP wants to read/write wav's in order to do steganography is of minor relevance. At least that's my impression when I look at the code. You are free to correct me of course. – joergl Feb 05 '13 at 15:26
  • I would add "homework" tag as well :) – Ostap Andrusiv Feb 05 '13 at 15:54
  • @OstapAndrusiv The [tag:homework] has been retired. – Andrew Thompson Feb 05 '13 at 16:05

1 Answers1

1

WAV-files have at least a header. You can't just read/modify/write it byte-by-byte.

I would use some sort of Java-WAV library. For instance this one: Java File IO

WavFile class is really nice and useful. They do have nice read/modify/write examples.

Using that you can implement LSB (...google helped me finding this link...).

Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
  • please tell me a way that i should store, the bits which i am displaying on console in an array and do some modification there and then write a file which would be newfile.wav – Maira Alvi Feb 05 '13 at 15:10
  • At least have a look at the examples provided at the [Java File IO site](http://www.labbookpages.co.uk/audio/javaWavFiles.html). They explain everything. – Ostap Andrusiv Feb 05 '13 at 15:56
  • i read that and tried to compile and run that code but it gives me one error class WavFile is missing etc. please help me i shall be thankful to those who are expert in these domains. instead of making fun of others questions as one person is doing not doing homework etc i have tried now i am stucked so i need help please help me – Maira Alvi Feb 05 '13 at 17:19
  • 1. Download [WavFile sources](http://www.labbookpages.co.uk/audio/files/javaWavFiles/WavFile.tar.gz). 2. Unpack and add them to classpath. 3. Try "Reading Wav Files" example. 4. Inside the do-while lopp there's the for-loop: inside, you can apply your LSB logic to every frame inside the ```buffer[]```. – Ostap Andrusiv Feb 05 '13 at 19:30
  • i am receiving run time error as java.lang.ArrayIndexOutOfBoundsException: 0 – Maira Alvi Feb 06 '13 at 07:12