-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
202 lines (166 loc) · 5.63 KB
/
Copy pathdecrypt.cpp
File metadata and controls
202 lines (166 loc) · 5.63 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int f1(int i, int j, char* key, int keylen){
return (key[i] ^ key[j]) % keylen;
}
int g1(int i, int j, char* key, int keylen){
return (key[i] * key[j]) % keylen;
}
char* getCol( char** buf, char* out, int numrows, int colidx){
for (int i = 0; i < numrows; ++i){
out[i] = buf[i][colidx];
}
}
int encrypt(char* key, int keylen, char* filename){
char** buffer = new char*[keylen+1];
for (int i = 0; i < keylen+1; ++i)
buffer[i] = new char[keylen];
char** buffer_clean = new char*[keylen+1];
for (int i = 0; i < keylen+1; ++i)
buffer_clean[i] = new char[keylen];
FILE* infile = fopen(filename, "rb");
if (!infile){
perror("fopen");
return -1;
}
// prepare the first row
char* row1 = new char[keylen+1];
strcpy(row1, key);
bool done = false;
int padding = 0;
while(!done){
int numRows = 0;
for (int i = 0; i < keylen; ++i){
int br = fread(buffer[i], sizeof(char), keylen, infile);
if (br == 0){
// no data on the row
done = true;
break;
} else if (br == keylen){
// we got a full row!
numRows++;
continue;
} else {
// pad to full row
int pad = keylen - br;
memset(&(buffer[i][br]), 0x00, pad);
padding += pad;
numRows++;
done = true;
break;
}
}
if (numRows == keylen){
// check if there is only 1 line left
long fpos = ftell(infile);
fseek(infile, 0, SEEK_END);
//fprintf(stderr, "fpos=%d, ftell(infile)==%d\n", fpos, ftell(infile));
if (ftell(infile) == fpos + keylen){
// this has the padding length, add to the current buffer
//fprintf(stderr, "One line remaining\n");
numRows++;
fseek(infile, fpos, SEEK_SET);
int br = fread(buffer[numRows-1], sizeof(char), keylen, infile);
done = true;
} else if (fpos == ftell(infile)){
done = true;
} else {
fseek(infile, fpos, SEEK_SET);
}
}
if (done && numRows == 0)
break;
// copy the buffer so we can reference it later
for (int i = 0; i < numRows; ++i)
for (int j = 0; j < keylen; ++j)
buffer_clean[i][j] = buffer[i][j];
// horizontal pass
for (int j = 0; j < keylen; ++j){
int x1=0;
for (int i = 0; i < numRows; ++i){
x1 += row1[i];
}
int x = (buffer[j][0] - x1) % 256;
buffer[j][0] = (char)x;
}
for (int i = 1; i < keylen; ++i){
char* prevcol = new char[numRows];
getCol(buffer, prevcol, numRows, i-1);
int x1=0;
for (int k = 0; k < numRows; ++k){
x1 += prevcol[k];
}
for (int j = 0; j < numRows; ++j){
int x = (buffer[j][i] - x1) % 256;
buffer[j][i] = (char)x;
}
delete[] prevcol;
}
// update the buffer so we can reference it later
for (int i = 0; i < numRows; ++i)
for (int j = 0; j < keylen; ++j)
buffer_clean[i][j] = buffer[i][j];
// Vertical pass
for (int j = 0; j < keylen; ++j){
int f = f1(0, j, row1, keylen);
int g = g1(0, j, row1, keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = row1[x1i];
int x2 = row1[x2i];
int x = (x1^x2)^buffer[0][j];
buffer[0][j] = (char)x;
}
for (int i = 1; i < numRows; ++i){
for (int j = 0; j < keylen; ++j){
int f = f1(i, j, buffer[i-1], keylen);
int g = g1(i, j, buffer[i-1], keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = buffer[i-1][x1i];
int x2 = buffer[i-1][x2i];
int x = (x1^x2)^(buffer[i][j]);
buffer[i][j] = (char)x;
}
}
// set up new row1
//memcpy(row1, buffer_clean[numRows-1], keylen);
// Remove padding and paddingLength
int pad = 0;
if (done){
//fprintf(stderr, "Done\n");
pad = buffer[numRows-1][0] << 8;
pad |= buffer[numRows-1][1];
for (int i = 0; i < numRows-2; ++i){
for (int j = 0; j < keylen; ++j)
printf("%c", buffer[i][j]);
}
for (int i = 0; i < keylen-pad; ++i)
printf("%c", buffer[numRows-2][i]);
} else {
for (int i = 0; i < numRows; ++i){
for (int j = 0; j < keylen; ++j)
printf("%c", buffer[i][j]);
}
}
}
}
int main(int argc, char** argv){
if (argc < 3){
printf("Usage: %s <Key> <Plaintext file>\n", argv[0]);
return 1;
}
char* key = new char[strlen(argv[1])];
strcpy(key, argv[1]);
char* filename = new char[strlen(argv[2])];
strcpy(filename, argv[2]);
encrypt(key, strlen(key), filename);
delete[] key;
delete[] filename;
}