-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathencryptor.c
120 lines (116 loc) · 3 KB
/
encryptor.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*
* Filename: encryptor.c
* Description: My 1337 s3cur3 encrypt0r
* Author: Mad Sc0lTrAg <[email protected]>
* Comment: Comments are for n00bz
*
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
void encrypt(uint8_t * buffer, uint32_t lfsr, uint32_t poly, unsigned int length)
{
for(uint32_t i = 0; i != length; i++)
{
for(uint8_t j = 7;; j--)
{
unsigned char lsb = lfsr & 1;
buffer[i] ^= lsb<<j;
lfsr >>= 1;
if (lsb)
lfsr ^= poly;
if (j == 0) break;
}
}
}
uint32_t parse_int(char * str)
{
char *endptr;
uint32_t val = strtoul(str, &endptr, 16);
if (endptr == str || *endptr != '\0')
{
fprintf(stderr, "%s is not a valid hex number\n", str);
exit(1);
}
return val;
}
int main(int argc, char ** argv)
{
struct stat sb;
off_t len;
char *p;
int fd;
int c;
int index;
char * kvalue = NULL;
char * pvalue = NULL;
if (argc < 5) {
fprintf (stderr, "usage: %s -k hex_number -p hex_number <file>\n", argv[0]);
return 1;
}
while ((c = getopt (argc, argv, "p:k:")) != -1)
switch (c)
{
case 'k':
kvalue = optarg;
break;
case 'p':
pvalue = optarg;
break;
case '?':
if (optopt == 'k')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (optopt == 'p')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort ();
}
char * file_name = argv[optind];
uint32_t key = parse_int(kvalue);
uint32_t poly = parse_int(pvalue);
fd = open (file_name, O_RDWR);
if (fd == -1) {
fprintf(stderr, "Cannot open file %s for writing: %s\n", argv[optind], strerror(errno));
return 1;
}
if (fstat (fd, &sb) == -1) {
fprintf(stderr, "Cannot stat file %s: %s\n", argv[optind], strerror(errno));
return 1;
}
if (!S_ISREG (sb.st_mode)) {
fprintf (stderr, "%s is not a file\n", argv[optind]);
return 1;
}
p = mmap (0, sb.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
perror ("mmap failed:");
return 1;
}
encrypt(p, parse_int(kvalue), parse_int(pvalue), sb.st_size);
if (close (fd) == -1) {
perror ("close");
return 1;
}
if (munmap (p, sb.st_size) == -1) {
perror ("munmap");
return 1;
}
return 0;
}