BalticMusicFan
BalticMusicFan

Reputation: 663

Encrypt strings in pure C in order to hide them in the executable

I am trying to avoid literal strings from appearing in the executable. I want to put a string encrypted in the executable and then decrypt and use it at runtime. I read a bit into How to hide a string in binary code? and http://bytes.com/topic/c/answers/222096-hiding-string-compiled-code, but this is not exactly what I want.

Could it be done using macros? For example, I can declare a wide char string using

wchar_t str[] = L"value"    

or

wchar_t *str = L"value"    

and magical L will convert chars to widechars. Is it possible to create another magical L like this to make encrypted strings? E.g:

ENC"value"    

For the moment I am defining already encrypted strings as macros:

#define SAMPLE_STRING "ftnurd eru etrth" /*Random chars for imitating encryption*/   

and passing them to a normal function like

char *decstr(char *data)    

Is there an easier method to do this?

I am using Windows XP SP3 with Visual C++ 2008 Express.

Upvotes: 1

Views: 544

Answers (1)

user529758
user529758

Reputation:

Why not simply create an encryption and decryption function? Then you could write something like

char *hidden_password = my_encrypt("pAsSw0rd");
puts(hidden_pasword); // "aCud5Ttei23w8D"
// ...
char *real_password = my_decrypt("aCud5Ttei23w8D");
puts(real_password); // "pAsSw0rd"

However, it's not a good idea to store hard-wired text strings in your executable if they contain sensitive data. Also, don't reinvent the wheel - before rolling your very own encryption method, try some established, high-quality, cryptographically secure libraries like OpenSSL.

Upvotes: 1

Related Questions