From 200f7cdf9704c2f32db04c9a359cdc5ca73ab322 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Thu, 14 Jul 2011 12:30:23 +1000 Subject: [PATCH] Revise the scramble() and unscramble() functions The scramble() and unscramble() functions now use a moving (incrementing) key for the XOR encryption/decryption. Also include a table of XOR keys to use at random for encryption. Revised the scramble() and unscramble() functions to use a moving (incrementing) key for the XOR encryption/decryption. Also generated a table of XOR keys to use at random for encryption. --- src/game.c | 23 +++++++++++++++++++---- src/trader.h | 1 - src/utils.c | 38 +++++++++++--------------------------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/game.c b/src/game.c index 854babc..bdda9a3 100644 --- a/src/game.c +++ b/src/game.c @@ -35,7 +35,18 @@ * Module constants and macros * ************************************************************************/ -#define GAME_BUFSIZE (1024) /* Buffer size for game save/load */ +// Game loading and saving constants + +#define GAME_BUFSIZE (1024) + +static const int game_file_crypt_key[] = { + 0x50, 0x52, 0x55, 0x59, 0x5A, 0x5C, 0x5F, + 0x90, 0x92, 0x95, 0x99, 0x9A, 0x9C, 0x9F, + 0xA0, 0xA2, 0xA5, 0xA9, 0xAA, 0xAC, 0xAF, + 0xD0, 0xD2, 0xD5, 0xD9, 0xDA, 0xDC, 0xDF +}; + +#define GAME_FILE_CRYPT_KEY_SIZE (sizeof(game_file_crypt_key) / sizeof(int)) // Macros used in load_game() @@ -103,7 +114,7 @@ #define save_game_printf(_fmt, _var) \ { \ snprintf(buf, GAME_BUFSIZE, _fmt "\n", _var); \ - scramble(GAME_FILE_CRYPT_KEY, buf, GAME_BUFSIZE); \ + scramble(crypt_key, buf, GAME_BUFSIZE); \ fprintf(file, "%s", buf); \ } @@ -484,6 +495,7 @@ void end_game (void) } // @@@ To be written + save_game(2); } @@ -662,6 +674,7 @@ bool save_game (int num) FILE *file; int saved_errno; struct stat statbuf; + int crypt_key; int i, j, x, y; char *p; @@ -673,6 +686,8 @@ bool save_game (int num) err_exit("out of memory"); } + crypt_key = game_file_crypt_key[randi(GAME_FILE_CRYPT_KEY_SIZE)]; + // Create the data directory, if needed data_dir = data_directory(); if (data_dir != NULL) { @@ -731,7 +746,7 @@ bool save_game (int num) // Write out the game file header and encryption key fprintf(file, "%s\n" "%s\n", GAME_FILE_HEADER, GAME_FILE_API_VERSION); - fprintf(file, "%d\n", GAME_FILE_CRYPT_KEY); + fprintf(file, "%d\n", crypt_key); // Write out various game variables save_game_write_int(MAX_X); @@ -774,7 +789,7 @@ bool save_game (int num) *p++ = '\n'; *p = '\0'; - scramble(GAME_FILE_CRYPT_KEY, buf, GAME_BUFSIZE); + scramble(crypt_key, buf, GAME_BUFSIZE); fprintf(file, "%s", buf); } diff --git a/src/trader.h b/src/trader.h index 1cb79c7..c0b319e 100644 --- a/src/trader.h +++ b/src/trader.h @@ -38,7 +38,6 @@ #define GAME_FILE_HEADER PACKAGE_NAME " Saved Game" #define GAME_FILE_API_VERSION "7.0" /* For game loads and saves */ #define GAME_FILE_SENTINEL (42) -#define GAME_FILE_CRYPT_KEY (0xAA) /************************************************************************ diff --git a/src/utils.c b/src/utils.c index 0d18c13..9534519 100644 --- a/src/utils.c +++ b/src/utils.c @@ -323,18 +323,18 @@ extern int randi (int limit) char *scramble (int key, char *buf, int bufsize) { - int i; - char c, e; + if ((buf != NULL) && (key != 0)) { + char *p = buf; + unsigned char k = ~key; + int i; + for (i = 0; (i < bufsize) && (*p != '\0'); i++, k++, p++) { + char c = *p; + char r = c ^ k; // Simple encryption: XOR on a moving key - if (key != 0) { - key = (~ key) & 0xFF; - for (i = 0; (i < bufsize) && (buf[i] != '\0'); i++) { - c = buf[i]; - e = c ^ key; // Simple encryption: XOR! - if ((c != '\r') && (c != '\n') - && (e != '\r') && (e != '\n') && (e != '\0')) { - buf[i] = e; + if ((c != '\r') && (c != '\n') && + (r != '\r') && (r != '\n') && (r != '\0')) { + *p = r; } } } @@ -360,21 +360,5 @@ char *scramble (int key, char *buf, int bufsize) char *unscramble (int key, char *buf, int bufsize) { - int i; - char c, d; - - - if (key != 0) { - key = (~ key) & 0xFF; - for (i = 0; (i < bufsize) && (buf[i] != '\0'); i++) { - c = buf[i]; - d = c ^ key; // Simple decryption: XOR! - if ((c != '\r') && (c != '\n') - && (d != '\r') && (d != '\n') && (d != '\0')) { - buf[i] = d; - } - } - } - - return buf; + return scramble(key, buf, bufsize); }