mm201 / pkmn-classic-framework

Pokémon application logic for Generation IV and V, including servers
http://pkmnclassic.net/
Other
227 stars 43 forks source link

[WIKI] Mistake in Request encryption #42

Closed sepalani closed 7 years ago

sepalani commented 7 years ago

According to https://github.com/sepalani/GamestatsHTTP/wiki/Gamestats2-encryption and https://github.com/sepalani/GamestatsHTTP/commit/0a03ae8a7ca4fbc4ef7c0370b1ba69de67de3759 it seems that the seed is processed like this using a modulo:

rand[-1] = seed
rand[n] = (rand[n-1] * X + Y) % Z

And not like this:

rand[-1] = seed
rand[n] = (rand[n-1] * X + Y) & ~Z

@mm201 Could you confirm that this is the correct behaviour for these Pokémon DS games as well or maybe they are another special case?

mm201 commented 7 years ago

It's interesting to note that for all X, X % 0x80000000 == X & 0x7fffffff, and 0x80000000 is Diamond/Pearl's Z constant, so the results are identical! Every other game I tested seemed to work fine too, but I did mostly reverse the formula from data I observed. It's entirely possible that it's not the authentic formula.

Can you provide some examples of games for which it doesn't work?

sepalani commented 7 years ago

Sure, https://github.com/sepalani/GamestatsHTTP/issues/24, https://github.com/sepalani/GamestatsHTTP/issues/20, https://github.com/sepalani/GamestatsHTTP/issues/19.

mm201 commented 7 years ago

Thanks for the tip! I've filed the issue under the right project and will make the fix as soon as I'm able to test that it doesn't break anything.

sepalani commented 7 years ago

Could you also try to see if the presumably not encrypted games are actually encrypted using the above algorithm leading the encrypted data to be equal to the plain data?

mm201 commented 7 years ago

Yeah, it checks out. Disabling encryption may not actually be a thing on version 2/3 requests, just epic fail RNG constants! (If the Z constant is a PoT of 65536 or less, encryption is fully gimped)

The wiki should be fixed now.