CRC-16和CRC-32检查

Gab*_*ves 0 c# crc32 crc16

我需要帮助尝试验证CRC-16值(还需要CRC-32值的帮助).我试着坐下来了解CRC是如何工作的,但我正在画一个空白.

我的第一个问题是当尝试使用在线计算器计算消息" BD001325E032091B94C412AC"到CRC16 = 12AC时.文档说明最后两个八位字节是CRC16值,所以我输入" BD001325E032091B94C4"到网站http://www.lammertbies.nl/comm/info/crc-calculation.html并收到5A90作为结果而不是12AC .

有谁知道为什么这些值不同,我在哪里可以找到如何计算CRC16和CRC32值的代码(我打算稍后学习如何做到这一点,但现在时间不允许)?

还有一些消息如下:

16000040FFFFFFFF00015FCB  
3C00003144010405E57022C7  
BA00001144010101B970F0ED  
3900010101390401B3049FF1  
09900C800000000000008CF3  
8590000000000000000035F7  
00900259025902590259EBC9  
0200002B00080191014BF5A2  
BB0000BEE0014401B970E51E  
3D000322D0320A2510A263A0  
2C0001440000D60000D65E54
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

我已经包含了更多信息.我引用的文档是TIA-102.BAAA-A(来自TIA标准).以下是文档中所述的内容(尽量避免侵犯版权):

分组中的最后一个块包括几个八位字节的用户信息和/或填充八位字节,然后是4个八位字节的CRC奇偶校验.这被称为分组CRC.

分组CRC是在中间块中包括的所有数据八位字节和最后块的用户信息的八位字节上编码的4字节循环冗余校验.具体计算如下.

设k是要计算分组CRC的用户信息和填充比特的总数.将k个消息比特视为k-1次多项式M(x)的系数,将第零个消息八位字节的MSB与x ^ k-1和最后一个消息八位字节的LSB与x ^ 0相关联.定义生成多项式GM(x)和反演多项式IM(x).

GM(x)= x ^ 32 + x ^ 26 + x ^ 23 + x ^ 22 + x ^ 16 + x ^ 12 + x ^ 11 + x ^ 10 + x ^ 8 + x ^ 7 + x ^ 5 + x ^ 4 + x ^ 2 + x + 1

IM(x)= x ^ 31 + x ^ 30 + x ^ 29 + ... + x ^ 2 + x +1

然后,根据以下公式计算分组CRC多项式FM(x).

FM(x)=(x ^ 32 M(x)mod GM(x))+ IM(x)模2,即GF(2)

FM(x)的系数被放置在CRC字段中,其中CRC的第0个八位字节的MSB对应于x ^ 31,并且CRC的第三个八位字节的LSB对应于x ^ 0.

在上面的引用中,我已经^展示了权力,因为格式在引用时不会保持不变.我不确定是什么,但这有帮助吗?

小智 5

我有一个我从互联网上发现的C++转换的类,它使用long来计算CRC32.它符合标准,是PKZIP,WinZip和以太网的用途.要测试它,使用Winzip并压缩文件然后使用此类计算相同的文件,它应该返回相同的CRC.它对我有用.

public class CRC32
{
    private int[] iTable;

    public CRC32() {
       this.iTable = new int[256];
       Init();
    }

    /**
     * Initialize the iTable aplying the polynomial used by PKZIP, WINZIP and Ethernet.
     */
    private void Init()
    {
       // 0x04C11DB7 is the official polynomial used by PKZip, WinZip and Ethernet.
       int iPolynomial = 0x04C11DB7;

       // 256 values representing ASCII character codes.
       for (int iAscii = 0; iAscii <= 0xFF; iAscii++)
       {
          this.iTable[iAscii] = this.Reflect(iAscii, (byte) 8) << 24;

          for (int i = 0; i <= 7; i++)
          {
             if ((this.iTable[iAscii] & 0x80000000L) == 0) this.iTable[iAscii] = (this.iTable[iAscii] << 1) ^ 0;
             else this.iTable[iAscii] = (this.iTable[iAscii] << 1) ^ iPolynomial;
          }
          this.iTable[iAscii] = this.Reflect(this.iTable[iAscii], (byte) 32);
       }
    }

    /**
     * Reflection is a requirement for the official CRC-32 standard. Note that you can create CRC without it,
     * but it won't conform to the standard.
     *
     * @param iReflect
     *           value to apply the reflection
     * @param iValue
     * @return the calculated value
     */
    private int Reflect(int iReflect, int iValue)
    {
       int iReturned = 0;
       // Swap bit 0 for bit 7, bit 1 For bit 6, etc....
       for (int i = 1; i < (iValue + 1); i++)
       {
          if ((iReflect & 1) != 0)
          {
             iReturned |= (1 << (iValue - i));
          }
          iReflect >>= 1;
       }
       return iReturned;
    }

    /**
     * PartialCRC caculates the CRC32 by looping through each byte in sData
     *
     * @param lCRC
     *           the variable to hold the CRC. It must have been initialize.
     *           <p>
     *           See fullCRC for an example
     *           </p>
     * @param sData
     *           array of byte to calculate the CRC
     * @param iDataLength
     *           the length of the data
     * @return the new caculated CRC
     */
    public long CalculateCRC(long lCRC, byte[] sData, int iDataLength)
    {
       for (int i = 0; i < iDataLength; i++)
       {
          lCRC = (lCRC >> 8) ^ (long) (this.iTable[(int) (lCRC & 0xFF) ^ (int) (sData[i] & 0xff)] & 0xffffffffL);
       }
       return lCRC;
    }

    /**
     * Caculates the CRC32 for the given Data
     *
     * @param sData
     *           the data to calculate the CRC
     * @param iDataLength
     *           then length of the data
     * @return the calculated CRC32
     */
    public long FullCRC(byte[] sData, int iDataLength)
    {
       long lCRC = 0xffffffffL;
       lCRC = this.CalculateCRC(lCRC, sData, iDataLength);
       return (lCRC /*& 0xffffffffL)*/^ 0xffffffffL);
    }

    /**
     * Calculates the CRC32 of a file
     *
     * @param sFileName
     *           The complete file path
     * @param context
     *           The context to open the files.
     * @return the calculated CRC32 or -1 if an error occurs (file not found).
     */
    long FileCRC(String sFileName, Context context)
    {
          long iOutCRC = 0xffffffffL; // Initilaize the CRC.

          int iBytesRead = 0;
          int buffSize = 32 * 1024;
          FileInputStream isFile = null;
          try
          {
             byte[] data = new byte[buffSize]; // buffer de 32Kb
             isFile = context.openFileInput(sFileName);
             try
             {
                while ((iBytesRead = isFile.read(data, 0, buffSize)) > 0)
                {
                   iOutCRC = this.CalculateCRC(iOutCRC, data, iBytesRead);
                }
                return (iOutCRC ^ 0xffffffffL); // Finalize the CRC.
             }
             catch (Exception e)
             {
                // Error reading file
             }
             finally
             {
                isFile.close();
             }
          }
          catch (Exception e)
          {
             // file not found
          }
          return -1l;
       }
 }
Run Code Online (Sandbox Code Playgroud)