Encrypt a file using XOR

2016.11.03

 

The reason I was looking for a job earlier, I found that I did not understand many knowledge and concepts deeply. Now I am watching the video of the Chuanzhi Podcast, and I feel that it is super good. Today I saw the use of XOR to encrypt files, and I am very interested. Let's take a look at how it is implemented.

 

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

public class IamageTest {

	public static void main(String[] args) throws Exception{
		// find the image file
		File inFile = new File("C:\\Users\\tuocheng\\Desktop\\tuocheng.jpg");
		File outFile = new File("e:\\encrypted image.jpg");
		
		//Create a data channel. Let the binary data of the picture flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		// While reading, XOR the read data with one data, and write the data out
		
		int content = 0;//This variable is used to store the read data
		while((content = input.read())!=-1){//If the end of the file is not read, then continue to read the data has been stored in the content variable
			output.write(content^12);
		}
		//close the resource
		output.close();
		input.close();
		
	}
}

    In this way, we look for our picture under the e disk, and after opening it is

 

   

    If we want to decrypt the file, we need to XOR 12 back, that is

    

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

public class IamageTest {

	public static void main(String[] args) throws Exception{
		// find the image file
		File inFile = new File("e:\\encrypted image.jpg");
		File outFile = new File("e:\\decrypted image.jpg");
		
		//Create a data channel. Let the binary data of the picture flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		// While reading, XOR the read data with one data, and write the data out
		
		int content = 0;//This variable is used to store the read data
		while((content = input.read())!=-1){//If the end of the file is not read, then continue to read the data has been stored in the content variable
			output.write(content^12);
		}
		//close the resource
		output.close();
		input.close();
	}
}

    As shown below:
    

     After opening:

      

      I'm going, handsome man.

      In addition, during this process, I found that the occupied memory has not changed. Another question is? Why can't I use QQ screenshots to paste directly to the Windows desktop, but I have to paste them to QQ first, I don't understand.

      After meeting this function, we can also encrypt our private information and prevent others from seeing it. .

 
 

 

    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326918266&siteId=291194637
XOR