Valhalla Legends Forums Archive | C/C++ Programming | Starcraft Cdkey Decode

AuthorMessageTime
Yegg
I'm having a lot of trouble getting a smaller version of the Starcraft cdkey decode function based off of the Python version that netytan and I wrote. The code is a almost a direct conversion from Python to C, yet C returns different results. Here is the code for the C version,

[code]void DecodeStarcraftKey(char *key) {
short i = 11;
int c, v = 0x13AC9741, seq[] = {6, 0, 2, 9, 3, 11, 1, 7, 5, 4, 10, 8};
for (; i > 0; i--) {
c = key[seq[i]];
if (c < 55) {
c = v & 0xFF & 7 ^ c;
v >>= 3;
} else c = i & 1 ^ c;
key[i] = c;
}
}[/code]

If I were to decode "1234567890123" with this function, my final result would be: "1404591935083". However, the Python version would be: "7263531935083". Below I will show the comparisons of the variable, c in the Python version, and the C version. For the most part they are the same, a few values change and this ruins the rest of the function.

Python:
9
1
5
6
8
2
2
4
0
3
1
7

C:
9
1
5
6
8
2
8
4
5
3
1

netytan and I have already wasted enough time on this and really have gotten nowhere with it. The Python version outputs 12 values if you include a print c in there, including a printf() (for c variable) in C outputs only 11 variables. This is obviously wrong. This code is becoming quite annoying, so for the time being I've given up on it. If I figure anything out I'll be sure to update my post. Any help would be greatly appreciated.
February 28, 2006, 10:41 PM
l2k-Shadow
[code]
short i = 11;
for (; i > 0; i--)
[/code]

Well now this is why it only outputs 11 numbers. You are telling it ok i = 11 and decrement i by 1 while i is bigger than 0.
[code]
for (; i >= 0; i--)
[/code]
^ works better.

Also
[code]
if (c < 55)
[/code]
is incorrect. Should be:
[code]
if (c <= 55)
[/code]

also try
[code]
c ^= ((v & 0xff) & 7)
[/code]
better coding habit..

plenty of fixed can be done on that code. the else should be an else if, although i dont think it matters:
[code]
else if (c < 65) {
c ^= (i & 1)
}
[/code]



March 2, 2006, 3:52 AM
Yegg
Thank you shadow, I also received help from hdx. He noted that I could not just replace the characters in key[] the way I was doing. I need to create a second char array and output the chars taken from key[] to my new char array (deckey[]). Here's the new code:

[code]void DecodeStarcraftKey(char *key, char *deckey) {
short i = 11;
int c, v = 0x13AC9741, seq[] = {6, 0, 2, 9, 3, 11, 1, 7, 5, 4, 10, 8};
deckey[12] = key[12];
for (; i >= 0; i--) {
c = key[seq[i]];
if (c <= 55) {
c ^= (v & 0xff & 7);
v >>= 3;
} else c ^= i & 1;
deckey[i] = c;
}
}[/code]
March 2, 2006, 11:40 PM

Search