当前位置: 代码迷 >> 综合 >> VB.NET实现DESFire ISO14443A CRC16
  详细解决方案

VB.NET实现DESFire ISO14443A CRC16

热度:6   发布时间:2023-12-15 11:21:14.0
    Public Shared Function CRC16_ISO14443A(ByVal InputBytes() As Byte) As Byte()If InputBytes Is Nothing OrElse InputBytes.Length < 1 ThenReturn NothingEnd IfDim CRC As UShort = &H6363US  'ISO14443A的CRC16,寄存器初始值为0xC6C6,逆序为0x6363。最后的US表示数据类型为UShortDim Polynomial As UShort = &H8408US  'ISO14443A的CRC16,多项式为X16+X12+X5+1(1 0001 0000 0010 0001),即0x1021,逆序为0x8408Dim i, j As IntegerDim uCRC(1) As ByteFor i = 0 To InputBytes.Length - 1CRC = CRC Xor InputBytes(i)For j = 0 To 7If (CRC And 1) ThenCRC = (CRC >> 1) Xor PolynomialElseCRC = CRC >> 1End IfNextNext'CRC = CUShort((CRC << 8) Xor ((CRC >> 8) And &HFF))'DESFire在APDU中使用的CRC,低字节在前。而BitConverter转换后也是低字节在前,因此无需刻意转换字节顺序。uCRC = BitConverter.GetBytes(CRC)Return uCRCEnd Function

DESFire使用ISO14443A的CRC16算法(也称为CRC_A算法、CRC16_A算法)。

基本参数是:

宽度:16位(16 bits);

多项式:X16+X12+X5+1(0x1021,逆序0x8408);

寄存器初始值:0xC6C6(逆序0x6363);

输出异或(XOR):0x0000;

输入输出reflect。

测试:

输入:0x00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

输出:0x77 F5