Search code examples
javaandroidfilexorrandomaccessfile

XOR operation on first 256 bytes of an video file


I am trying to implement xor operation on the first 256 bytes of video file which creats an output video file, I want that output video to stop working, then when the same output video is passed as input, another video file should be created which should behave as original one. Basically I am trying to encrypt and decrypt it.

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.apache.commons.io.FileUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView text;
    private String workingPath;
    File yourFile;
    File outputFile;
    Button decryptButton;
    byte[] buffer;
    RandomAccessFile raf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      workingPath = Environment.getExternalStorageDirectory()+"";
        outputFile = new File(workingPath, "result4.mp4");
        yourFile = new File(workingPath, "result5.mp4");
      buffer  = new byte[256];

        try {
            raf = new RandomAccessFile(yourFile,"rw");
            raf.read(buffer);
            try {
                Log.d("pos1",raf.getFilePointer()+"");
            } catch (IOException e) {
                e.printStackTrace();
            }
         //   FileUtils.writeByteArrayToFile(new File(workingPath+"/new.mp4"), buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

       byte[] outbyte = xor(buffer);
        text = (TextView)findViewById(R.id.textView);

        try {
            raf.seek(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Log.d("pos2",raf.getFilePointer()+"");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            raf.write(outbyte);
            byte[] arr = new byte[(int) raf.length()];
            raf.read(arr);

           FileOutputStream fileOuputStream =
                    new FileOutputStream(outputFile);
            fileOuputStream.write(arr);
            fileOuputStream.close();
            //raf.readFully(o);
            // outputFile = raf;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public static byte[] xor(byte[] a) {
        int key=11;
        byte[] result = new byte[a.length];

        for (int i = 0; i < a.length; i++) {
                    a[i] = (byte)(a[i] ^ key);  // we need to cast it back into a byte because it was converted to an int by XOR
                }

        return result;
    }

}

In the above code, the original input video(yourFile) also stops working.I don't know why


Solution

  • You're obviously changing yourFile by raf.write(outbyte); which will render the video unplayable. This will write to the original file.

    What you actually should do instead is write all the bytes of yourFile to outputFile with the first 256 modified.

    Check out this example.

    import java.io.*;
    public class XORTest
    {
        public static void main(String args[])throws IOException
        {
            RandomAccessFile reader=new RandomAccessFile("a.txt","r");
            RandomAccessFile writer=new RandomAccessFile("b.txt","rw");
            byte[] buffer  = new byte[256];
    
            reader.read(buffer);
            xor(buffer);
            writer.write(buffer);
            while(true)
            {
                int o = reader.read(buffer);
                if(o < 0)
                    break;
                writer.write(buffer);
    
            }
            writer.close();
            reader.close();
        }
    
        public static byte[] xor(byte[] a) {
            int key=11;
            byte[] result = new byte[a.length];
    
            for (int i = 0; i < a.length; i++) {
                        a[i] = (byte)(a[i] ^ key);                     }
    
            return result;
        }
    }