-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_decrypt.c
More file actions
311 lines (214 loc) · 6.6 KB
/
encrypt_decrypt.c
File metadata and controls
311 lines (214 loc) · 6.6 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <string.h>
#define MAX_BUF 256
#define IV 0b10110001
#define KEY 0b11001011
#define CTR 0b00110101
void encode(unsigned char*, unsigned char*, int);
void decode(unsigned char*, unsigned char*, int);
unsigned char processCtr(unsigned char, unsigned char);
unsigned char encryptByte(unsigned char, unsigned char, unsigned char);
unsigned char decryptByte(unsigned char, unsigned char, unsigned char);
unsigned char getBit(unsigned char, int);
unsigned char setBit(unsigned char, int);
unsigned char clearBit(unsigned char, int);
int main() {
char str[8];
int choice;
int count;
unsigned char ct[MAX_BUF];
unsigned char pt[MAX_BUF];
unsigned char text[MAX_BUF];
printf("\nYou may:\n");
printf(" (1) Encrypt a message \n");
printf(" (2) Decrypt a message \n");
printf(" (0) Exit\n");
printf("\n what is your selection: ");
fgets(str, sizeof(str), stdin);
sscanf(str, "%d", &choice);
if (choice == 0)
return 0;
switch (choice) {
case 1:
printf("\nEncrypt: ");
fgets(text, sizeof(text), stdin);
for(count = 0; text[count] != '\0'; ++count){
encode(text, ct, sizeof(text));
}
for(int i = 0; i< count; ++i){
printf("%d ", ct[i]);
}
break;
case 2:
int numBytes = 0;
int encryptNum;
for (int i = 0; i < sizeof(pt) / sizeof(pt[0]); ++i) {
scanf("%d", &encryptNum);
if (encryptNum == -1) {
break;
}
ct[i] = encryptNum;
numBytes++;
}
decode(ct, pt, numBytes);
printf("\n%s", pt);
break;
}
return(0);
}
/*
Function: getBit
Purpose: retrieve value of bit at specified position
in: character from which a bit will be returned
in: position of bit to be returned
return: value of bit n in character c (0 or 1)
*/
unsigned char getBit(unsigned char c, int n) {
unsigned char dec = ((c & (1 << n)) >> n);
return dec;
}
/*
Function: setBit
Purpose: set specified bit to 1
in: character in which a bit will be set to 1
in: position of bit to be set to 1
return: new value of character c with bit n set to 1
*/
unsigned char setBit(unsigned char c, int n) {
unsigned char dec = c |(1 << n);
return dec;
}
/*
Function: clearBit
Purpose: set specified bit to 0
in: character in which a bit will be set to 0
in: position of bit to be set to 0
return: new value of character c with bit n set to 0
*/
unsigned char clearBit(unsigned char c, int n) {
unsigned char dec = c & (~(1 << n));
return dec;
}
/*Function: processCtr
Purpose: processes the given counter value using the given key, as part of the encryption algorithm
in/out: the counter value is copied into a temporary counter which gets updated
in: key value is xor with the current bits of the counter
return: temporary counter value as the updated counter
*/
unsigned char processCtr(unsigned char ctr, unsigned char key) {
unsigned char tmp = ctr;
int val;
if(tmp % 2 == 0) {
for(int x = 0; x <= 7; x+=2){
val = getBit(ctr,x) ^ getBit(key,x);
if(val == 1){
tmp = setBit(tmp,x);
}
else{
tmp = clearBit(tmp,x);
}
}
}
else{
for(int x = 1; x <= 7; x+=2){
val = getBit(ctr,x) ^ getBit(key,x);
if(val == 1) {
tmp = setBit(tmp,x);
}
else{
tmp = clearBit(tmp,x);
}
}
}
return tmp;
}
/*Function: encryptByte
Purpose: encrypts the given plaintext byte pt
in: plaintext byte pt
in: using the counter value ctr and the
in: previous byte of the ciphertext prev
return: corresponding encrypted ciphertext byte
*/
unsigned char encryptByte(unsigned char pt, unsigned char ctr, unsigned char prev) {
unsigned char tmpBytes = 0b00000000;
for (int x = 0; x <= 7; ++x){
if(getBit(ctr,x) == 1){
if((getBit(pt,x) ^ getBit(prev,x)) == 0){
tmpBytes = clearBit(tmpBytes,x);
}
else{
tmpBytes = setBit(tmpBytes,x);
}
}
else{
if((getBit(pt,x) ^ getBit(prev,7-x)) == 0){
tmpBytes = clearBit(tmpBytes,x);
}
else{
tmpBytes = setBit(tmpBytes,x);
}
}
}
return tmpBytes;
}
/*Function: encode
Purpose: encrypts each plaintext character into its corresponding ciphertext byte
in: takes an array of plaintext characters stored in parameter pt, which contains numBytes bytes
in: stores the encrypted byte into the ciphertext array ct
*/
void encode(unsigned char *pt, unsigned char* ct, int numBytes) {
unsigned char ctr = processCtr(CTR, KEY);
unsigned char prev = IV;
for (int i = 0; i < numBytes; ++i){
ct[i] = encryptByte(pt[i], ctr, prev);
prev = ct[i];
ctr++;
ctr = processCtr(ctr, KEY);
}
}
/*Function: decryptByte
Purpose: decrypts each ciphertext byte into its corresponding plaintext byte
in: takes an array of ciphertext bytes stored in parameter ct, which contains numBytes bytes.
return: the corresponding decrypted plaintext byte
*/
unsigned char decryptByte(unsigned char ct, unsigned char ctr, unsigned char prev) {
int bit;
unsigned char tmpByte = 0b00000000;
for (int i = 0; i < 8; ++i) {
if (getBit(ctr, i) == 0) {
bit = getBit(ct, i) ^ getBit(prev, 7-i);
if (bit == 0) {
tmpByte = clearBit(tmpByte, i);
}
else if (bit == 1) {
tmpByte = setBit(tmpByte, i);
}
}
if (getBit(ctr, i) == 1) {
bit = getBit(ct, i) ^ getBit(prev, i);
if (bit == 0) {
tmpByte = clearBit(tmpByte, i);
}
else if (bit == 1) {
tmpByte = setBit(tmpByte, i);
}
}
}
return tmpByte;
}
/*Function: decode
Purpose: decrypts each ciphertext byte into its corresponding plaintext byte
in: takes an array of ciphertext bytes stored in parameter ct
in: numBytes bytes
in: stores the decrypted byte into the plaintext array pt
*/
void decode(unsigned char *ct, unsigned char *pt, int numBytes) {
unsigned char counter = processCtr(CTR, KEY);
unsigned char previous = IV;
for (int i = 0; i < numBytes; ++i) {
pt[i] = decryptByte(ct[i], counter, previous);
previous = ct[i];
counter++;
counter = processCtr(counter, KEY);
}
}