forked from abhimanyuiscoding/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar Cypher
99 lines (80 loc) · 1.85 KB
/
Caesar Cypher
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
#include<iostream>
#include<string.h>
using namespace std;
void encryptyfunc()
{
char text[100];
char temp;
int i, key;
cout <<"Enter a message to encrypt: " <<endl;
cin >>text;
cout <<"Enter key: " <<endl;
cin >> key;
for(i = 0; text[i] != '\0'; ++i){
temp = text[i];
//If the message to be encypted is in lower case
if(temp >= 'a' && temp <= 'z'){
temp = temp + key;
if(temp > 'z'){
temp = temp - 'z' + 'a' - 1;
}
text[i] = temp;
}
//If the message to be encypted is in upper case
else if(temp >= 'A' && temp <= 'Z'){
temp = temp + key;
if(temp > 'Z'){
temp = temp - 'Z' + 'A' - 1;
}
text[i] = temp;
}
}
cout << "Encrypted message:" << text <<endl;
}
void decryptfunc()
{
char text[100];
char temp;
int i, key;
cout <<"Enter a message to decrypt: " <<endl;
cin >>text;
cout <<"Enter key: " <<endl;
cin >> key;
for(i = 0; text[i] != '\0'; ++i){
temp = text[i];
//If the message to be decypted is in lower case.
if(temp >= 'a' && temp <= 'z'){
temp = temp - key;
if(temp < 'a'){
temp = temp + 'z' - 'a' + 1;
}
text[i] = temp;
}
//If the message to be decypted is in upper case.
else if(temp >= 'A' && temp <= 'Z'){
temp = temp - key;
if(temp < 'A'){
temp = temp + 'Z' - 'A' + 1;
}
text[i] = temp;
}
}
cout << "Decrypted message:" <<text << endl;
}
int main()
{
int choice;
cout << "Choose any one !" <<endl;
cout << "1.Encryption \t 2.Decryption"<<endl;
cin >> choice;
switch(choice)
{
case 1: encryptyfunc();
break;
case 2: decryptfunc();
break;
default:cout << "Please select valid option";
break;
}
return 0;
}