Wednesday, 16 July 2014

Is hacking always bad?

Initially, Hacking was used to describe an activity done as a hobby, usually in a sophisticated manner. It basically had no relations with doing anything with computers (Harvey, n.d.), and only after the discovery of computers, the term “Computer Hacking” came into use. In actual sense, computer hacking meant doing anything using the computer as a pastime, which included making computers do almost anything that they are normally not made for.

However, at present, people generally refer to computer hacking as a criminal offence that consists of activities like breaking into computer systems, stealing people’s or organizations’ data, and doing some sort of damage, using computers or computer-like devices. Quite easily, the list can go on. Historically, many illegal activities done using computers have been recorded as the acts of hacking. Hacking has mostly been perceived as doing illicit things with computers though the hackers (and their communities) have disagreed with all of it and labeled such people with unethical intents as “crackers” and/or “phreaks” (BBC News, 2000).

The Dining Philosophers Problem

A Solution: Breaking the Dependency” improves upon the Broken Solution”.

The Dining Philosophers problem is a classic concurrency or synchronization problem in Operating Systems. E. Dijkstra posed and solved this problem in 1965. In this problem, there are 5 philosophers around a dining table and 5 forks available for eating. The life of the philosophers consists of alternative period of eating and thinking, and eating needs two forks (left and right). When philosopher gets hungry, he/she tries to acquire two forks, eats for a while, and then puts down the forks and continues to think.


The broken solution is that when the philosopher is hungry, he/she picks up the left fork first and waits for right fork, when gets it eats for a while and put both forks back to the table (Arpaci-Dusseau & Arpaci-Dusseau, 2014). The problem with this solution, because of which it is called “broken”, is that if all philosophers pick up the forks to their left and wait for the ones on the right, nobody will get to eat (as all forks are already acquired) leading to a deadlock. This phenomenon can be said to be “Circular Wait”.

The solution “Breaking the Dependency” improves upon it by changing the way the forks are acquired by one philosopher. More specifically, Philosopher 4 (or any other philosopher) can be made to pick up the right fork first, instead of the one on the left (Arpaci-Dusseau & Arpaci-Dusseau, 2014).  
void getforks() {
if (p == 4) {
sem_wait(forks[right(p)]);
sem_wait(forks[left(p)]);
} else {
sem_wait(forks[left(p)]);
sem_wait(forks[right(p)]);
}
}

Friday, 9 May 2014

Java program for Hill Cipher

import javax.swing.JOptionPane;

/**
 *
 * @author dixit bhatta
 */

