mirror of
https://git.zap.org.au/git/trader.git
synced 2024-09-01 17:14:15 -04:00
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.
This commit is contained in:
parent
91d77f002c
commit
200f7cdf97
23
src/game.c
23
src/game.c
@ -35,7 +35,18 @@
|
|||||||
* Module constants and macros *
|
* 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()
|
// Macros used in load_game()
|
||||||
@ -103,7 +114,7 @@
|
|||||||
#define save_game_printf(_fmt, _var) \
|
#define save_game_printf(_fmt, _var) \
|
||||||
{ \
|
{ \
|
||||||
snprintf(buf, GAME_BUFSIZE, _fmt "\n", _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); \
|
fprintf(file, "%s", buf); \
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,6 +495,7 @@ void end_game (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @@@ To be written
|
// @@@ To be written
|
||||||
|
save_game(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -662,6 +674,7 @@ bool save_game (int num)
|
|||||||
FILE *file;
|
FILE *file;
|
||||||
int saved_errno;
|
int saved_errno;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
|
int crypt_key;
|
||||||
int i, j, x, y;
|
int i, j, x, y;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
@ -673,6 +686,8 @@ bool save_game (int num)
|
|||||||
err_exit("out of memory");
|
err_exit("out of memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
crypt_key = game_file_crypt_key[randi(GAME_FILE_CRYPT_KEY_SIZE)];
|
||||||
|
|
||||||
// Create the data directory, if needed
|
// Create the data directory, if needed
|
||||||
data_dir = data_directory();
|
data_dir = data_directory();
|
||||||
if (data_dir != NULL) {
|
if (data_dir != NULL) {
|
||||||
@ -731,7 +746,7 @@ bool save_game (int num)
|
|||||||
|
|
||||||
// Write out the game file header and encryption key
|
// Write out the game file header and encryption key
|
||||||
fprintf(file, "%s\n" "%s\n", GAME_FILE_HEADER, GAME_FILE_API_VERSION);
|
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
|
// Write out various game variables
|
||||||
save_game_write_int(MAX_X);
|
save_game_write_int(MAX_X);
|
||||||
@ -774,7 +789,7 @@ bool save_game (int num)
|
|||||||
*p++ = '\n';
|
*p++ = '\n';
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
scramble(GAME_FILE_CRYPT_KEY, buf, GAME_BUFSIZE);
|
scramble(crypt_key, buf, GAME_BUFSIZE);
|
||||||
fprintf(file, "%s", buf);
|
fprintf(file, "%s", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,6 @@
|
|||||||
#define GAME_FILE_HEADER PACKAGE_NAME " Saved Game"
|
#define GAME_FILE_HEADER PACKAGE_NAME " Saved Game"
|
||||||
#define GAME_FILE_API_VERSION "7.0" /* For game loads and saves */
|
#define GAME_FILE_API_VERSION "7.0" /* For game loads and saves */
|
||||||
#define GAME_FILE_SENTINEL (42)
|
#define GAME_FILE_SENTINEL (42)
|
||||||
#define GAME_FILE_CRYPT_KEY (0xAA)
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
|
38
src/utils.c
38
src/utils.c
@ -323,18 +323,18 @@ extern int randi (int limit)
|
|||||||
|
|
||||||
char *scramble (int key, char *buf, int bufsize)
|
char *scramble (int key, char *buf, int bufsize)
|
||||||
{
|
{
|
||||||
int i;
|
if ((buf != NULL) && (key != 0)) {
|
||||||
char c, e;
|
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) {
|
if ((c != '\r') && (c != '\n') &&
|
||||||
key = (~ key) & 0xFF;
|
(r != '\r') && (r != '\n') && (r != '\0')) {
|
||||||
for (i = 0; (i < bufsize) && (buf[i] != '\0'); i++) {
|
*p = r;
|
||||||
c = buf[i];
|
|
||||||
e = c ^ key; // Simple encryption: XOR!
|
|
||||||
if ((c != '\r') && (c != '\n')
|
|
||||||
&& (e != '\r') && (e != '\n') && (e != '\0')) {
|
|
||||||
buf[i] = e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -360,21 +360,5 @@ char *scramble (int key, char *buf, int bufsize)
|
|||||||
|
|
||||||
char *unscramble (int key, char *buf, int bufsize)
|
char *unscramble (int key, char *buf, int bufsize)
|
||||||
{
|
{
|
||||||
int i;
|
return scramble(key, buf, bufsize);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user