Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Vigenere Cipher
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// C++ code to implement Vigenere Cipher
#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++)
{
// converting in range 0-25
char x = (str[i] + key[i]) %26;

// convert into alphabets(ASCII)
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++)
{
// converting in range 0-25
char x = (cipher_text[i] - key[i] + 26) %26;

// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}

// Driver program to test the above function
int main()
{
string str = "GEEKSFORGEEKS";
string keyword = "AYUSH";

string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);

cout << "Ciphertext : "
<< cipher_text << "\n";

cout << "Original/Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}