-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDESEncryption.c
More file actions
50 lines (48 loc) · 1.34 KB
/
DESEncryption.c
File metadata and controls
50 lines (48 loc) · 1.34 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void DES_Shift(char* data, int mode) {
int len = strlen(data), shift;
printf("Enter the key: ");
scanf("%d", &shift);
shift = (mode == 1) ? shift : 26 - shift;
for (int i = 0; i < len; i++) {
if (data[i] >= 'a' && data[i] <= 'z') {
data[i] = 'a' + (data[i] - 'a' + shift) % 26;
} else if (data[i] >= 'A' && data[i] <= 'Z') {
data[i] = 'A' + (data[i] - 'A' + shift) % 26;
}
}
}
int main() {
printf("1. Encrypt\n2. Decrypt\n3. Exit\n");
char* data = NULL;
int ch,size,shift;
size_t buffer = 0;
while (1) {
printf("Enter your choice: ");
scanf("%d", &ch);
getchar();
switch (ch) {
case 1:
printf("Enter the message to encrypt: ");
getline(&data, &buffer, stdin);
DES_Shift(data, 1);
printf("Encrypted message: %s\n", data);
break;
case 2:
printf("Enter the message to decrypt: ");
getline(&data, &buffer, stdin);
DES_Shift(data, 0);
printf("Decrypted message: %s\n", data);
break;
case 3:
exit(0);
default:
printf("Invalid choice\n");
break;
} free(data);
data = NULL;
}
return 0;
}