timmi
timmi

Reputation: 11

How can I read a wav file and replace bits in it?

I've just started learning Java and I'm working on an audio steganography project in Java (i.e. hiding a text file in a wav file).

For that I store a text and wav file in two files. Now I want to read them in bytes and replace random bits of the wav file with text file bits.

How can i read the files in bytes? (I've tried using InputStream). Is it possible to replace random bits in wav file with bits of text file? Are there any functions for it? Once I've read the files (both text and wav file), how can I edit (and replace) the bytes of wav file?

This is the the First screen, when user hits on HIDE button, the next screen opens up.

public class OptionScreen extends javax.swing.JFrame {

    /** Creates new form OptionScreen */
    public OptionScreen() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        hide = new javax.swing.JButton();
        unhide = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        hide.setText("Hide");
        hide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hideActionPerformed(evt);
            }
        });

        unhide.setText("UnHide");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(hide, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94)
                .addComponent(unhide, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(81, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(109, 109, 109)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hide, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(unhide, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(101, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void hideActionPerformed(java.awt.event.ActionEvent evt) {                                     
        second s=new second();
        s.setVisible(true);
    }                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OptionScreen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton hide;
    private javax.swing.JButton unhide;
    // End of variables declaration                   
}


import java.io.DataInputStream;
import java.io.File;
import java.io.File.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

public class second extends javax.swing.JFrame {
private File afile;     // put at bottom of class
private File tfile;
    /** Creates new form second */
    public second() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        text = new javax.swing.JButton();
        audio = new javax.swing.JButton();
        tname = new javax.swing.JLabel();
        aname = new javax.swing.JLabel();
        begin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        text.setText("TEXT FILE");
        text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textActionPerformed(evt);
            }
        });

        audio.setText("AUDIO FILE");
        audio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                audioActionPerformed(evt);
            }
        });

        begin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        begin.setText("BEGIN");
        begin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                beginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(text, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(audio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(aname, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(270, 270, 270)
                        .addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(text)
                    .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(audio)
                    .addComponent(aname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void textActionPerformed(java.awt.event.ActionEvent evt) {                                     
        JFileChooser opentext=new JFileChooser();
        opentext.showOpenDialog(this);
        int returnVal = opentext.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        tfile = opentext.getSelectedFile();
        String tfname=tfile.getName();
        tname.setText(tfname);
        // ... code that loads the contents of the file in the text area

    } 

    }                                    

    private void audioActionPerformed(java.awt.event.ActionEvent evt) {                                      
        JFileChooser openaudio=new JFileChooser();
        openaudio.showOpenDialog(this);
        int returnVal = openaudio.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        afile = openaudio.getSelectedFile();
        aname.setText(afile.getName());
        // ... code that loads the contents of the file in the text area
    } 
    }                                     

    private void beginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        try {
            InputStream fint = new FileInputStream(tfile);

            byte []bt=new byte[(int)afile.length()];
            InputStream fina = new FileInputStream(afile);
            int offset = 0;
            int numRead = 0;
            //
            try {

                System.out.println(new DataInputStream(fint).readByte());
            } catch (IOException ex) {
                Logger.getLogger(second.class.getName()).log(Level.SEVERE, null, ex);
            }
            //
            System.out.println(bt);


//////////////////////////////////////////////////////////////////

            FileOutputStream outs=new FileOutputStream(new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Music\\Secret.wav"));
        } catch (FileNotFoundException ex) {

        }

        //end of try catch
    }                                     

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new second().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel aname;
    private javax.swing.JButton audio;
    private javax.swing.JButton begin;
    private javax.swing.JButton text;
    private javax.swing.JLabel tname;
    // End of variables declaration                   
}

Upvotes: 1

Views: 1061

Answers (1)

AlexR
AlexR

Reputation: 115378

You have 2 ways: (1) read your file into byte array, than manipulate it in memory and then write it back to disk. (2) use RandomAccessFile that allows you manipulating file directly on disk.

Upvotes: 1

Related Questions