反转CRC32

时间:2009-10-03 15:26:10

标签: algorithm reverse-engineering reverse crc crc32

我正在寻找一种扭转a CRC32 checksum的方法。有解决方案,但它们是badly writtenextremely technical和/或in Assembly。汇编(目前)超出了我的范围,所以我希望有人可以用更高级别的语言拼凑一个实现。 Ruby是理想的,但我可以解析PHP,Python,C,Java等。

任何参赛者?

4 个答案:

答案 0 :(得分:18)

如果原始字符串为4个字节或更少,CRC32只能是可逆的。

答案 1 :(得分:5)

阅读the document called "Reversing CRC Theory and Practice"

这是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;
        }
    }
}

答案 2 :(得分:1)

如果您知道创建的多边形,则可以通过退出位来生成原始的32位来反转它。但是如果你想从给定的文件中反转CRC32并在文件的末尾添加一系列字节以匹配原始的CRC我在PHP中发布的代码:

我花了一些时间在上面,所以我希望它可以帮助那些处理更棘手问题的人: Reversing CRC32 干杯!

答案 3 :(得分:0)

Cade Roux关于逆转CRC32是正确的。

您提到的链接提供了一种解决方案,通过更改原始字节流来修复已变为无效的CRC。通过更改一些(不重要的)字节来实现此修复,从而重新创建原始CRC值。