Skip to content

Instantly share code, notes, and snippets.

@SaicharanKandukuri
Last active June 2, 2022 02:52
Show Gist options
  • Save SaicharanKandukuri/ce0939c2e02077071da3326ba60ff32e to your computer and use it in GitHub Desktop.
Save SaicharanKandukuri/ce0939c2e02077071da3326ba60ff32e to your computer and use it in GitHub Desktop.
My version of ceaser cypher in c++ with just alphabets with both upper & lower case
#include <iostream>
/*
(C) SaicharanKandukuri <[email protected]> 2022
please try not to copy paste this in your class 👌
*/
#define SHIFT 3 // hardoced ( XD )
using namespace std;
string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // ALPHABETS for encryption
string LALPHABETS = "abcdefghijklmnopqrstuvwxyz"; // LALPHABETS for encryption
string targetCharset;
int getindexof(char charc)
{
if (ALPHABET.find(charc) != string::npos)
{
::targetCharset = ALPHABET;
return ALPHABET.find(charc);
}
else if (LALPHABETS.find(charc) != string::npos)
{
::targetCharset = LALPHABETS;
return LALPHABETS.find(charc);
}
else
{
return -1;
}
}
string encrypt(string plaintext)
{
string ciphertext = "";
for (int i = 0; i < plaintext.length(); i++)
{
int index = (getindexof(plaintext[i]) + SHIFT) % 26;
ciphertext += ::targetCharset[index];
}
return ciphertext;
}
string decrypt(string ciphertext)
{
string plaintext = "";
for (int i = 0; i < ciphertext.length(); i++)
{
int index = (getindexof(ciphertext[i]) - SHIFT) % 26;
plaintext += ::targetCharset[index];
}
return plaintext;
}
// command line interface
int main()
{
/*
just to display usability of functions
try to use cmd line way instead for better experience
*/
string plaintext, ciphertext;
cout << "Enter plaintext: ";
cin >> plaintext;
ciphertext = encrypt(plaintext);
cout << "Encrypted text: " << ciphertext << endl;
cout << "Decrypted text: " << decrypt(ciphertext) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment