forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar_Cipher.cpp
60 lines (60 loc) · 1.6 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
#include<iostream>
#include<string.h>
using namespace std;
int main() {
cout<<"Enter the message:\n";
char msg[100];
cin.getline(msg,100); //take the message as input
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key; //take the key as input
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1) //for encryption{
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//encrypt for lowercase letter
If (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
//encrypt for uppercase letter
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
printf("Encrypted message: %s", msg);
}
else if (choice == 2) { //for decryption
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//decrypt for lowercase letter
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
//decrypt for uppercase letter
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
cout << "Decrypted message: " << msg;
}
}