-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.cpp
More file actions
105 lines (88 loc) · 2.55 KB
/
Copy pathforge.cpp
File metadata and controls
105 lines (88 loc) · 2.55 KB
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
#include <stdio.h>
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define BUF_SZ 1024
#define FILESZ 8192
int cpy(char* src, int offset, int srclen, char* dst, int cpylen){
if (offset > srclen)
return 0;
int count = cpylen;
if (offset + count > srclen){
count = srclen - offset;
//printf("reducing copy amount from %d to %d\n", cpylen, count);
}
memcpy(dst, &src[offset], count);
return count;
}
int encrypt(int max_value, char* file, int filelen)
{
int i;
int count;
unsigned int in_buf[BUF_SZ], temp, sum;
char in[BUF_SZ];
int fd;
int last;
last = 13;
sum = 0;
//while ((count = read(fd, in, BUF_SZ)) > 0) {
int bufloc=0;
while( (count = cpy(file, bufloc, filelen, in, BUF_SZ)) > 0 ){
if (count == 0) {
//close(fd);
return -1;
}
in_buf[0] = (unsigned int)in[0];
bufloc++;
sum = (sum + in_buf[0]) % UINT_MAX;
in_buf[0] = (last + in_buf[0] + 11) % UINT_MAX;
for (i = 1; i < count; i++) {
temp = (unsigned int)in[i];
bufloc++;
sum = (sum + temp) % UINT_MAX;
in_buf[i] = (((in_buf[i - 1] * 7 + temp) * 13571) %
UINT_MAX + temp + i) % UINT_MAX;
/* printf(" %u ",in_buf[i]); */
}
last = (in_buf[i - 1] + sum) % max_value;
}
//fprintf(stderr, "bufloc==%d, filelen==%d\n", bufloc, filelen);
if (count < 0) {
printf("Error: read input file\n");
return -1;
}
//printf("%d", last);
//close(fd);
return last;
}
int main(int argc, char** argv){
if (argc < 4){
printf("Usage: %s <MaxValue> <DesiredResult> <ForgedFile>\n", argv[0]);
return 1;
}
int MAXVAL = atoi(argv[1]);
int WANT = atoi(argv[2]);
char* FILENAME = argv[3];
char buffer[FILESZ];
memset(buffer, 0, FILESZ);
int filelen = 0;
FILE *f = fopen(FILENAME, "r");
if (f == NULL) perror("fopen");
else {
filelen = fread(buffer, sizeof(char), BUF_SZ, f);
fclose(f);
}
int current = encrypt(MAXVAL, buffer, filelen);
//fprintf(stderr, "Currently have: %d, need: %d\n", current, WANT);
int tmplen = filelen;
while(current != WANT && tmplen < FILESZ-2){
buffer[tmplen++] = ' ';
buffer[tmplen++] = '\x08';
current = encrypt(MAXVAL, buffer, tmplen);
//fprintf(stderr, "Currently have: %d, need: %d\n", current, WANT);
}
printf("%s", buffer);
return 0;
}