Skip to main content
GameDev.net gamedev.net
🔒 Locked

preprocessor directive to encode a string?

Started by floatingwoods Apr 23, 2009 at 6:49 AM 4 replies 1.6k views
Original Post
floatingwoods
floatingwoods
Does anyone have an idea how to convert a string into something that can't be read that easily when opening the compiled application with a hex-editor? I want to avoid users to easily change some strings in my application by using a hex editor, so I ideally want to see the strings in my source code (for me to understand and modify), but not in the compiled application. At run-time, I can then convert back (or decode) the strings to what they are supposed to say before printing them. The encoding doesn't need to be complex, a simple shift of a few characters would already do (e.g. "ABCDE" --> "FGHIJ").
Rewdew
Rewdew
How about a WHATEVER("yourstring") and having a preprocessor replace the string before compiling? It's probably the most flexible way. Of course you'll still need code to decode the strings, which will be very easy to reverse, at which point it's just a matter of 3 minutes to write an IDA script that'll decode all your strings :-).
Zahlman
Zahlman
Put the strings in a text file. Encrypt the text file. Embed the text file as a resource in the program (optional). Write the code to extract text from the resource (or read it from the file, if left as a file). Write the code to decrypt the text.

For development, have a script that re-en/decrypts the text file.
visitor
visitor
Quote:

The encoding doesn't need to be complex, a simple shift of a few characters would already do (e.g. "ABCDE" --> "FGHIJ").


If your "attacker" is smart enough to use a hex-editor, Ceasar's shift is not going to stop them... :)
samoth
samoth
The preprocessor can't do that, nor can templates as string literals can't be template parameters (unless you're willing to encode your text char by char, which would be a real pain).

However, you can write a plugin for your IDE which converts all occurrences of decode("foo") into something like decode("Z!q" /* foo */) via a regex. Optionally, have the regex catch the text in comments and back-replace het obfuscated string. That way, it's unintrusive, you can still edit strings as plaintext. That's the solution I use, it works quite well.

You should be aware, however, that this kind of obfuscation will at best scare off a few 8 year olds, it is not a serious obstacle of any kind for someone who is trying to read your code.
floatingwoods
floatingwoods
Thank you for all the inputs. I don't have that many strings to encode, so Zahlman's solution would have been a little bit overkill. I instead wrote a simple string to hex, and hex to string routine (with some additional encoding) and simply copy/pasted the output of an encoding into the code like:

txt=getLightDecodedString("0a2b45ff19ced778")

The purpose was not to avoid at all cost anyone to change the text, but simply avoid the easy scenario where someone uses a hex editor to modify the text.
Rewdew
Rewdew
Hm how exactly is it harder to change the text when you encoded it that way?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.