Saturday, July 27, 2013

Additive Cipher Encryption and Decryption

//Additive Cipher
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
int main()
{
    int key;
    string plainText, cipherText;
    //freopen("input.txt","r",stdin);
    while(cin>>key>>plainText){
        //encryption
        cipherText = "";
        int len = plainText.size();
        for(int i=0; i<len; i++){
            int x = (plainText[i] - 'a' + key) % 26;
            cipherText += toupper(x + 'a');
        }
        cout<<"Cipher Text: "<<cipherText<<endl;
        //decrption
        plainText = "";
        for(int i=0; i<len; i++){
            int x = (cipherText[i] - 'A' - key) % 26;
            if(x<0) x += 26;
            plainText += (x + 'a');
        }
        cout<<"Plain Text: "<<plainText<<endl;
    }
    return 0;
}

No comments:

Post a Comment