: : : : : : : Hi!
: : : : : : :
: : : : : : : I have a question about this source code:
: : : : : : :
: : : : : : :
http://www.programmersheaven.com/download/4123/download.aspx
: : : : : : :
: : : : : : : I've downloaded it for use in a game I've made. It's purpose is to encrypt the hi-score file. It works perfectly in the DOS version of the game, but I'm now porting the game to Win32 and I've run into a problem.
: : : : : : : The encryption works as it should within the Win32 version of the game, but when I try to use a hi-score file made with the DOS version in the Win32 version, the decryption comes out completely messed up -- despite the fact that I've used the exact same code key in both versions! Why is that?!
: : : : : : :
: : : : : : It could be that the endianness is different between the two. See
http://en.wikipedia.org/wiki/Big_endian for info about endianness.
: : : : : : Most often encryptions are based on bit scrambling. If the endianness is different, then the unscrambling goes wrong.
: : : : : :
: : : : :
: : : : :
: : : : : Thanks for your reply! I've now read the wikipedia info on endianess and while I understand the concept I'll be damned if I know how to apply this to my program -- or if I can at all! I guess it isn't a problem with the source code as it doesn't change, but must be how the code is compiled in Borland Pascal versus Virtual Pascal?
: : : : :
: : : : You can remedy this by forcing both compilers to use a single storage method using a compiler directive, or by rewriting the file IO to make sure the order of the bytes is identical.
: : : :
: : : Hi !
: : : Doesn't the problem reside in the length of the Word type ?
: : : It's 16-bit long in Borland Pascal 32-bit in Virtual Pascal.
: : : You can either use the
SmallWord type instead of Word or use the {&Use32-} directive.
: : :
: : I never considered that, because as a TP7, 16-bit Delphi, and 32-bit Delphi programmer I've never seen a
word being anything else than a 16-bit unsigned integer. But that's a good point, and worth looking into for PP2005.
: :
:
:
: Thanks for the input, Alcatiz! I tried changing all the word types in my encryption unit to smallword, but that didn't change anything. Neither does using the {&Use32-} compiler directive, so I guess the problem isn't in the word type.
:
: Zibadian: I've looked for compiler directives for setting a single storage method, but haven't found any. Do you have any idea what it's called? I'm afraid that rewriting the file IO is a bit beyond my programming capabilities... :o(
:
In Delphi I cannot fins it either, but I found the Swap() function.
This kind of reading/writing is actually quite simple. The code below shows one way of copying the value of a word into an array:
type
TMyWord = array[0..1] of byte;
var
SomeWord: word;
SomeMyWord: TMyWord;
begin
Move(SomeWord, SomeMyWord, SizeOf(SomeWord));
end;
Then you can directly control the order of the bytes inthe writing part:
// Same endianness
write(f, SomeMyWord[0], SomeMyWord[1]);
// Different endianness
write(f, SomeMyWord[1], SomeMyWord[0]);
Reading it is quite similar:
// Same endianness
read(f, SomeMyWord[0], SomeMyWord[1]);
// Different endianness
read(f, SomeMyWord[1], SomeMyWord[0]);
Move(SomeMyWord, SomeWord, SizeOf(SomeWord));
You can expand these to include all multi-byte types as you see fit.