public class HillCipher {
    //the 3x3 key matrix for 3 characters at once
    public static int[][] keymat = new int[][]{
                { 1, 2, 1 },
                { 2, 3, 2 },
                { 2, 2, 1 },
            };
     public static int[][] invkeymat = new int[][]{
                { -1, 0, 1 },
                { 2, -1, 0 },
                { -2, 2, -1},
            };
    public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static void main(String[] args) {
        // TODO code application logic here
        String text,outtext ="";
        int ch, n;
        ch = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1 to Encrypt and 2 to Decrypt!"));
        text = JOptionPane.showInputDialog(null, "Enter plain/cipher text to encrypt?");
        text = text.toUpperCase();
        text = text.replaceAll("\\s",""); //removing spaces
        n = text.length() % 3;
        if(n!=0){
            for(int i = 1; i<= (3-n);i++){
                text+= 'X';
            }
        }
        System.out.println("Padded Text:" + text);
        char[] ptextchars = text.toCharArray();

Java program for Vigenere Cipher

import javax.swing.JOptionPane;

/**
 *
 * @author dixit bhatta
 */
public class Vigenere {
    public static String key = "ENIGMA"; //our key
    public static void main(String args[]){
        String text;
        int ch;
        ch = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1 to Encrypt and 2 to Decrypt!"));
        text = JOptionPane.showInputDialog(null, "Enter plain/cipher text to encrypt/decrypt?");
        text = text.toUpperCase();
        text = text.replaceAll("\\s",""); //removing spaces
        char[] ptextchars = text.toCharArray();
                switch(ch){
            case 1:
                    for(int i=0;i< text.length(); i++){
                        ptextchars[i] = encrypt(ptextchars[i],i);
                    }
                    break;
            case 2:
                    for(int i=0;i< text.length(); i++){
                        ptextchars[i] = decrypt(ptextchars[i],i);
                    }
                    break;
            default: System.out.println("Invalid Choice!");
        }
        System.out.println(ptextchars);
    }

    private static char encrypt(char c, int i) {
        int pos = (int)c % 65;
        pos = (((int)key.charAt(i%6))%65 + pos)%26;
        return (char)(pos+65);
    }

    private static char decrypt(char c, int i) {
        int pos = (int)c % 65;
        pos = (pos - (((int)key.charAt(i%6))%65))%26;
        if(pos+65 <65){ //adjustment for unstable indexes
            pos = pos + 26;
        }
        return (char)(pos+65);
    }

}

Java program for Playfair Cipher

import javax.swing.JOptionPane;

/**
 *
 * @author dixit bhatta
 */
public class PlayFair {
    //the key matrix
    public static char[][] keymat = new char[][]{
                { 'M', 'O', 'N', 'A', 'R' },
                { 'C', 'H', 'Y', 'B', 'D' },
                { 'E', 'F', 'G', 'I', 'K'},
                { 'L', 'P', 'Q', 'S', 'T'},
                { 'U', 'V', 'W', 'X', 'Z'}
            };
    public static String trans = "J"; //for translating J to I
    public static char filler = 'X'; //filler letter is X
    public static void main(String args[]){
        String text,outtext="";
        int ch;
        ch = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1 to Encrypt and 2 to Decrypt!"));
        text = JOptionPane.showInputDialog(null, "Enter plain/cipher text to encrypt/decrypt?");
        text = text.toUpperCase();
        text = text.replaceAll("\\s",""); //removing spaces
        text = text.replace(trans, "I"); //changing J with I
        text = text.replaceAll("([A-Z])\\1+","$1"+filler+"$1"); //handling repeated letters
        if(text.length() % 2 !=0)
            text+= filler;
        char[] ptextchars = text.toCharArray();
        System.out.println("Padded Text: "+text);
                switch(ch){
            case 1:
                    for(int i=0;i< text.length(); i+=2){
                        outtext += encrypt(ptextchars[i],ptextchars[i+1]);
                    }
                    break;
            case 2:
                    for(int i=0;i< text.length(); i+=2){
                        outtext += decrypt(ptextchars[i],ptextchars[i+1]);
                    }
                    break;
            default: System.out.println("Invalid Choice!");
        }
        System.out.println("Output: "+outtext);
    }

    private static String encrypt(char c1, char c2) {
        int[] posa = new int[2];
        int[] posb = new int[2];
        String frag = "";
        posa = search(c1);
        posb = search(c2);
        if(posa[0] == posb[0]){//same row
            c1 = keymat[posa[0]][(posa[1]+1)%5];
            c2 = keymat[posb[0]][(posb[1]+1)%5];
            frag = (""+c1+c2+"");
        }
        else if(posa[1] == posb[1]){ //same column
            c1 = keymat[(posa[0]+1)%5][posa[1]];
            c2 = keymat[(posb[0]+1)%5][posb[1]];
            frag = (""+c1+c2+"");
        }
        else{
            c1 = keymat[posb[0]][posa[1]];
            c2 = keymat[posa[0]][posb[1]];
            frag = (""+c1+c2+"");
        }
        return frag;
    }

    private static String decrypt(char c1, char c2) {
        int[] posa = new int[2];
        int[] posb = new int[2];
        String frag = "";
        posa = search(c1);
        posb = search(c2);
        if(posa[0] == posb[0]){//same row
            c1 = keymat[posa[0]][(posa[1]-1)%5];
            c2 = keymat[posb[0]][(posb[1]-1)%5];
            frag = (""+c1+c2+"");
        }
        else if(posa[1] == posb[1]){ //same column
            c1 = keymat[(posa[0]-1)%5][posa[1]];
            c2 = keymat[(posb[0]-1)%5][posb[1]];
            frag = (""+c1+c2+"");
        }
        else{
            c1 = keymat[posb[0]][posa[1]];
            c2 = keymat[posa[0]][posb[1]];
            frag = (""+c1+c2+"");
        }
        return frag;
    }

    private static int[] search(char c) {
        int i,j;
        int[] pos = new int[2];
        for (i = 0; i < 5; i++) {
            for (j = 0; j < 5; j++) {
                if (keymat[i][j] == c){
                    pos[0] = i;
                    pos[1] = j;
                    break;
                }
            }
        }
        return pos;
    }
   
   
}

Java program for Mono-alphabetic Substitution Cipher

import javax.swing.JOptionPane;

/**
 *
 * @author dixit bhatta
 */
public class Monoalphabetic {
    public static String key = "ISYVKJRUXEDZQMCTPLOFNBWGAH";
    public static void main(String[] args) {
        // TODO code application logic here
        String text;
        int ch;
        ch = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1 to Encrypt and 2 to Decrypt!"));
        text = JOptionPane.showInputDialog(null, "Enter plain/cipher text to encrypt?");
        text = text.toUpperCase();
        text = text.replaceAll("\\s",""); //removing spaces
        char[] ptextchars = text.toCharArray();
        switch(ch){
            case 1:
                    for(int i=0;i< text.length(); i++){
                        ptextchars[i] = encrypt(ptextchars[i]);
                    }
                    break;
            case 2:
                    for(int i=0;i< text.length(); i++){
                        ptextchars[i] = decrypt(ptextchars[i]);
                    }
                    break;
            default: System.out.println("Invalid Choice!");
        }
        System.out.println(ptextchars);
    }

        private static char encrypt(char a) {
            int pos = (int)a;
            pos = (pos % 91)- 65;
            a = key.charAt(pos);
            return a;
        }

        private static char decrypt(char a) {
            int ascii = 0;
            for(int i=0;i< key.length(); i++){
                if(key.charAt(i) == a)
                    ascii = i + 65;
            }
            return (char)ascii;
        }  
}

Java program for Caesar Cipher

import javax.swing.JOptionPane;

/**
 *
 * @author dixit bhatta
 */
public class Ceaser {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String text;
        int ch, factor;
        ch = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1 to Encrypt and 2 to Decrypt!"));
        text = JOptionPane.showInputDialog(null, "Enter plain/cipher text to encrypt?");
        factor = Integer.parseInt(JOptionPane.showInputDialog(null, "Shift characters by?"));
        text = text.toUpperCase();
        text = text.replaceAll("\\s",""); //removing spaces
        char[] ptextchars = text.toCharArray();
        switch(ch){
            case 1:
                    for(int i=0; i<text.length();i++){
                        ptextchars[i] = encrypt(text.charAt(i),i,factor);
                    }
                    break;
            case 2:
                    for(int i=0; i<text.length();i++){
                        ptextchars[i] = decrypt(text.charAt(i),i,factor);
                    }
                    break;
            default: System.out.println("Invalid Choice!");
        }
        System.out.println(ptextchars);
    }

    private static char encrypt(char a, int i, int f) {
        int ascii = (int)a;
        ascii = ((ascii + f ) % 90);
        if(ascii < 65){//adjustment for characters bigger than Z
            ascii = ascii + 65;
        }
            //moving using 3 characters
        a = (char) ascii;
        return a;
    }

    private static char decrypt(char a, int i, int f) {
        int ascii = (int)a;
        ascii = ((ascii - f ) % 90);
        if(ascii < 65){//adjustment for characters smaller than a
            ascii = ascii + 25;
        }          
        //moving using 3 characters
        a = (char) ascii;
        return a;
    }
}