反转 CRC32
我找到了一个反转 CRC32 的代码,但我不知道它是如何工作的,因为我不太擅长编程,我刚刚开始。我只想比较 2 个文件,即旧文件和新文件,然后在新文件中修复 CRC32 在文件末尾添加 4 个字节,因此这 2 个文件将具有相同的 CRC32。这是代码,用 C# 编写:
public class Crc32
{
public const uint poly = 0xedb88320;
public const uint startxor = 0xffffffff;
static uint[] table = null;
static uint[] revtable = null;
public void FixChecksum(byte[] bytes, int length, int fixpos, uint wantcrc)
{
if (fixpos + 4 > length) return;
uint crc = startxor;
for (int i = 0; i < fixpos; i++) {
crc = (crc >> 8) ^ table[(crc ^ bytes[i]) & 0xff];
}
Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
crc = wantcrc ^ startxor;
for (int i = length - 1; i >= fixpos; i--) {
crc = (crc << 8) ^ revtable[crc >> (3 * 8)] ^ bytes[i];
}
Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
}
public Crc32()
{
if (Crc32.table == null) {
uint[] table = new uint[256];
uint[] revtable = new uint[256];
uint fwd, rev;
for (int i = 0; i < table.Length; i++) {
fwd = (uint)i;
rev = (uint)(i) << (3 * 8);
for (int j = 8; j > 0; j--) {
if ((fwd & 1) == 1) {
fwd = (uint)((fwd >> 1) ^ poly);
} else {
fwd >>= 1;
}
if ((rev & 0x80000000) != 0) {
rev = ((rev ^ poly) << 1) | 1;
} else {
rev <<= 1;
}
}
table[i] = fwd;
revtable[i] = rev;
}
Crc32.table = table;
Crc32.revtable = revtable;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些 PHP 代码,也可以满足您的需求
我花了很多时间试图出于合法目的这样做
所以我希望它能帮助某人
像这样的函数可以用于:
要在线测试 crc32 字符串,您可以使用: http://www.lammertbies.nl/comm/info/crc-calculation.html
请参阅http://www.reversing.be/article.php?story=20061209172953555 摘自此处
由于我不是原作者,原作者在他的文章上花了很多时间,而且
提供了不小的帮助
这是 PHP 代码,可用于修改给定文件,如下所示:
Here's some PHP code that will also do what you're looking for
I spent a great deal of time trying to do this for legitimate purposes
so I hope it helps someone
Functions like this could be used to:
To test crc32 strings online you can use: http://www.lammertbies.nl/comm/info/crc-calculation.html
See http://www.reversing.be/article.php?story=20061209172953555 from where this was adapted from
as I'm not the original author, the original spent a great deal of time on his essay, and it
helped in no small way
Here's PHP code which can be used to modify a given file as follows:
您想知道它是如何工作的,或者如何使用它吗?
如果是后者,则从代码签名来看:
似乎您将第二个文件的内容放入一个数组中(末尾有额外的 4 个字节用于修复),并将其作为
bytes
参数。您将此数组的长度作为length
参数传递,将偏移量传递到要插入修复的位置(在本例中为 length - 4)作为fixpos
参数,并将您想要的CRC作为wantcrc
参数,您可以通过计算第一个文件的CRC来获得该值。FixChecksum
方法似乎将 4 字节修复写入您提供的数组中您提供的偏移量处。调用FixChecksum
后,您只需将结果写入第二个文件。Would you like to know how it works, or how to use it?
If it's the later, than from the code signature:
It seems that you put the contents of your second file to an array (with additional 4 bytes at the end for the fix), and pass it as the
bytes
parameter. You pass the lengths of this array as thelength
parameter, you pass the offset to the place to insert the fix to, (in this case length - 4) asfixpos
parameter, and you put your desired CRC aswantcrc
parameter, you can obtain this value, by calculating CRC of the first file.FixChecksum
method appears to write the 4 bytes fix in the array you have provided at the offset you have provided. After you've made the call toFixChecksum
you just need to write the results to your second file.