-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMonoalphabetic Cipher.cpp
93 lines (76 loc) · 1.84 KB
/
Monoalphabetic 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Monoalphabetic Cipher
*
* author: dipta
* created: 05-Oct-2018 00:20:22
*
*/
#include<bits/stdc++.h>
using namespace std;
char cptext[100];
char KEYen[] = "abcdefghijklmnopqrstuvwxyz";
char KEYde[] = "apfcsvmniehjybogzqrtxwlkud";
void input(char []);
void encrypt(char []);
void decrypt(char []);
int main() {
char str[100];
printf("\n\t\tMonoalphabetic Cipher Algorithm");
printf("\n\t\t-------------------------------\n");
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:
input(str);
encrypt(str); printf("\n\t\tEncrypt text: %s\n", cptext);
break;
case 2:
input(str);
decrypt(str); printf("\n\t\tDecrypt text: %s\n", cptext);
break;
case 3:
exit(0);
break;
default:
printf("\n\t\t---> Invalid Choice!\n\n");
break;
}
}
return 0;
}
void input(char str[]) {
printf("\n\t\tEnter plain text: "); gets(str);
}
void encrypt(char str[]) {
int len = strlen(str);
strlwr(str);
for (int i=0; i<len; i++) {
if (str[i]>='a' && str[i]<='z') {
for (int j=0; j<26; j++) {
if (str[i] == KEYen[j])
cptext[i] = KEYde[j];
}
}
else cptext[i] = str[i];
}
cptext[len] = '\0';
}
void decrypt(char str[]) {
int len = strlen(str);
strlwr(str);
for (int i=0; i<len; i++) {
if (str[i]>='a' && str[i]<='z') {
for (int j=0; j<26; j++) {
if (str[i] == KEYde[j])
cptext[i] = KEYen[j];
}
}
else cptext[i] = str[i];
}
cptext[len] = '\0';
}