Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Changing CRC32 code

Newbie Spellweaver
Joined
Aug 30, 2012
Messages
7
Reaction score
0
Hi there,
I'm making a program that can calculate and change the CRC32 code of files.
I found a nice calculation and it works great. But now I want my program to be able to change the crc to the one I want such as:
Dim crc32 as string = 69921A00

Someone told me that I couldnt change it but I had to write new file and then add bytes to it. So writing the file I want to change the crc32 of and then add bytes, so I can set the Crc32 code to what I want? Any ideas?


Here is the CRC32 calculation code:
Code:
Option Explicit On
Option Strict On

<System.Diagnostics.DebuggerStepThrough()> _
Friend Class CRC32

    Private crc32Table() As Integer
    Private Const BUFFER_SIZE As Integer = 1024I

    Friend Function GetCrc32(ByRef stream As System.IO.FileStream) As Integer
        Dim crc32Result As Integer = &HFFFFFFFF

        Dim buffer(BUFFER_SIZE) As Byte
        Dim readSize As Integer = BUFFER_SIZE
        Dim count As Integer = stream.Read(buffer, 0I, readSize)
        Dim i As Integer
        Dim iLookup As Integer

        Do While (count > 0I)
            For i = 0I To count - 1I
                iLookup = (crc32Result And &HFF) Xor buffer(i)
                crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                crc32Result = crc32Result Xor crc32Table(iLookup)
            Next i
            count = stream.Read(buffer, 0I, readSize)
        Loop
        Return Not (crc32Result)
    End Function

    Friend Function GetCrc32String(ByRef stream As System.IO.FileStream) As String
        Return String.Format("{0:X8}", GetCrc32(stream))
    End Function

    Friend Sub New()
        ' This is the official polynomial used by CRC32 in PKZip.
        ' Often the polynomial is shown reversed (04C11DB7).
        Dim dwPolynomial As Integer = &HEDB88320
        Dim i, j As Integer

        ReDim crc32Table(256I)
        Dim dwCrc As Integer

        For i = 0I To 255I
            dwCrc = i
            For j = 8I To 1I Step -1I
                If (dwCrc And 1I) > 0I Then
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2I) And &H7FFFFFFF
                    dwCrc = dwCrc Xor dwPolynomial
                Else
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2I) And &H7FFFFFFF
                End If
            Next j
            crc32Table(i) = dwCrc
        Next i
    End Sub
End Class
 
Ginger by design.
Loyal Member
Joined
Feb 15, 2007
Messages
2,340
Reaction score
653
If you're trying to cause collisions, all you have to do is pad the file with 4 bytes and take the checksum, then increment those 4 bytes as they were a DWORD from 00 00 00 00 to ff ff ff ff and you should generate a collision. The probability that you won't generate the collision you want with those 4 bytes is incredibly unlikely. There are faster ways to do this, so feel free to research the math involved.

Also, the most efficient way to iterate these bytes is to calculate a CRC up to the end of the file, then save your current state and calculate the CRC with the next block, if it's not the desired one, reset to the original state, and repeat. A modern computer should be able to generate a collision doing this in a few seconds.
 
Back
Top