Vignere Cipher
This is an updated and my own version of Vignere Cipher.
#include <iostream>
#include<vector>
#define MAAX 99999
using namespace std;
vector<string> vignere_decrypt(){
vector<string> s;
string key;
cout<<"Enter the key: ";
cin>>key;
cout<<"Enter the string which needs to be Decrypted. then type xyz and press Enter: ";
for(int i=0;i<MAAX;i++){
string temp;
cin>>temp;
if(temp=="xyz"){
break;
}
s.push_back(temp);
}
cout<<endl;
int s_size=s.size();
int itr=0;
int key_size=key.size();
for(int i=0;i<s_size;i++){
int curr_size=s[i].size();
for(int j=0;j<curr_size;j++){
char elem=s[i][j];
int asc=int(elem);
int k=int(key[itr]);
k=(k>=65 && k<=90)?k-65:(k>=97 && k<=122)?k-97:1;
if((asc>=65 && asc<=90) || (asc>=97 && asc<=122)){
// Only if it is a capital or small letter
if(asc>=65 && asc<=90){
// Capital Letter
asc-=65;
asc=(asc+26-k)%26;
s[i][j]=char(asc+65);
itr=(itr+1)%key_size;
}
else if(asc>=97 && asc<=122){
// Small Letter
asc-=97;
asc=(asc+26-k)%26;
s[i][j]=char(asc+97);
itr=(itr+1)%key_size;
}
}
}
}
return s;
}
/* Vignere Cipher */
vector<string> vignere_encrypt(){
vector<string> s;
cout<<"Enter the key: ";
string key;
cin>>key;
cout<<"Enter the string which needs to be encrypted, then type xyz and press Enter: ";
for(int i=0;i<MAAX;i++){
string temp;
cin>>temp;
if(temp=="xyz"){
break;
}
s.push_back(temp);
}
cout<<endl;
int s_size=s.size();
int itr=0;
int key_size=key.size();
for(int i=0;i<s_size;i++){
int curr_size=s[i].size();
for(int j=0;j<curr_size;j++){
char elem=s[i][j];
int asc=int(elem);
int k=int(key[itr]);
k=(k>=65 && k<=90)?k-65:(k>=97 && k<=122)?k-97:1;
if((asc>=65 && asc<=90) || (asc>=97 && asc<=122)){
// Only if it is a capital or small letter
if(asc>=65 && asc<=90){
// Capital Letter
asc-=65;
asc=(asc+k)%26;
s[i][j]=char(asc+65);
itr=(itr+1)%key_size;
}
else if(asc>=97 && asc<=122){
// Small Letter
asc-=97;
asc=(asc+k)%26;
s[i][j]=char(asc+97);
itr=(itr+1)%key_size;
}
}
}
}
return s;
}
int main() {
/** Enter the string and then type xyz and enter **/
vector<string> s;
s=vignere_encrypt();
cout<<"After Encrypting:-\n";
for(string i:s){
cout<<i<<" ";
}cout<<endl;
return 0;
}
Comments
Post a Comment