-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.h
139 lines (127 loc) · 4.98 KB
/
encrypt.h
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
#ifndef VARINT
#include "variableint.h"
#endif
typedef uint16_t BLOCKDATA;
struct lenstr
{
char *data;
BLOCKDATA len;
};
typedef struct lenstr lengthString;
lengthString encode(char *data, int *inputFormat, int *outputFormat, int datalen, int inputFormatLen, int outputFormatLen, bool verbose);
/*
// Basic plan: Convert from base (inputFormatSize) to base (outputFormatSize), they shouldn't share any traits
// Convert the data from the open file into the VariableInt
// Basic plan: Convert from base (inputFormatSize) to base (outputFormatSize), they shouldn't share any traits
// Convert the data from the open file into the VariableInt
{
VariableInt multer = variableInt();
setVarInt(&multer, inputFormatSize);
int dataRead;
while ((dataRead = fgetc(targetFile)) != EOF)
{
unsigned char reformat = inputFormat[dataRead];
multVarInt(dataVarInteger, &multer, dataVarInteger);
dataVarInteger->data[0] += reformat; // Insert the character here. What could possibly go wrong?
}
destroyVarInt(multer);
}
fclose(targetFile);
if (verbose)
{
printf("Numerical representation obtained\n");
}
// Read the VariableInt back out, in a different base encrypted in the output symbol format
{
int blocklen = bitlenVarInt(dataVarInteger) / ((outputFormatSize == 2) ? 2 : 1);
int pos = blocklen - 1;
if (verbose)
{
printf("Allocating memory for output\n");
}
char *newStr = malloc(blocklen); // This will be the maximum size needed to store the base conversion
if (verbose)
{
printf("Memory successfully allocated\n");
}
VariableInt outputLen = variableInt();
outputLen.data[0] = outputFormatSize;
while (dataVarInteger->data[0] != 0 || dataVarInteger->len > 1)
{
newStr[pos] = outputFormat[(unsigned char)divVarInt(dataVarInteger, &outputLen, dataVarInteger)];
pos--;
}
destroyVarInt(outputLen);
FILE *outputFile = fopen(outputFileNM, "wb");
if (!outputFile)
{
printf("The output file \"%s\" was not able to be created!\n", outputFileNM);
exit(EXIT_FAILURE);
}
for (pos++; pos < blocklen; pos++)
{
fputc(newStr[pos], outputFile);
}
fclose(outputFile);
}
// Convert the data from the open file into the VariableInt
int inputUnformat[256];
int outputUnformat[256];
unformat(inputFormat, inputUnformat, inputFormatSize);
unformat(outputFormat, outputUnformat, outputFormatSize);
{
// Get the contents of the file
VariableInt multer = variableInt();
setVarInt(&multer, outputFormatSize);
int dataRead = 0;
while ((dataRead = getc(targetFile)) != EOF)
{
int reformat = outputUnformat[(unsigned) dataRead];
if (reformat == -1)
{
fprintf(stderr, "The output format was insufficient to decrypt this file.\n");
exit(EXIT_FAILURE);
}
multVarInt(dataVarInteger, &multer, dataVarInteger);
dataVarInteger->data[0] += (unsigned char) reformat;
}
destroyVarInt(multer);
}
if (verbose)
{
printf("Numerical representation obtained\n");
}
fclose(targetFile);
{
int blocklen = bitlenVarInt(dataVarInteger) / ((outputFormatSize == 2) ? 2 : 1);
int pos = blocklen - 1;
if (verbose)
{
printf("Allocating memory for output\n");
}
char *newStr = malloc(blocklen); // This will be the maximum size needed to store the base conversion
if (verbose)
{
printf("Memory successfully allocated\n");
}
VariableInt inputLen = variableInt();
inputLen.data[0] = inputFormatSize;
while (dataVarInteger->data[0] != 0 || dataVarInteger->len > 1)
{
newStr[pos] = inputUnformat[(unsigned char)divVarInt(dataVarInteger, &inputLen, dataVarInteger)];
pos--;
}
destroyVarInt(inputLen);
FILE *outputFile = fopen(outputFileNM, "wb");
if (!outputFile)
{
printf("The output file \"%s\" was not able to be created!\n", outputFileNM);
exit(EXIT_FAILURE);
}
for (pos++; pos < blocklen; pos++)
{
fputc(newStr[pos], outputFile);
}
fclose(outputFile);
}
*/