Skip to content

Instantly share code, notes, and snippets.

@thasan3003
Created December 2, 2018 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thasan3003/5b51a3b364ced41f8a454a2a62bc188e to your computer and use it in GitHub Desktop.
Save thasan3003/5b51a3b364ced41f8a454a2a62bc188e to your computer and use it in GitHub Desktop.
Caesar Cipher is the most common encryption decryption algorithm in cryptography. It is a type of substitution cipher technique.
/*
Md Tahmid Hasan
CSE, BSMRSTU
tahmidhasan3003
*/
#include<bits/stdc++.h>
using namespace std;
string encDec(string inputText, int key, bool encryption)
{
string outputText;
char temp;
for(int i=0; i<inputText.length(); i++)
{
temp = toupper(inputText[i]);
if(temp > 64 && temp < 91)
outputText = encryption ? outputText=outputText + (char)(65+(temp-65+key)%26) : outputText=outputText + (char)(65+(26+temp-65-key)%26);
else
outputText += temp;
}
return outputText;
}
int main()
{
string inputText;
int key=0, op;
while(1)
{
cout<<"Choose any...\n1. Key Setup\n2. Encryption\n3. Decryption\n4. Exit"<<endl;
cin>>op;
getchar();
switch (op)
{
case 1:
cout<<"Input Key:"<<endl;
cin>>key;
getchar();
cout<<endl<<"Your new key is\t: "<<key<<endl;
key %= 26;
break;
case 2:
cout<<endl<<"Input original message:"<<endl;
getline(cin, inputText);
cout<<endl<<"Plain Text\t: "<<inputText<<endl;
cout<<"Cipher Text\t: "<<encDec(inputText, key, true)<<endl;
cout<<"Used key \t: "<<key<<endl;
break;
case 3:
cout<<endl<<"Input encrypted message:"<<endl;
getline(cin, inputText);
cout<<endl<<"Cipher Text\t: "<<inputText<<endl;
cout<<"Plain Text\t: "<<encDec(inputText, key, false)<<endl;
cout<<"Used key \t: "<<key<<endl;
break;
case 4:
exit(0);
default:
cout<<"wrong input. Please input again..."<<endl;
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment