Showing posts with label Additive Cipher. Show all posts
Showing posts with label Additive Cipher. Show all posts

Saturday, July 27, 2013

Additive Cipher Decryption by brute force attack

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

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;
}