-
Notifications
You must be signed in to change notification settings - Fork 1
/
decrypt.c
178 lines (157 loc) · 3.98 KB
/
decrypt.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
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
/*
* decrypt.c
*
* Functions for quickly decrypting a given input.
*
* James Shephard
* CMPT 300 - D100 Burnaby
* Instructor Brian Booth
* TA Scott Kristjanson
*/
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include "decrypt.h"
#include "memwatch.h"
//this table will map ascii values to the corresponding 'numeric' values
//as defined in the assignment's table
char conversion_table[255];
//Performs the inverse of the above, taking one of the 41 given characters
//and converting it into corresponding ascii
char inversion_table[255];
//Don't reinitialize tables every time
bool tables_initialized = false;
/*
* initialize_table
*
* Initializes our lookup and inverse lookup tables
*/
void initialize_table()
{
int i;
for (i = 0; i < 255; i++)
conversion_table[i] = -1; //default 'error' value.
//ASCII characters to our base 41 encoding
conversion_table[' '] = 0;
for (i = 'a'; i <= 'z'; i++)
conversion_table[i] = i - 'a' + 1; //goes 1 thru 26
conversion_table['#'] = 27;
conversion_table['.'] = 28;
conversion_table[','] = 29;
conversion_table['\''] = 30;
conversion_table['!'] = 31;
conversion_table['?'] = 32;
conversion_table['('] = 33;
conversion_table[')'] = 34;
conversion_table['-'] = 35;
conversion_table[':'] = 36;
conversion_table['$'] = 37;
conversion_table['/'] = 38;
conversion_table['&'] = 39;
conversion_table['\\'] = 40;
//Set up the inverse table
for (i = 0; i < 255; i++)
if (conversion_table[i] != -1)
inversion_table[conversion_table[i]] = i;
}
/*
* modular_exponentiation
*
* Quickly calculates num^n % mod
* Source: http://homepages.math.uic.edu/~leon/cs-mcs401-s08/handouts/fastexp.pdf
*
* returns: num^n % mod
*/
unsigned long long modular_exponentiation(unsigned int num, unsigned int n, unsigned int mod)
{
unsigned long long x = num;
unsigned long long y = num;
if (n & 1 == 0)
y = 1;
int np = n/2;
while (np > 0)
{
x = (x * x) % mod;
if (np & 1 == 1)
{
if (y == 1)
y = x;
else
y= (y * x) % mod;
}
np /= 2;
}
return y;
}
/*
* decrypt
*
* Decrypts the given encrypted string, then stores it at same location
*
* encrypted_string: Location of encrypted string. Used for storing decrypted
* string.
*
* returns:
* Length if no errors occur
* -1 if an error occurs
* -2 if malloc fails
*/
int decrypt(char* encrypted_string)
{
//Initialize our conversion arrays
if (!tables_initialized)
{
initialize_table();
tables_initialized = true;
}
int length = strlen(encrypted_string);
//length when we factor out the excess 8th characters
int true_length = strlen(encrypted_string) - (strlen(encrypted_string) / 8);
//we use an array to temporarily store the decrypted string
//in the event that we are unable to successfuly decrypt it
//so that we don't destroy the original data
char* intermediary = (char*)malloc(length);
if (intermediary == NULL)
{
return -2;
}
for (int i = 0, j = 0; i < true_length; i++, j++)
{
//Skip every 8th character in encrypted_string
if ((j + 1) % 8 == 0)
j++;
intermediary[i] = encrypted_string[j];
}
//Make sure remainder is clear.
for (int i = true_length; i < length; i++)
intermediary[i] = 0;
//Loop through in groups of 6
for (int i = 0; i < true_length; i += 6)
{
unsigned long long temp = 0;
for (int k = 0; k < 6 && i + k < true_length; k++)
{
int value = conversion_table[intermediary[i + k]];
if (value == -1)
{
free(intermediary);
return -1; //Undefined character in the encrypted text
}
temp += value * pow (41, 5 - k);
}
//Step 3
//M=C^d % n
//d=1921821779
//n=4294434817
temp = modular_exponentiation(temp, 1921821779, 4294434817);
for (int k = 0; k < 6 && i + k < true_length; k++)
{
int result = (temp / (long)pow(41, 5 - k)) % 41;
intermediary[i + k] = inversion_table[result];
}
}
//Copy our decrypted string to the given pointer
memcpy(encrypted_string, intermediary, length);
free(intermediary);
return strlen(encrypted_string);
}