-
Notifications
You must be signed in to change notification settings - Fork 4
/
encrypt.c
72 lines (63 loc) · 1.51 KB
/
encrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* File encrypt.c */
#include "wand_head.h"
#ifndef __CONVERT
#include <curses.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Uses seeded random xor to encrypt because setkey doesnt work on our
system. */
void crypt_file(char *name)
{
char buffer[1024];
int fd, length, loop;
if ((fd = open(name, O_RDONLY)) == -1)
{
#ifndef __CONVERT
endwin();
#endif
sprintf(buffer, "Wanderer: cannot open %s", name);
perror(buffer);
exit(1);
}
if ((length = read(fd, buffer, 1024)) < 1)
{
#ifndef __CONVERT
endwin();
#endif
sprintf(buffer, "Wanderer: read error on %s", name);
perror(buffer);
exit(1);
}
close(fd);
/* Right, got it in here, now to encrypt the stuff */
#ifndef __CONVERT
addstr("Running crypt routine....\n");
refresh();
#endif
srand(BLURFL);
for (loop = 0; loop < length; loop++)
buffer[loop] ^= rand();
if ((fd = open(name, O_WRONLY | O_TRUNC)) == -1)
{
#ifndef __CONVERT
endwin();
#endif
sprintf(buffer, "Wanderer: cannot write to %s", name);
perror(buffer);
exit(1);
}
if (write(fd, buffer, length) != length)
{
#ifndef __CONVERT
endwin();
#endif
sprintf(buffer, "Wanderer: write error on %s", name);
perror(buffer);
exit(1);
}
close(fd);
/* ok, file now contains encrypted/decrypted game. */
/* lets go back home... */
}