几种卡片校验算法

一、卡片分类

  • M1卡:普通IC卡,0扇区不可以修改,其他扇区可反复擦写。
  • UID卡:普通复制卡,可以重复擦写所有扇区,主要应用在IC卡复制上,遇到带有防火墙的读卡器就会失效。
  • CUID卡:可擦写防屏蔽卡,可以重复擦写所有扇区,UID卡复制无效的情况下使用,可以绕过防火墙。
  • FUID卡:不可擦写防屏蔽卡,此卡的特点0扇区只能写入一次,写入一次变成M1卡,CUID复制没用的情况下使用,可以绕过防火墙。
  • UFUID卡:高级复制卡,我们就理解为是UID和FUID的合成卡,需要封卡操作,不封卡就是UID卡封卡后就变为M1卡。

二、一些校验算法

1.异或校验(XOR)

异或运算是一种二进制运算,其规则如下:

  • 如果两个比特相同,则结果为 0。
  • 如果两个比特不同,则结果为 1。

在异或校验中,数据的每个比特与一个预定的校验位进行异或运算,结果作为校验位。

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void XorCalculator(String Hex) {
String[] hexValues = Hex.split("6A 44 26 00 96 53 00 00 6E 6C 02 00 91 00");
int result = 0;
for (String hexValue : hexValues) {
try {
int decimalValue = Integer.parseInt(hexValue, 16);
result ^= decimalValue;
} catch (NumberFormatException e) {
System.err.println("Invalid hex value: " + hexValue);
}
}
System.out.println("XOR result: " + Integer.toHexString(result).toUpperCase());
}
//输出结果:XOR result: 5C

2.和校验

校验和是通过对数据中所有字节进行简单的加法或异或操作生成的值。其实就是直接加起来。示例代码如3

3.和取反校验

加起来然后每个比特位取反

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void HexSumAndBinaryInvertConverter(String Hex) {
String[] hexValues = Hex.split("6A 44 26 00 96 53 00 00 6E 6C 02 00 91 00");
int sum = 0;
for (String hexValue : hexValues) {
try {
int decimalValue = Integer.parseInt(hexValue, 16);
sum += decimalValue;
} catch (NumberFormatException e) {
System.err.println("Invalid hex value: " + hexValue);
}
}
String binarySum = Integer.toBinaryString(sum);
String invertedBinarySum = invertBinary(binarySum);
String hexResult = Integer.toHexString(Integer.parseInt(invertedBinarySum, 2)).toUpperCase();

System.out.println("Sum: " + sum);
System.out.println("Binary: " + binarySum);
System.out.println("Inverted Binary: " + invertedBinarySum);
System.out.println("Hex: " + hexResult);
}
//输出结果:Sum: 810
// Binary: 1100101010
// Inverted Binary: 0011010101
// Hex: D5

4.奇偶校验(Parity Check)

奇偶校验是一种简单的校验方法,用于确保数据中的1的数量是奇数或偶数。可以是奇校验(保证奇数个1)或偶校验(保证偶数个1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static String addParityBit(String data) {
int countOnes = 0;
// 计算数据中1的数量
for (char bit : data.toCharArray()) {
if (bit == '1') {
countOnes++;
}
}
// 决定奇偶校验位
char parityBit = (countOnes % 2 == 0) ? '0' : '1';

// 返回添加了奇偶校验位的数据
return data + parityBit;
}

5.循环冗余检验(Cyclic Redundancy Check,CRC)

CRC 是一种广泛用于网络通信和存储系统中的强大的差错检测算法。它通过除法运算实现,使用一个预定的生成多项式,将其附加到数据末尾,并在接收端进行类似的计算来验证数据的完整性。

6.海明码(Hamming Code)

海明码是一种纠错码,可以检测和纠正单一比特的错误。它通过在数据中添加冗余比特来实现,以提供对错误的纠正能力。