English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Principle and Detailed Explanation of XOR Operation for Java to Encrypt Any File

Brief introduction to XOR: XOR is a binary bit operation based on binary, represented by the symbol XOR or ^, whose operation rules are that for each binary bit of the numbers on both sides of the operator, if the values are the same, 0 is taken, and if they are different,1.

A simple understanding is that it is a non-carry addition, such as1+1=0,0+0=0,1+0=0=1.

Requirement description

In the information age, data encryption is an important topic. During the project, I also implemented a relatively complex encryption algorithm, but due to the confidentiality of the technology involved, I implement a relatively simple version here, using the file input/output stream and XOR operation to encrypt any file. As for the decryption algorithm, it is very simple, just think about it.

Mathematical principles

This encryption algorithm utilizes the XOR function of two numbers. To put it simply, the principle of XOR is actually to operate on the binary encoding of the file. Simply put, it is that when two binary bits are the same, they are 0, and when they are different, they are1Let's see the following example:

//7The binary representation of:
00000111
//2The binary representation of:
00000010
//The result of the XOR operation between the two:
00000101 //That is to say, the number5
//---------------------------
//The result of the XOR operation is then again XORed with2XOR
//5The binary representation of:
00000101
//2The binary representation of:
00000010
//The result of the XOR operation between the two:
00000111 //That is to say7It's amazing that it has returned to7isn't it?

Code implementation

import java.io.*;
class FileSecret
{
	public static void main(String[] args) throws Exception
	 {
		//Find the file to be encrypted, specify the drive yourself, and the input and output do not need to be on the same drive
		File inFile = new File("Drive:\\Encrypted file");
		//The file to be encrypted is output to the specified drive
		File outFile = new File("Drive:\\Decrypted file");
		//Establish a data channel to allow the binary data of the image to flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		//During the reading process, the data read is XORed with a number, which should be generated by some encryption algorithm, here I just simply write a number928(My birthday), then perform an XOR operation, and output the data obtained
		int content = 0 ;
		//This variable is used to store the data read, of course, here we can use longer data types such as long, of course, we can also use other data types, as long as the data types at both ends can be converted to each other, at least they can be forced to convert to the same type
		while((content=input.read())!=})-1) // If it has not reached the end of the file, continue to read data, and the data read has been stored in the content variable.-1End of file symbol
		{
			output.write(content^928);
			//Write to the output file stream
		}
		//Close resources
		input.close();
		output.close();
	}
}

Code functionality evaluation

For this piece of code, the functionality is basically able to meet the requirements, but there are some deficiencies. First, it does not use an encryption algorithm to generate the other end of the XOR number. Second, I did not implement the decryption of the file, in fact, decryption is very simple. Please read the mathematical principles part carefully to know how to write the decryption algorithm. In fact, encryption and decryption are not implemented at the same place at the same time, but rather encryption is double implemented using the same encryption algorithm for operation.

Improving the algorithm with random numbers

In the above process, we actually use a given value to perform an XOR operation with the binary file we read in. Can we replace this convention with a random number? The answer is yes. First, we use an int type variable to store it, so the range that can be represented is: positive and negative21The number of digits that can be represented by billions, the specific code is as follows:

//Method to generate random numbers
import java.util.*;
public class RandomTest{
 public static void main(String[] args){
  Random random = new Random();
  int num = random.nextInt(11);//represents 0-10Random number between, the random number generated should be saved, for the use of the encryptor and decryptor
  System.out.println("Random number: "+num);
 }
}

Improved encryption algorithm

Encryption end code:

import java.io.*;
import java.util.*;
class FileSecret
{
	public static void main(String[] args) throws Exception
	 {
		//Find the file to be encrypted, specify the drive yourself, and the input and output do not need to be on the same drive
		File inFile = new File("Drive:\\Encrypted file");
		//The file to be encrypted is output to the specified drive
		File outFile = new File("Drive:\\Decrypted file");
		//Establish a data channel to allow the binary data of the image to flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		//Generate another number for encryption XOR
		Random random = new Random();
		int num = random.nextInt(11);
		//represents 0-10Random number between, the random number generated should be saved, for the use of the encryptor and decryptor
		System.out.println("Random number: "+num);
		//During the reading process, the data read is XORed with a number, which should be generated by some encryption algorithm, here I just simply write a number928(My birthday), then perform an XOR operation, and output the data obtained
		int content = 0 ;
		//This variable is used to store the data read, of course, here we can use longer data types such as long, of course, we can also use other data types, as long as the data types at both ends can be converted to each other, at least they can be forced to convert to the same type
		while((content=input.read())!=})-1) // If it has not reached the end of the file, continue to read data, and the data read has been stored in the content variable.-1End of file symbol
		{
			output.write(content^num);
			//Write to the output file stream
		}
		//Close resources
		input.close();
		output.close();
	}
}

The encryption end needs to inform the decryption end of the num generated in the above code, otherwise, the decryption of the file cannot be achieved.

Decryption end code:

import java.io.*;
class FileSecret
{
	public static void main(String[] args) throws Exception
	 {
		//Find the file to be encrypted, specify the drive yourself, and the input and output do not need to be on the same drive
		File inFile = new File("Drive:\\Encrypted file");
		//The file to be encrypted is output to the specified drive
		File outFile = new File("Drive:\\Decrypted file");
		//Establish a data channel to allow the binary data of the image to flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		//During the reading process, the data read is XORed with a number, which should be generated by some encryption algorithm, here I just simply write a number928(My birthday), then perform an XOR operation, and output the data obtained
		int content = 0 ;
		//This variable is used to store the data read, of course, here we can use longer data types such as long, of course, we can also use other data types, as long as the data types at both ends can be converted to each other, at least they can be forced to convert to the same type
		while((content=input.read())!=})-1) // If it has not reached the end of the file, continue to read data, and the data read has been stored in the content variable.-1End of file symbol
		{
			output.write(content ^ encrypted number received from the encryption end);
			//Write to the output file stream
		}
		//Close resources
		input.close();
		output.close();
	}
}

Further Improvement

In fact, in our code, the standard password should be randomly generated and contain letters, numbers, and various symbols. So how do we generate such an encryption string? After generating such an encryption string, how do we convert it into binary code? Provide an idea: you can generate any string you want using Java's regular expression, and then generate the corresponding binary code using the string conversion method. I have implemented an extremely complex encryption generation method, but it cannot be disclosed as it involves the materials of the laboratory project, and there are many classic encryption algorithms in the field of cryptography that can also be used.

Summary

That is all about the principle and detailed explanation of file encryption using XOR technique in Java provided in this article. Hope it is helpful to everyone. Those who are interested can continue to refer to this site:

Java Implementation of Simple Encryption and Decryption Algorithm Using XOR Operation Example Code

Code Example of Java Programming Implementation of XOR Operation on Hexadecimal String

Code Analysis of XOR Problem in Java

If there are any shortcomings, please leave a message to point them out. Thank you friends for your support of this site!

Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content artificially, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like