-
Notifications
You must be signed in to change notification settings - Fork 3
/
Caesar Cipher.cpp
77 lines (66 loc) · 1.84 KB
/
Caesar Cipher.cpp
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
#include<bits/stdc++.h>
using namespace std;
char cptext[100];
int Input(char [], int );
void encrypt(char [], int );
void decrypt(char [], int );
int main() {
int key;
char str[100];
int choice;
while(true) {
printf("\n\t\t(1) Encrypt");
printf("\n\t\t(2) Decrypt");
printf("\n\t\t(3) Exit");
printf("\n\t\tEnter your choice: "); scanf("%d", &choice);
getchar();
switch(choice) {
case 1:
key = Input(str, key);
encrypt(str, key); printf("\n\t\tEncrypt text: %s\n", cptext);
break;
case 2:
key = Input(str, key);
decrypt(str, key); printf("\n\t\tDecrypt text: %s\n", cptext);
break;
default:
exit(0);
break;
}
}
return 0;
}
int Input(char str[], int key) {
printf("\n\t\tEnter plain text: "); gets(str);
printf("\t\tEnter key: "); scanf("%d", &key);
return key;
}
void encrypt(char str[], int key) {
int in = 0;
for(int i=0; str[i]; i++) {
if ((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) {
char ch = (str[i]>='a' && str[i]<='z') ? 'a' : 'A';
int ind = str[i] - ch;
ind = (ind + key) % 26;
cptext[in++] = ind + ch;
}
else if (str[i] == ' ') cptext[in++] = str[i];
}
cptext[in] = '\0';
}
void decrypt(char str[], int key) {
int in = 0;
for(int i=0; str[i]; i++) {
if ((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) {
char ch = (str[i]>='a' && str[i]<='z') ? 'a' : 'A';
int ind = str[i] - ch;
for(int k=0; k<key; k++) {
if (ind-1 < 0) ind = 25;
else ind = ind - 1;
}
cptext[in++] = ind + ch;
}
else if (str[i] == ' ') cptext[in++] = str[i];
}
cptext[in] = '\0';
}