Name - Pallavi Bharti Roll No - 18141112 Experiment 3: Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Name – Pallavi Bharti

Roll No - 18141112
Experiment 3
Implement Poly-alphabetic Vigenere Cipher using any programming language

Program –

#include<bits/stdc++.h>
using namespace std;
string generateKey(string str, string key)
{
int x = str.size();

for (int i = 0; ; i++)


{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}

string cipherText(string str, string key)


{
string cipher_text;

for (int i = 0; i < str.size(); i++)


{
char x = (str[i] + key[i]) %26;
x += 'A';

cipher_text.push_back(x);
}
return cipher_text;
}

string originalText(string cipher_text, string key)


{
string orig_text;

for (int i = 0 ; i < cipher_text.size(); i++)


{
char x = (cipher_text[i] - key[i] + 26) %26;

x += 'A';
orig_text.push_back(x);
}
return orig_text;
}

int main()
{
string str;
string keyword;
cout<<"Original Message: ";
cin>>str;
cout<<"Key: ";
cin>>keyword;
string key = generateKey(str, keyword);
cout<<"Generated Key: "<<key;
string cipher_text = cipherText(str, key);

cout << "\nCiphertext : "


<< cipher_text << "\n";

cout << "Decrypted Text : "


<< originalText(cipher_text, key);
return 0;
}

Output –

You might also like