English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Preface: It's been a long time since I wrote a blog, and I've been very busy in the past year, with all kinds of work that can't be done. I believe many office workers will have this feeling. Recently, when performing NFC card writing operations, I need to calculate a check bit. Generally speaking, the check bit is usually obtained by performing an XOR operation on the previous few bytes.
Now I'll talk about the scenario I use first:
Take a16The data of the byte is written to the CPU card (such as a transportation card), and the last byte is a check code---The first fifteen bytes are XOR.
I started looking for some algorithms written by others on the Internet and found that the calculation results were not correct, or they were written too complexly, so I wrote one myself, which I felt was also relatively simple, and now I share it with everyone, hoping to communicate with each other.
First section: What is exclusive OR operation (mainly from Baidu Encyclopedia, those who are familiar with it can skip)
Definition:
Exclusive OR is also known as exclusive OR, or abbreviated as xor
Exclusive OR (xor) is a mathematical operator. It is applied to logical operations. The mathematical symbol for exclusive OR is “⊕”, and the computer symbol is “xor”. Its operation rule is:
a⊕b=(¬a∧b)∨(a∧¬b)
If the two values a and b are different, then the exclusive OR result is1. If the two values a and b are the same, the exclusive OR result is 0.
Exclusive OR is also called half-addition, and its operation rule is equivalent to binary addition without carry: in binary, using1represents true, 0 represents false, then the exclusive OR operation rule is: 0⊕0=0,1⊕0=1, 0⊕1=1,1⊕1=0 (both are 0, different is1These rules are the same as addition, but without carry.
Exclusive OR is abbreviated as XOR, EOR, or EX-OR
There are three operators in the program: XOR, xor, ⊕.
Usage as follows
z=x⊕y
z=xxory
Operation rules:
1.a⊕a=0
2.a⊕b=b⊕a
3.a⊕b⊕c=a⊕(b⊕c)=(a⊕b)⊕c;
4.d=a⊕b⊕c can be deduced a=d⊕b⊕c.
5.a⊕b⊕a=b.
6.If x is a binary number 0101, y is a binary number1011
Then x⊕y=1110
Only when the two compared bits are different, the result is1, otherwise the result is 0
That is, if the two inputs are the same, it is 0, otherwise it is1”!
Logic:
Logical expression: F=AB'⊕A'B((AB'⊕A'B)'=AB⊙A'B', ⊙ is the “equivalence” operation)
The truth table of XOR logic is shown in the figure1The
Shown, its logical symbol is as shown in the figure2Shown. The relationship of XOR logic is: when AB is different, the output P=1;When AB is the same, the output P=0.“⊕”is the XOR operation symbol, and the XOR logic is also a combination of AND, OR and NOT logic, its logical expression is:
P=A⊕B
From the figure1From which we can know, the rules of XOR operation are
0⊕0=0,0⊕1=1
1⊕0=1,1⊕1=0
Mnemonic: take 0 for the same, take1
In fact, XOR is defined as eitherone(isone),butnotboth in English, that is, only one is true (1)., take true (1).
Function:
It is widely used in computers, the logical symbol of XOR (xor) is generally used, and sometimes ⊕ is used:
True⊕False=true
False⊕True=true
False⊕False=false
True⊕True=false
Or as:
True⊕False=True
False⊕True=True
False⊕False=False
True⊕True=False
Some computer languages use1Meaning true, use 0 to represent false, so the two bytes are XORed bit by bit as follows
The following is the XOR calculation of two binary numbers:
In reality, we use decimal numbers, so let's see how two decimal numbers are performed XOR calculations:
5⊕2=?
1.Before performing XOR calculations, the values will be converted to binary:
5and2Convert to binary respectively: 0101,0010
2.Then take the result 0111Convert to decimal:7
3.Therefore5⊕2=7
Tricks:
Different from other languages, C language and C++The XOR in the language does not use xor, but uses “^”, and the key input method is Shift+6.(In other languages, the “^” usually represents exponentiation.)
If you need to swap the values of two variables, in addition to the usual method of using a temporary variable for exchange, you can also use XOR to exchange values using only two variables, such as:
a=a^b; b=b^a; a=a^b;
Explanation:
a1=a^b b=a1^b a=a1^b=a1^(a1^b)=a1^a1^b=b
Attention:
a=a^b^(b=a);//This kind of form is incorrect UB behavior, which may have different results in different compilers. Do not use it.
This completes the exchange of a and b.
In summary: XORing the same variable with the XOR value of another variable results in itself.
Use case: It can be used in one or more stages of an encryption algorithm to make the algorithm more complex, harder to crack, and more secure.[1]
第二节:Using Java language implementation:
private static String xor(String strHex_X,String strHex_Y){ // 16)); String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16)); String result = ""; //Determine whether it is8bit binary, otherwise fill in zeros to the left 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 numbers at the same position are the same, then fill in 0, otherwise fill in1 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 for the XOR operation between one byte of a hexadecimal string, such as the XOR operation of a fifteen-byte hexadecimal string:
1312f70f900168d900007df57b4884
Firstly, we need to split: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 one-byte checksum obtained is: e6
Supplementary, I have added a simple calling method for some friends for reference:
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]); } code = xorString(code, dateArr[i]); } } return code; }
Then call it in the main function or other methods:
String code = checkcode_0007("1312f70f900168d900007df57b4884");
code is the obtained checksum.
Summary
That's all about the code examples of Java programming for the XOR operation on hexadecimal strings in this article. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave comments for improvement. Thank you for your support to this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any copyright-infringing content, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)