C# CRC-16和CRC-32检查
我需要帮助尝试验证CRC-16值(也需要帮助CRC-32值)。我试着坐下来理解CRC是如何工作的,但我还是一片空白 我的第一个问题是,试图使用在线计算器将消息“C# CRC-16和CRC-32检查,c#,crc32,crc16,C#,Crc32,Crc16,我需要帮助尝试验证CRC-16值(也需要帮助CRC-32值)。我试着坐下来理解CRC是如何工作的,但我还是一片空白 我的第一个问题是,试图使用在线计算器将消息“BD001325E032091B94C412AC”计算到CRC16=12AC中时。文档说明最后两个八位字节是CRC16值,因此我将“BD001325E032091B94C4”输入站点,并接收5A90作为结果,而不是12AC 有人知道为什么这些值不同,以及我在哪里可以找到如何计算CRC16和CRC32值的代码(我计划稍后学习如何计算,但现在
BD001325E032091B94C412AC
”计算到CRC16=12AC中时。文档说明最后两个八位字节是CRC16值,因此我将“BD001325E032091B94C4
”输入站点,并接收5A90作为结果,而不是12AC
有人知道为什么这些值不同,以及我在哪里可以找到如何计算CRC16和CRC32值的代码(我计划稍后学习如何计算,但现在时间不允许)
更多信息如下:
16000040FFFFFFFF00015FCB
3C00003144010405E57022C7
BA00001144010101B970F0ED
3900010101390401B3049FF1
09900C800000000000008CF3
8590000000000000000035F7
00900259025902590259EBC9
0200002B00080191014BF5A2
BB0000BEE0014401B970E51E
3D000322D0320A2510A263A0
2C0001440000D60000D65E54
--编辑--
我已经包括了更多的信息。我参考的文件是TIA-102.BAAA-A(来自TIA标准)。以下是文件说明的内容(尽量避免侵犯版权):
分组中的最后一个块包括用户信息和/或信息的几个八位字节
填充八位字节,然后进行4位八位字节CRC奇偶校验。这被称为
数据包CRC
数据包CRC是对所有数据进行编码的4八位循环冗余校验
包含在中间块中的八位字节和用户信息的八位字节
最后一个街区。具体计算如下
设k为数据包覆盖的用户信息和pad位的总数
计算CRC。将K消息位作为A的系数
k–1次多项式M(x),关联第0条消息的MSB
具有x^k–1的八位字节和具有x^0的最后一个消息八位字节的LSB。定义
生成多项式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字段中,其MSB为第0位
对应于x^31的CRC八位字节和CRC第三个八位字节的LSB
对应于x^0
在上面的引文中,我用了
^
来显示powers,因为引文时格式没有保持不变。我不知道该怎么做,但这有帮助吗?CRC计算有很多参数:多项式、初始值、最终异或。。。有关详细信息,请参阅。您的CRC似乎与您使用的站点不匹配,但您可以尝试从文档中找到正确的参数,并使用不同的计算器,例如(尽管我担心它不支持十六进制输入)
需要记住的一点是,CRC-16通常是在假定为校验和加上两个零字节的数据上计算的,例如,您可能正在寻找一个CRC16
函数,其中CRC16(BD001325E032091B94C40000)==12AC
。通过这种方式计算校验和,附加校验和的数据的CRC将计算为0,这使得检查更容易,例如CRC16(BD001325E032091B94C412AC)=0000
./reveng -w 16 -s 16000040FFFFFFFF00015FCB 3C00003144010405E57022C7 BA00001144010101B970F0ED 3900010101390401B3049FF1 09900C800000000000008CF3 8590000000000000000035F7 00900259025902590259EBC9 0200002B00080191014BF5A2 BB0000BEE0014401B970E51E 3D000322D0320A2510A263A0 2C0001440000D60000D65E54
width=16 poly=0x1021 init=0xc921 refin=false refout=false xorout=0x0000 check=0x2fcf name=(none)
如“(无)”所示,16位CRC不是reveng上列出的22位CRC中的任何一位,尽管它与其中几个类似,只是在初始化方面有所不同
<> P>附加信息,是根据ReXG目录中的CRC-32或CRC-32/BZIP,根据位是否颠倒而提供的。 < P>我在Internet上发现了一个C++类,它使用了一个长的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;
}
}
公共类CRC32
{
私有int[]可移植;
公共CRC32(){
this.iTable=新整数[256];
Init();
}
/**
*使用PKZIP、WINZIP和Ethernet使用的多项式初始化iTable。
*/
私有void Init()
{
//0x04C11DB7是PKZip、WinZip和以太网使用的官方多项式。
int iPolynomial=0x04C11DB7;
//256个值,表示ASCII字符代码。
对于(int iAscii=0;iAscii,12AC值从何处获得?BD001325E032091B94C412AC的最后两个字节。如果查看示例块,最后两个字节似乎始终是校验和。可能存在不同的CRC-16值,这完全取决于使用的多项式。Gabriel:“文档”=什么文档?你是对的,我没有确定是哪一个文档。我编辑了我的问题以显示更多细节。我更新了我的原始问题以包含更多信息。我现在将此标记为答案,因为我没有太多时间专门学习CRC,听起来我有很多工作要做或者是我收到的那些。谢谢你提供的链接和信息。我会在某一天重新访问这些链接并尝试理解它们。@ygoe修复。谢谢。