XOR briefly introduces: XOR is a binary-based bit operation, represented by the symbol XOR or ^. Its algorithm is to use the same value to 0 and 1 for each binary bit of the number on both sides of the operator.
A simple understanding is to add without carrying, such as 1+1=0, 0+0=0, 1+0=1.
Requirement description
Encrypting data in the information age is a very important topic. During the process of doing the project, I also implemented a relatively complex encryption algorithm. However, since the technology involved is confidential, I implemented a relatively simple version here, using the file input and output streams and XOR operations to encrypt arbitrary files. Regarding the decryption algorithm, it is very simple and can be solved by thinking about it yourself.
Principles of mathematics
This encryption algorithm uses the function of two numbers XOR. Let’s briefly talk about the principle of XOR. XOR is actually an operation of binary encoding of a file. Simply put, when the two binary bits are the same, it is 0 and not 1 at the same time. See the following example:
//Binary representation of 7: 00000111//Binary representation of 2: 00000010//The result obtained by the XOR of both: 00000101 //The result obtained by the number 5//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code implementation
import java.io.*;class FileSecret{public static void main(String[] args) throws Exception {//Find the file to be encrypted, specify the drive letter yourself, and the input and output do not need to be in the same drive letter File inFile = new File("Drive letter://encrypted file");//Output the file to be encrypted to the specified drive letter File outFile = new File("Drive letter://decrypted file");//Create a data channel to allow the binary data of the picture to flow into FileInputStream input = new FileInputStream(inFile);FileOutputStream output = new FileOutputStream(outFile);//In the process of reading, the read data XOR is a number. This number should be generated by some kind of encryption algorithm. Here I just simply compile a number 928 (my birthday), then perform an exclusive OR, and output the obtained data int content = 0;//This variable is used to store the read data. Of course, longer data types such as long can be used here. Of course, we can also use other data types. Just satisfy the data types on both ends of ^ that can be converted to each other, at least it can be cast while ((content=input.read())!=-1) // If the end of the file is not reached, then continue to read the data, and the read data has been stored in the content variable. -1 is the ending character of the file {output.write(content^928);//Write to the output file stream}//Close the resource input.close();output.close();}}Code function evaluation
For this code, the function can basically meet the needs, but there are shortcomings. First, the encryption algorithm is not used to generate the other end of the XOR number, and second, I did not implement the decryption of the file. In fact, the decryption is very simple. Please read the mathematical principle part carefully and you can know how to write the decryption algorithm. In fact, encryption and decryption are not implemented at the same time in the same place, but encryption is calculated using the same encryption algorithm.
Improved algorithm using random numbers
In the above process, we actually use a given value to perform XOR with the binary file we read. So can we use a random number to replace this convention? The answer is yes. First of all, we use variables of type int to store them. The range that can be represented is: plus or minus 2.1 billion representable numbers. The specific code is as follows:
//Methods 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);//Indicate that a random number between 0-10 is generated. We should be able to save the generated random number for encryption and decryptors to use System.out.println("Random number is: "+num); }}Improved encryption algorithm
Encryption 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 letter yourself, and the input and output do not need to be in the same drive letter File inFile = new File("Driver://encrypted file");//Output the file to be encrypted to the specified drive letter File outFile = new File("Driver://decrypted file");//Create a data channel to allow the binary data of the picture to flow into FileInputStream input = new FileInputStream(inFile);FileOutputStream output = new FileOutputStream(outFile);//Another number that generates encrypted XOR Random random = new Random();int num = random.nextint(11);//Denotes that a random number between 0 and 10 is generated. We should be able to save the generated random number for encryption and decryptors to use System.out.println("random number is: "+num);//In the process of reading, the read data will be XOR a number, which should be generated by some kind of encryption algorithm. Here I simply compile a number 928 (my birthday), and then perform XOR, and output the obtained data int content = 0 ;//This variable is used to store the read data. Of course, longer data types such as long can be used here. Of course, we can also use other data types. We only need to satisfy the data types on both ends of ^ to convert each other, and at least cast type conversion can be performed while((content=input.read())!=-1) // If it is not reached the end of the file, then continue to read the data, and the read data has been stored in the content variable. -1 is the end character of the file {output.write(content^num);//Write to the output file stream}//Close the resource input.close();output.close();}}The encryption side needs to inform the decryption side of the num generated in the above code, otherwise the decryption of the file cannot be implemented.
Decryption code:
import java.io.*;class FileSecret{public static void main(String[] args) throws Exception {//Find the file to be encrypted, specify the drive letter yourself, and the input and output do not need to be in the same drive letter File inFile = new File("Drive letter://encrypted file");//Output the file to be encrypted to the specified drive letter File outFile = new File("Drive letter://decrypted file");//Create a data channel to allow the binary data of the picture to flow into FileInputStream input = new FileInputStream(inFile);FileOutputStream output = new FileOutputStream(outFile);//In the process of reading, the read data XOR is a number. This number should be generated by some kind of encryption algorithm. Here I just simply compile a number 928 (my birthday), then perform an exclusive OR, and output the obtained data int content = 0;//This variable is used to store the read data. Of course, longer data types such as long can be used here. Of course, we can also use other data types. Just satisfy the data types on both ends of ^ that can be converted to each other, at least it can be cast while ((content=input.read())!=-1) // If the end of the file is not reached, then continue to read the data, and the read data has been stored in the content variable. -1 is the end character of the file {output.write(content^ encrypted number transmitted from the encryption end);//Write to the output file stream}//Close the resource input.close();output.close();}}Improved again
In fact, in our code, the standard encryption code should be randomly generated and contains various symbols such as letters and numbers. So how do we generate such an encryption string? How do I convert such an encryption string into binary code after generating it? Provide an idea: Using Java regular expressions can generate any string you want, and then use the string conversion method to generate the corresponding binary code. I have implemented an extremely complex encryption generation method myself, but it cannot be disclosed. This involves the information of laboratory projects, and there are many fields of cryptography and many classic encryption algorithms that can also be used.
Summarize
The above is all the content of this article about the detailed explanation of the encryption principle and use of any file in Java XOR operation. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java uses XOR operation to implement simple encryption and decryption algorithm instance code
Java programming implementation code example of Exor OR operation on hexadecimal strings
XOR problem code analysis in java
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!