gpt4 book ai didi

c# - 基本的 CRC32 维基百科实现与在线看到的标准 CRC32 不同

转载 作者:可可西里 更新时间:2023-11-01 08:27:56 25 4
gpt4 key购买 nike

我有一个基本的 CRC32 实现,遵循维基百科的 Code Fragment:1 sample .我认为我做对了,修改为对余数多项式使用 n 位寄存器,而不是按照示例使用 n+1 位。

我得到的结果和网上的CRC32实现结果不一样。在我的实现中,我必须在此处更改什么?

请忽略逻辑的 Console.Writeline 语句。

    const UInt32 poly = 0x04C11DB7;

public static UInt32 GenerateCRC_32(byte[] message)
{
byte[] augmentedMsg = new byte[message.Length + 4];
message.CopyTo(augmentedMsg, 0);

UInt32 remainder = Convert.ToUInt32(augmentedMsg[0]) << 24 |
Convert.ToUInt32(augmentedMsg[1]) << 16 |
Convert.ToUInt32(augmentedMsg[2]) << 8 |
Convert.ToUInt32(augmentedMsg[3]);

for (Int32 i = 4; i < augmentedMsg.Length; i++)
{
for (int bit = 0; bit < 8; bit++)
{
UInt32 nextBit = ((UInt32)augmentedMsg[i] >> (7 - bit)) & 0x01;
if ((remainder & 0x80000000) > 0)
{
Console.WriteLine("---------------DO XOR --------------------");
Console.WriteLine(Convert.ToString(((remainder << 1) | nextBit), 2).PadLeft(32, '0'));
Console.WriteLine(Convert.ToString(poly, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");

remainder = ((remainder << 1) | nextBit) ^ poly;

Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
else
{
remainder = (remainder << 1) | nextBit;

Console.WriteLine("--------------NO---------------------");
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
}
}

Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine(remainder.ToString("X"));

return remainder;
}

我不是在寻找优化逻辑的最佳方法,因为我只是想使用 C# 来遵循维基百科示例。

输入消息:'A'(十六进制:0x41)输出:0x30476DC0根据 this website :输出应该是:0xD3D99E8B

我想我缺少 CRC 的反转/初始化,但我不确定如何更改此基本实现以获得与网站结果相同的结果。

运行我的程序的输出:

--------------NO---------------------
10000010000000000000000000000000
------------------------------------------
---------------DO XOR --------------------
00000100000000000000000000000000
00000100110000010001110110110111
------------------------------------------
00000000110000010001110110110111
------------------------------------------
--------------NO---------------------
00000001100000100011101101101110
------------------------------------------
--------------NO---------------------
00000011000001000111011011011100
------------------------------------------
--------------NO---------------------
00000110000010001110110110111000
------------------------------------------
--------------NO---------------------
00001100000100011101101101110000
------------------------------------------
--------------NO---------------------
00011000001000111011011011100000
------------------------------------------
--------------NO---------------------
00110000010001110110110111000000
------------------------------------------
00110000010001110110110111000000

最后一行变成十六进制:0x30476DC0

@Mark Adler 评论的跟进:**

我修改了上面的内容如下,修改如下(代码内嵌注释):

  1. 初始化为0xFFFFFFFF
  2. 反转输入消息字节
  3. 与最终值异或,异或值的反转

    public static UInt32 GenerateCRC_32(字节[]消息){ byte[] augmentedMsg = new byte[message.Length + 8]; message.CopyTo(augmentedMsg, 4);//修改为初始化创建空间

    UInt32 remainder = Convert.ToUInt32(augmentedMsg[0]) << 24 |
    Convert.ToUInt32(augmentedMsg[1]) << 16 |
    Convert.ToUInt32(augmentedMsg[2]) << 8 |
    Convert.ToUInt32(augmentedMsg[3]);

    remainder = ~remainder; // Overwrite the above and initialized the register to 0xFFFFFFFF

    for (Int32 i = 4; i < augmentedMsg.Length; i++)
    {
    byte reversedMessage = Reverse(augmentedMsg[i]); // Reversed the augmented message byte
    for (int bit = 0; bit < 8; bit++)
    {
    UInt32 nextBit = Convert.ToUInt32(reversedMessage >> (7 - bit)) & 0x1; // Use the reversed message byte
    if ((remainder & 0x80000000) > 0)
    {
    Console.WriteLine("---------------DO XOR --------------------");
    Console.WriteLine(Convert.ToString(((remainder << 1) | nextBit), 2).PadLeft(32, '0'));
    Console.WriteLine(Convert.ToString(poly32, 2).PadLeft(32, '0'));
    Console.WriteLine("------------------------------------------");

    remainder = Convert.ToUInt32((UInt32)((UInt32)(remainder << 1) | nextBit) ^ poly32);

    Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
    Console.WriteLine("------------------------------------------");
    }
    else
    {
    remainder = (UInt32)((UInt32)(remainder << 1) | nextBit);

    Console.WriteLine("--------------NO---------------------");
    Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
    Console.WriteLine("------------------------------------------");
    }
    }
    }

    Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");

    remainder = (~remainder);

    Console.WriteLine("XOR ^ 0xFFFFFFFF : " + Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");

    remainder = Reverse(remainder);

    Console.WriteLine("Reversed the Abv : " + Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");
    return remainder;

输出:

---------------DO XOR --------------------
11111111111111111111111111111111
00000100110000010001110110110111
------------------------------------------
11111011001111101110001001001000
------------------------------------------
---------------DO XOR --------------------
11110110011111011100010010010000
00000100110000010001110110110111
------------------------------------------
11110010101111001101100100100111
------------------------------------------
---------------DO XOR --------------------
11100101011110011011001001001110
00000100110000010001110110110111
------------------------------------------
11100001101110001010111111111001
------------------------------------------
---------------DO XOR --------------------
11000011011100010101111111110010
00000100110000010001110110110111
------------------------------------------
11000111101100000100001001000101
------------------------------------------
---------------DO XOR --------------------
10001111011000001000010010001010
00000100110000010001110110110111
------------------------------------------
10001011101000011001100100111101
------------------------------------------
---------------DO XOR --------------------
00010111010000110011001001111010
00000100110000010001110110110111
------------------------------------------
00010011100000100010111111001101
------------------------------------------
--------------NO---------------------
00100111000001000101111110011011
------------------------------------------
--------------NO---------------------
01001110000010001011111100110110
------------------------------------------
--------------NO---------------------
10011100000100010111111001101100
------------------------------------------
---------------DO XOR --------------------
00111000001000101111110011011000
00000100110000010001110110110111
------------------------------------------
00111100111000111110000101101111
------------------------------------------
--------------NO---------------------
01111001110001111100001011011110
------------------------------------------
--------------NO---------------------
11110011100011111000010110111100
------------------------------------------
---------------DO XOR --------------------
11100111000111110000101101111000
00000100110000010001110110110111
------------------------------------------
11100011110111100001011011001111
------------------------------------------
---------------DO XOR --------------------
11000111101111000010110110011110
00000100110000010001110110110111
------------------------------------------
11000011011111010011000000101001
------------------------------------------
---------------DO XOR --------------------
10000110111110100110000001010010
00000100110000010001110110110111
------------------------------------------
10000010001110110111110111100101
------------------------------------------
---------------DO XOR --------------------
00000100011101101111101111001010
00000100110000010001110110110111
------------------------------------------
00000000101101111110011001111101
------------------------------------------
--------------NO---------------------
00000001011011111100110011111010
------------------------------------------
--------------NO---------------------
00000010110111111001100111110100
------------------------------------------
--------------NO---------------------
00000101101111110011001111101000
------------------------------------------
--------------NO---------------------
00001011011111100110011111010000
------------------------------------------
--------------NO---------------------
00010110111111001100111110100000
------------------------------------------
--------------NO---------------------
00101101111110011001111101000000
------------------------------------------
--------------NO---------------------
01011011111100110011111010000000
------------------------------------------
--------------NO---------------------
10110111111001100111110100000000
------------------------------------------
---------------DO XOR --------------------
01101111110011001111101000000000
00000100110000010001110110110111
------------------------------------------
01101011000011011110011110110111
------------------------------------------
--------------NO---------------------
11010110000110111100111101101110
------------------------------------------
---------------DO XOR --------------------
10101100001101111001111011011100
00000100110000010001110110110111
------------------------------------------
10101000111101101000001101101011
------------------------------------------
---------------DO XOR --------------------
01010001111011010000011011010110
00000100110000010001110110110111
------------------------------------------
01010101001011000001101101100001
------------------------------------------
--------------NO---------------------
10101010010110000011011011000010
------------------------------------------
---------------DO XOR --------------------
01010100101100000110110110000100
00000100110000010001110110110111
------------------------------------------
01010000011100010111000000110011
------------------------------------------
--------------NO---------------------
10100000111000101110000001100110
------------------------------------------
---------------DO XOR --------------------
01000001110001011100000011001100
00000100110000010001110110110111
------------------------------------------
01000101000001001101110101111011
------------------------------------------
--------------NO---------------------
10001010000010011011101011110110
------------------------------------------
---------------DO XOR --------------------
00010100000100110111010111101100
00000100110000010001110110110111
------------------------------------------
00010000110100100110100001011011
------------------------------------------
--------------NO---------------------
00100001101001001101000010110110
------------------------------------------
--------------NO---------------------
01000011010010011010000101101100
------------------------------------------
--------------NO---------------------
10000110100100110100001011011000
------------------------------------------
---------------DO XOR --------------------
00001101001001101000010110110000
00000100110000010001110110110111
------------------------------------------
00001001111001111001100000000111
------------------------------------------
--------------NO---------------------
00010011110011110011000000001110
------------------------------------------
--------------NO---------------------
00100111100111100110000000011100
------------------------------------------
00100111100111100110000000011100(279E601C)
XOR ^ 0xFFFFFFFF : 11011000011000011001111111100011(D8619FE3)
Reversed the Abv : 11000111111110011000011000011011(C7F9861B)

这不是预期的输出。我使用下面的查表代码实现了相同的结果,结果与上面完全相同(0xC7F9861B),这是错误的

public static UInt32 GenerateCRC_32_from_Table(byte[] message)
{
byte[] augmentedMsg = new byte[message.Length + 4];
message.CopyTo(augmentedMsg, 0);

UInt32 remainder = 0xFFFFFFFF;

foreach (byte msgByte in augmentedMsg)
{
byte reversedMsgByte = Reverse(msgByte);
remainder = ((remainder << 8) | Convert.ToUInt32(reversedMsgByte)) ^ crc32_table[((remainder >> 24)) & 0xFF];
}

remainder = Reverse(~remainder);
return remainder;
}

而如果我使用下面的代码(避免消息扩充)产生了正确的结果。

public static UInt32 GenerateCRC_32_from_Table(byte[] message)
{
UInt32 remainder = 0xFFFFFFFF;

foreach (byte msgByte in message)
{
byte reversedMsgByte = Reverse(msgByte);
remainder = (remainder << 8) ^ crc32_table[((remainder >> 24) ^ Convert.ToUInt32(reversedMsgByte)) & 0xFF];
}

remainder = Reverse(~remainder);
return remainder;
}

评论中提到的 Reverse() 和 poly32:**

    const UInt32 poly32 = 0x04C11DB7;

public static UInt32 Reverse(UInt32 message)
{
UInt32 msgReversed = 0;
for (int i = 0; i < 32; i++)
{
msgReversed = ((message & 0x80000000) >> (31 - i)) | msgReversed;
message = message << 1;
}
return msgReversed;
}

public static byte Reverse(byte message)
{
byte msgReversed = 0;
for (int i = 0; i < 8; i++)
{
msgReversed = (byte)(((byte)((byte)(message) & 0x80) >> (7 - i)) | msgReversed);
message = (byte)(message << 1);
}
return msgReversed;
}

最佳答案

您所指的标准 CRC 已反射(reflect)出来,即位被反转,并使用 0xfffffff 进行初始化,最终的 CRC 与 0xffffffff 进行异或运算>.

您正在生成 CRC,其中位未反转,初始 CRC 为零,并且末尾没有异或。

通常要实现位反转 CRC,您将输入字节保持原样,但反射(reflect)多项式并向另一个方向移动。输入字节被送入 CRC 的底部而不是顶部。通过增强消息方法,这也减少了四个要处理的字节总数。

更新问题:

问题中的代码试图“扩充”消息,但随后甚至没有使用扩充。相反,它使用从偏移量 4 开始的数据,这相当于使用从偏移量 0 开始的原始消息。

相反,应该做的是甚至不要尝试将消息扩充和排他或消息放入 CRC 的顶部,而不是尝试将消息馈送到 CRC 的底部。

此外,反转 CRC,应用反转的消息,然后再次反转 CRC 相当于什么都不做,而是反转多项式并向另一个方向移动。多项式是一个常数,所以在编写代码时会进行反转,如我的原始答案中所述。 0x04c11db7 反转为 0xedb88320

所以代码最终看起来像这样(在 C 中):

#include <stddef.h>
#include <stdint.h>

/* CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order. */
#define POLY 0xedb88320

/* Compute CRC of buf[0..len-1] with initial CRC crc. This permits the
computation of a CRC by feeding this routine a chunk of the input data at a
time. The value of crc for the first chunk should be zero. */
uint32_t crc32(uint32_t crc, const unsigned char *buf, size_t len)
{
int k;

crc = ~crc;
while (len--) {
crc ^= *buf++;
for (k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
}
return ~crc;
}

关于c# - 基本的 CRC32 维基百科实现与在线看到的标准 CRC32 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29745406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com