Sunday, August 25, 2013

Vigenere Cipher Encryption in C++

//Vigenere Cipher
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;
#define siz 100
int P[siz], K[siz], C[siz];

int main()
{
    string plainText, key, cipherText;
    //freopen("input.txt", "r", stdin);
    while(getline(cin, plainText) && getline(cin, key)){
        cout << "Plain Text: " << plainText << endl;
        cout << "Key: " << key << endl;
        cout << "Cipher Text: ";
        int len = key.size();
        int j = 0;
        for(int i=0; i<plainText.size(); i++){
            if(isalpha(plainText[i])){
                P[i] = plainText[i] - 'a';
                K[j] = tolower(key[j % len]) - 'a';
                C[j] = (P[i] + K[j]) % 26;
                cout<<(char)toupper(C[j] + 'a')<<" ";
                j++;
            }
        }
        puts("");
    }
    return 0;
}

No comments:

Post a Comment