对视频文件的前256个字节进行XOR运算

时间:2016-06-24 11:18:57

标签: java android file xor randomaccessfile

我正在尝试在创建输出视频文件的视频文件的前256个字节上实现xor操作, 我希望输出视频停止工作,然后当相同的输出视频作为输入传递时,应创建另一个视频文件,该文件应该像原始视频一样。基本上我正在尝试加密和解密它。

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;
    }

}

在上面的代码中,原始输入视频(yourFile)也停止工作。我不知道为什么

1 个答案:

答案 0 :(得分:1)

您显然正在通过yourFile更改raf.write(outbyte);,这会使视频无法播放。这将写入原始文件。

实际应该做的是将yourFile的所有字节写入outputFile,并修改前256个字节。

查看此示例。

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;
    }
}