Vigenere Cipher

Below is my implementation of Vigenere Cipher algorithm, it simply takes a keyword and a string which needs to be encrypted and shows the encrypted version of that string. Also you can use the vigenere_decrypt function which takes an encrypted string and a keyword which was used to encrypt that string, provided the string must have been encrypted using the Vigenere Cipher only.
If there maybe some spacing issues you can remove unwanted spaces to get rid of it, cause I had to give them as the code wouldn't fit in the screen like that.
Off to Bed...Cya!
#include<bits/stdc++.h>
#define NO_OF_LETTERS 26
using namespace std;
/*
 *
 *             Vigenere Cipher!!
 *
 */
 
 /*  The below function will get an input 
 string from the user and will show it's 
 encrypted version using vigenere cipher
 */
void vigenere_decrypt(string s,string keyword);
void vigenere_cipher(){
    string keyword;
    string s;
    int itr=0;
    cout<<"This is vigenere cipher,\nEnter a" 
    "secret keyword for your encryption.\t";
    cin>>keyword;
    //keyword--->bar
    //Varying Keys;1,0,18
    cout<<endl;
    cout<<"Now enter the string which you" 
    "would like to encrypt\t";
    cin>>s;
    int keyword_size=keyword.size();
    for(int i=0;i<s.size();i++){
        //Do something, but what?
        //Encrypt the string.
        //How?
        char c=s[i];
        //Will only do something if char is alphabet
        if((c>='A' && c<='Z')||(c>='a' && c<='z')){
            if(isupper(c)){
                s[i]=((s[i]-'A')+keyword[itr])
                %NO_OF_LETTERS+'A';
            itr=(itr+1)%keyword_size;
            }else{
                s[i]=((s[i]-'a')+keyword[itr])
                %NO_OF_LETTERS+'a';
                itr=(itr+1)%keyword_size;
            }
        }
    }
    cout<<"The encrypted string is "<<s<<endl;
    // vigenere_decrypt(s,keyword);
} 

void vigenere_decrypt(string s,string keyword){
    int itr=0;
    int keyword_size=keyword.size();
    for(int i=0;i<s.size();i++){
        char c=s[i];
        if((c>='A' && c<='Z')||(c>='a' && c<='z')){
            if(isupper(c)){
                s[i]=((s[i]-'A')+
                (NO_OF_LETTERS-(keyword[itr]
                %NO_OF_LETTERS)))%NO_OF_LETTERS+'A';
                itr=(itr+1)%keyword_size;
            }else{
                s[i]=((s[i]-'a')+
                (NO_OF_LETTERS-(keyword[itr]
                %NO_OF_LETTERS)))%NO_OF_LETTERS+'a';
                itr=(itr+1)%keyword_size;    
            }
            
        }
    }
    cout<<"The Dencryted string is "<<s<<endl;
}

int main() {
    
    return 0;
}

Comments

Popular Posts