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

Simple statistical Attacks

//simple statistical Attacks
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;

map <char, int> mp;
map <char, int>::iterator it;

int main()
{
    string plainText, cipherText;
    int key;
    char secret;
    //freopen("input.txt","r",stdin);
    while(cin>>cipherText){
        int len = cipherText.size();

        for(int i=0; i<len; i++){
            mp[tolower(cipherText[i])]++;
        }

        int mx = 0;
        for(it = mp.begin(); it != mp.end(); it++){
            if((*it).second > mx) {
                mx = (*it).second;
                secret = (*it).first;
            }
        }
        key = abs((secret - 'a') - 4);
        cout<<"key: "<<key<<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;
        //clearing
        mp.clear();
    }
    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;
}

Tuesday, July 16, 2013

How to find inverse Matrix using C++

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define siz 100

int det(int a[siz][siz], int n){
    int res = 0;
    int b[siz][siz];

    if(n==1) return a[1][1];
    else if(n==2) return a[1][1]*a[2][2]-a[1][2]*a[2][1];
    else {
        int t, s;
        for(int k=1; k<=n; k++){
            t = s = 1;
            for(int i=2;i<=n;i++){
                for(int j=1;j<=n;j++)
                    if(j==k) continue;
                    else b[t][s++] = a[i][j];
                t++;
                s=1;
            }
            res += (pow(-1,1+k)*a[1][k]*det(b,n-1));
        }
        return res;
    }
}

int main()
{
    int n;
    int a[siz][siz], transpose[siz][siz];
    freopen("input.txt","r",stdin);
    while(cin>>n){

        //taking input
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cin>>a[i][j];

        puts("Input Matrix:");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
            puts("");
        }
        int res = det(a, n);
        puts("Determinant:");
        cout<<res<<endl;

        //transpose
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                transpose[i][j] = a[j][i];

        //transpose output
        puts("Matrix Transpose:");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cout<<transpose[i][j]<<" ";
            puts("");
        }

        int t, s, l = 0;
        int result[siz];
        for(int k=1;k<=n;k++){
            for(int m=1; m<=n;m++){
                t = s = 1;
                int component[siz][siz];
                for(int i=1;i<=n;i++){
                    if(i==k) continue;
                    for(int j=1;j<=n;j++){
                        if(m==j) continue;
                        else component[t][s++] = transpose[i][j];
                    }
                    s = 1;
                    t++;
                }
                result[l++] = det(component, n-1);
            }
        }

        l = 0;
        double inverse[siz][siz];
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i%2==0){
                    if(j%2==0) {
                        inverse[i][j] = result[l++];
                    } else {
                        inverse[i][j] = result[l++]*(-1);
                    }
                } else {
                    if(j%2==0) {
                        inverse[i][j] = result[l++]*(-1);
                    } else {
                        inverse[i][j] = result[l++];
                    }
                }
                inverse[i][j] /= res;
            }
        }

        //inverse output
        puts("Matrix Inverse:");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cout<<inverse[i][j]<<" ";
            puts("");
        }
    }
    return 0;
}

How to find Addition Table using C++

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++){
            int x = i;
            for(int j=0;j<n;j++){
                cout<<x++%10<<" ";
            }
            puts("");
        }
    }
    return 0;
}

How to find Multiplication table using C++

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++){
            int x = i;
            for(int j=0;j<n-1;j++){
                if(j == 0) printf("0 ");
                cout<<(x)%n<<" ";
                x+=i;
            }
            puts("");
        }
    }
    return 0;
}

How to find Multiplicative Inverse using C++

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int q, r1, r2, r, t1, t2, t;
    while(cin>>r1>>r2){
        t1 = 0;
        t2 = 1;

        while(r2 != 0){
            q = r1 / r2;
            r = r1 % r2;
            t = t1 - t2*q;
            cout<<q<<" "<<r1<<" "<<r2<<" "<<r<<" "<<t1<<" "<<t2<<" "<<t<<endl;
            r1 = r2;
            r2 = r;
            t1 = t2;
            t2 = t;

        }

        if(r1 != 1) {
            puts("There is no multiplicative inverse");
        } else {
            cout<<t1<<endl;
        }
    }
    return 0;
}