Friday 24 November 2017

C++ Program to Implement the Vigenere Cypher


Code:

#include    iostream
#include    string
using namespace std;

class Vigenere
{
    public:
        string key;

        Vigenere(string key)
        {
            for (int i = 0; i < key.size(); ++i)
            {
                if (key[i] >= 'A' && key[i] <= 'Z')
                    this->key += key[i];
                else if (key[i] >= 'a' && key[i] <= 'z')
                    this->key += key[i] + 'A' - 'a';
            }
        }

        string encrypt(string text)
        {
            string out;

            for (int i = 0, j = 0; i < text.length(); ++i)
            {
                char c = text[i];

                if (c >= 'a' && c <= 'z')
                    c += 'A' - 'a';
                else if (c < 'A' || c > 'Z')
                    continue;

                out += (c + key[j] - 2 * 'A') % 26 + 'A';
                j = (j + 1) % key.length();
            }

            return out;
        }

        string decrypt(string text)
        {
            string out;

            for (int i = 0, j = 0; i < text.length(); ++i)
            {
                char c = text[i];

                if (c >= 'a' && c <= 'z')
                    c += 'A' - 'a';
                else if (c < 'A' || c > 'Z')
                    continue;

                out += (c - key[j] + 26) % 26 + 'A';
                j = (j + 1) % key.length();
            }

            return out;
        }
};

int main()
{
    Vigenere cipher("VIGENERECIPHER");

    string original =
            "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
    string encrypted = cipher.encrypt(original);
    string decrypted = cipher.decrypt(encrypted);

    cout << original << endl;
    cout << "Encrypted: " << encrypted << endl;
    cout << "Decrypted: " << decrypted << endl;
}


Output:

Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

------------------
(program exited with code: 0)
Press return to continue


More C++ Programs:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...