khany
khany

Reputation: 1187

vb xor checksum

This question may already have been asked but nothing on SO actually gave me the answer I need.

I am trying to reverse engineer someone else's vb.NET code and I am stuck with what a Xor is doing here. Here is 1 line of the body of a soap request that gets parsed (some values have been obscured so the checksum may not work in this case):

<HD>CHANGEDTHIS01,W-A,0,7753.2018E,1122.6674N, 0.00,1,CID_V_01*3B</HD>

and this is the snippet of vb code that checks it

LastStar = strValues(CheckLoop).IndexOf("*")
StrLen = strValues(CheckLoop).Length
TransCheckSum = Val("&h" + strValues(CheckLoop).Substring(LastStar + 1, (StrLen - (LastStar + 1))))

CheckSum = 0
For CheckString = 0 To LastStar - 1
    CheckSum = CheckSum Xor Asc(strValues(CheckLoop)(CheckString))
Next '

If CheckSum <> TransCheckSum Then
    'error with the checksum
    ...

OK, I get it up to the For loop. I just need an explanation of what the Xor is doing and how that is used for the checksum.

Thanks.

PS: As a bonus, if anyone can provide a c# translation I would be most grateful.

Upvotes: 1

Views: 4469

Answers (2)

Guffa
Guffa

Reputation: 700690

Using Xor is a simple algorithm to calculate a checksum. The idea is the same as when calculating a parity bit, but there is eight bits calculated across the bytes. More advanced algorithms like CRC and MD5 are often used to calculate checksums for more demanding applications.

The C# code would look like this:

string value = strValues[checkLoop];

int lastStar = value.IndexOf("*");
int transCheckSum = Convert.ToByte(value.Substring(lastStar + 1, 2), 16);

int checkSum = 0;
for (int checkString = 4; checkString < lastStar; checkString++) {
  checkSum ^= (int)value[checkString];
}

if (checkSum != transCheckSum) {
  // error with the checksum
}

I made some adjustments to the code to accomodate the transformation to C#, and some things that makes sense. I declared the variables used, and used camel case rather than Pascal case for local variables. I use a local variable for the string, instead of getting it from the collection each time.

The VB Val method stops parsing when it finds a character that it doesn't recognise, so to use the framework methods I assumed that the length of the checksum is two characters, so that it can parse the string "3B" rather than "3B</HD>".

The loop starts at the fourth character, to skip the first "<HD>", which should logically not be part of the data that the checksum should be calculated for.

In C# you don't need the Asc function to get the character code, you can just cast the char to an int.

Upvotes: 2

Axel
Axel

Reputation: 361

The code is basically getting the character values and doing a Xor in order to check the integrity, you have a very nice explanation of the operation in this page, in the Parity Check section : http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html

Upvotes: 0

Related Questions