Preface: I haven’t written a blog for a long time. I feel so busy in the past year, and I have endless work to do. I believe many office workers will feel this way. Recently, I have performed a card writing operation on NFC and need to calculate a check bit. Generally speaking, most of the check bits are obtained by XOR operation of the first few bytes.
Now let me talk about the scenarios I use:
Write a 16-byte data into the CPU card (such as a traffic card), and the last byte is the verification code--the first fifteen bytes XOR.
I started looking for some algorithms written by others on the Internet and found that the calculation result was wrong, or the writing was too complicated, so I wrote one myself, which felt that it was relatively simple. Now I will share it with you, I hope to communicate with you together.
Section 1: What is XOR operation (mainly excerpted from Baidu Encyclopedia, familiar children's boots can be skipped)
definition:
ExclusiveOR, English is exclusiveOR, or abbreviated as xor
XOR is a mathematical operator. It is applied to logical operations. The mathematical symbol of XOR is "", and the computer symbol is "xor". The algorithm is:
ab=(¬a∧b)∨(a∧¬b)
If the values a and b are not the same, the XOR result is 1. If the values a and b are the same, the XOR result is 0.
Exclusive OR is also called semi-addition operation. Its algorithm is equivalent to binary addition without carry: in binary, 1 is used to represent true and 0 is false, then the algorithm of exclusive OR is: 00=0, 10=1, 01=1, 11=0 (both are both 0, different is 1). These laws are the same as addition, but do not carry.
XOR is referred to as XOR, EOR, and EX-OR
There are three types of operators in the program: XOR, xor, and.
How to use it is as follows
z=xy
z=xxory
Operation rules:
1.aa=0
2.ab=ba
3.abc=a(bc)=(ab)c;
4.d=abc can deduce a=dbc.
5.aba=b.
6. If x is the binary number 0101, y is the binary number 1011
Then xy=1110
Only when the two comparison bits are different, the result is 1, otherwise the result is 0
That is, "When two inputs are the same, they are 0, and when they are different, they are 1"!
logic:
Logical expression: F=AB'A'B ((AB'A'B)'=AB⊙A'B', ⊙ is the "same or" operation)
The truth table of XOR logic is shown in Figure 1
The logical symbols are shown in Figure 2. The relationship between XOR logic is: when AB is different, output P=1; when AB is the same, output P=0. "" is an XOR operation symbol, and XOR logic is also a combination with or non-logical, and its logical expression is:
P=AB
From Figure 1, the rules of XOR operation are
00=0,01=1
10=1,11=0
Mental formula: 0 is the same, 1 is the same
In fact, XOR is defined in English as either (isone), but notboth, that is, when only one is true (1), take true (1).
effect:
It is commonly used in computers. The logical symbol of XOR is generally used with xor, which is also useful:
True or false = true
False true = true
False = false
True = false
Or:
TrueFalse=True
FalseTrue=True
FalseFalse=False
TrueTrue=False
Some computer languages use 1 to represent true and 0 to represent false, so the two bytes are bitwise exclusive or as follows
The following are two binary values for XOR calculation:
In reality, decimal values are used, so let’s take a look at how two decimal values are calculated for XOR:
52=?
1. Before performing XOR calculation, all the values will be converted into binary:
5 and 2 are converted into binary: 0101 and 0010 respectively
2. Convert the result 0111 to decimal: 7
3. So 52=7
Clever use:
Unlike other languages, XOR in C and C++ does not use xor, but "^", and the typing method is Shift+6. (And the "^" in other languages generally means multiply)
If you need to exchange the values of two variables, in addition to the commonly used borrowed intermediate variables for exchange, you can also use XOR and only use two variables for exchange, such as:
a=a^b;b=b^a;a=a^b;
Detailed explanation:
a1=a^bb=a1^ba=a1^b=a1^(a1^b)=a1^a1^b=b
Notice:
a=a^b^(b=a);//此类形式是不正确的UB行为,在不同编译器中会有不同的结果,切勿使用
This completes the exchange of a and b.
To sum up: the same variable and another variable and its XOR value are equal to itself.
Use case: It can be used in a certain link or more links of the encryption algorithm, making the algorithm more complex, less easy to be cracked, and has higher security. [1]
Section 2: Implementation in Java language:
private static String xor(String strHex_X,String strHex_Y){//Convert x and y into binary form String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));String result = "";//Determine whether it is 8-bit binary, otherwise the left zero is supplemented if(anotherBinary.length() != 8){for (int i = anotherBinary.length(); i <8; i++) {anotherBinary = "0"+anotherBinary;}}if(thisBinary.length() != 8){for (int i = thisBinary.length(); i <8; i++) {thisBinary = "0"+thisBinary;}}//XOR operation for (int i=0;i<anotherBinary.length();i++){//If the number of the same position is the same, add 0, otherwise add 1 if(thisBinary.charAt(i)==anotherBinary.charAt(i)) result+="0"; else{result+="1";}}Log.e("code",result);return Integer.toHexString(Integer.parseint(result, 2));}Note: The above method is an XOR operation between one byte of a hexadecimal string, such as an XOR operation for a fifteen-byte hexadecimal string:
1312f70f900168d900007df57b4884
Split first: 13 12 f7 0f 90 01 68 d9 00 00 7d f5 7b 48 84
13 xor 12-->1
1 xor f7-->f6
f6 xor 0f-->f9
....
62 xor 84-->e6
That is, the obtained one-byte verification code is: e6
In addition, I have added a simple call method to some friends for reference only:
public String checkcode_0007(String para){String[] dateArr = new String[15];try {dateArr[0] = para.substring(0, 2);dateArr[1] = para.substring(2, 4);dateArr[2] = para.substring(4, 6);dateArr[3] = para.substring(6, 8);dateArr[4] = para.substring(8, 10);dateArr[5] = para.substring(10, 12);dateArr[6] = para.substring(12, 14);dateArr[7] = para.substring(14, 16);dateArr[8] = para.substring(16, 18);dateArr[9] = para.substring(18, 20);dateArr[10] = para.substring(20, 22);dateArr[11] = para.substring(22, 24);dateArr[12] = para.substring(24, 26);dateArr[13] = para.substring(26, 28);dateArr[14] = para.substring(28, 30);}catch (Exception e) {// TODO: handle exception}String code = "";for (int i = 0; i < dateArr.length-1; i++) {if(i == 0){code = xorString(dateArr[i], dateArr[i+1]);} else{code = xorString(code, dateArr[i]);}}return code;}Then call it in the main function or other method:
String code = checkcode_0007("1312f70f900168d900007df57b4884");The code is the check code obtained.
Summarize
The above is the entire content of this article about Java programming implementation of the code example of Exclusive Or operation of hexadecimal strings. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!