Wednesday, December 15, 2010

Challange-Response Authentication

import java.io.*;
import java.util.Random;
/**
 *
 * @author Sanket
 */
public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String originaluser = "sanket";

        f:
        for (int j = 0; j < 3; j++) {
            System.out.print("Eneter User Name : ");
            String username = br.readLine();

            if (originaluser.equals(username)) {
                System.out.println("Valid user name");
                Random rgenerator = new Random();
                int x = rgenerator.nextInt(100);
                int y = x * 10;

                System.out.println("Challenging number is : " + x);
                System.out.print("Enter Password : ");
                int pass = Integer.parseInt(br.readLine());

                f1:
                for (int z = 0; z < 3; z++) {
                    if (z == 1 || z == 2) {
                        System.out.println("Retype password : ");
                        pass = Integer.parseInt(br.readLine());
                    }
                    if (pass == y) {
                        System.out.println("valid user");
                        break f;
                    } else {
                        System.out.println("invalid password");
                        if (z == 2) {
                            System.out.println("access denied");
                            break f;
                        }
                    }
                }
            } else {
                System.out.println("Invalid user name");
                if (j == 2) {
                    System.out.println("access denied");
                }
            }
        }
    }
}

RSA Encryption/ Decryption Algorithm

import java.util.*;

/**
 *
 * @author Sanket
 */
class RSA
{
    public static void main(String args[])
    {       
        Scanner sr=new Scanner(System.in);
               
        int x=0, j=0, p=0,q=0;
        int a[]=new int[100];
       
        System.out.println("Enter two prime numbers : ");
        main:
        {
            first:
                for(j=0;j<6;j++)
                {
                    if(j>=5)
                    {
                        System.out.println("First learn prime number concept and then try again");
                        break main;
                    }

                    System.out.print("Enter P : ");
                    p=sr.nextInt();
                   
                    x=0;
                    for(int i=1;i<=p;i++)
                    {
                        if(p%i==0)
                        {
                            a[x]=i;
                            if(i<p)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(p+" is not a prime number, Enter P again");
                            continue first;
                        }
                        continue first;
                    }
                   
                    if(x==1)
                        break first;
                }
               
            j=0;
            second:
                for(j=0;j<=6;j++)
                {
                    if(j>=5)
                    {
                        System.out.println("First learn prime number concept and then try again");
                        break main;
                    }
                   
                    System.out.print("Enter Q : ");
                    q=sr.nextInt();
                   
                    x=0;
                    for(int i=1;i<=q;i++)
                    {
                        if(q%i==0)
                        {
                            a[x]=i;
                            if(i<q)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(q+" is not a prime number, Enter Q again");
                            continue second;
                        }
                        continue second;
                    }
                   
                    if(x==1)
                        break second;
                }
   

            int n=p*q;
            int phi=(p-1)*(q-1);

            j=0;
            int e=0;
           
            third:
                for(j=0;j<6;j++)
                {
                    if(j==5)
                    {
                        System.out.println("First learn basic math & Prime number Concept and then try again");
                        break main;
                    }
                   
                    System.out.print("Enter value for E between 1 and "+phi+" and prime number : ");   
                    e=sr.nextInt();
                       
                    x=0;
                    for(int i=1;i<=p;i++)
                    {
                        if(e%i==0)
                        {
                            a[x]=i;
                            if(i<e)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(e+" is not a prime number, Enter E again");
                            continue third;
                        }
                    }
                    else
                    {
                        if(x==1)   
                            if(e>1&&e<phi)
                            {
                                break third;
                            }
                            else
                            {
                                System.out.println(e+" is not between 1 and "+phi);
                                continue third;
                            }
                    }
                }
           
            int d1=0;
            four:   
                for(int i=1;i<e;i++)
                {
                    d1=((phi*i)+1);
                    if(d1%e==0)
                    {
                        break four;
                    }
                }
           
            int d=d1/e;
           
            System.out.print("\nPublic key is : { "+n+", "+e+" } \n");
            System.out.print("Private key is : { "+n+", "+d+" } \n\n");
           
            System.out.print("Enter message to encrypt : ");
            int msg=sr.nextInt();

            int c=1, m=1;

            for(int i=0;i<e;i++)
            {
                c=c*msg;
                c=c%n;
            }
            System.out.println("\nEncrypted message is : "+c);

            for(int w=0;w<d;w++)
            {
                m=m*c;
                m=m%n;
            }
            System.out.println("Decrypted message is : "+m);
        }
    }
}

Scramble Game


import java.io.*;

/**
 *
 * @author Sanket
 */
class Scramble
{
                public static void main(String args[])throws IOException
                {
                               
                                String str1, str2;
                               

                                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                                System.out.println("Enter first string : ");
                                str1=br.readLine();

                                System.out.println("Enter second string : ");
                                str2=br.readLine();

                                char sak1[]=str1.toCharArray();
                                char sak2[]=str2.toCharArray();

                                if(str1.length()<str2.length()||str1.length()==0||str2.length()==0||str1==null||str2==null)
                                                System.out.println("not Scramble");
                                else
                                {
                                                int c=0;
                                                for(int i=0; i<str2.length();i++)
                                                {
                                                                for(int j=0;j<str1.length();j++)
                                                                {
                                                                                if(sak2[i]==sak1[j]&&sak1[j]!=0)
                                                                                {
                                                                                                sak1[j]=0;
                                                                                                c++;
                                                                                }
                                                                }
                                                }
                               
                                                if(c==str2.length())
                                                                System.out.println("Scramble");
                                                else
                                                                System.out.println("not Scramble");
                                }
                }
}

Run up and down test


import java.io.*;

class RunUpAndDown {

    void ruab(double seq[], double n, double critival) {
        double count = 1;                           // total runs
        int i = 0;

        f:
        while (i < n - 1) {

            int j = 0;
            while (seq[i] < seq[i + 1]) {
                i++;
                j++;

                if (i == n - 1) {
                    break f;
                }
            }

            if (j > 0) {
                count++;
            }

            while (seq[i] > seq[i + 1]) {
                i++;

                if (i == n - 1) {
                    break f;
                }
            }

            count++;
        }

        //System.out.println("Count is : " + count);

        double mean = ((2 * n) - 1) / 3.0;                      //formulea for mean
        double varsq = ((16 * n) - 29) / 90;                    //formulea for variance
        double var = Math.sqrt(varsq);                    // square root of variance
        double rup = (count - mean) / var;

        System.out.println(rup);

        if (critival > rup && rup > -critival) {
            System.out.println("Randomness of given numbers accepted");
        } else {
            System.out.println("Randomness of given numbers rejected");
        }
    }
}

/**
 *
 * @author Sanket
 */
public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter total number in sequence : ");
        int n = Integer.parseInt(br.readLine());


        double seq[] = new double[n];
        System.out.println("Enter numbers in sequences : ");
        for (int i = 0; i < n; i++) {
            seq[i] = Double.parseDouble(br.readLine());
        }

        double p = (double) n;

        System.out.println("Enter Critical Value : ");
        double cv = Double.parseDouble(br.readLine());

        RunUpAndDown r = new RunUpAndDown();
        r.ruab(seq, p, cv);

    }
}

Ceaser Cipher

import java.io.*;

/**
 *
 * @author Sanket
 */
class CeaserCipher
{
    public static void main(String args[])throws IOException
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String arr="abcdefghijklmnopqrstuvwxyz";
        char arr1[]=arr.toCharArray();
       
        System.out.println("Enter message to send : ");
        String plain=br.readLine();
        char plainarr[]=plain.toCharArray();
       
        System.out.println("Enter key to encrypt message : ");
        int n=Integer.parseInt(br.readLine());
       
        char c='a';
        char carr[]=new char[100];
        System.out.println("\nEncrypted message is : ");
        for(int j=0;j<plain.length();j++)
        {
            for(int i=0;i<arr.length();i++)
            {
                if(plainarr[j]==arr1[i])
                {
                    int s=arr.indexOf(arr1[i]);
                    s+=n;
                    c=arr.charAt(s);                   
                    System.out.print(c);
                }
            }
            carr[j]=c;
        }
       
        System.out.println("\nDecrypted message is : ");
       
        for(int m=0;m<plain.length();m++)
        {
            for(int r=0;r<arr.length();r++)
            {
                if(carr[m]==arr1[r])
                {
                    int q=arr.indexOf(arr1[r]);
                    q-=n;
                    char f=arr.charAt(q);
                    System.out.print(f);
                }
            }
        }   

    }
}

KS-test

import java.io.*;

/**
 *
 * @author Sanket
 */
class KSTest {

    void kst(double num[], double dalpha) {
        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                if (num[i] > num[j]) {
                    double temp;
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }

        double d1[] = new double[num.length];
        double d2[] = new double[num.length];
        double n = (double) num.length;

        for (int i = 0; i < num.length; i++) {
            d1[i] = ((i + 1) / n) - num[i];
        }

        for (int i = 0; i < num.length; i++) {
            d2[i] = (num[i] - (i) / n);
        }

        double d1max = d1[0];
        for (int i = 0; i < num.length; i++) {
            if (d1max <= d1[i]) {
                d1max = d1[i];
            }
        }

        System.out.println("D+ = " + d1max);

        double d2max = d2[0];
        for (int i = 0; i < num.length; i++) {
            if (d2max <= d2[i]) {
                d2max = d2[i];
            }
        }

        System.out.println("D- = " + d2max);

        double dplus = d1max;
        double dminus = d2max;
        double d;

        if (dplus > dminus) {
            d = dplus;
            System.out.println("D = " + d);
        } else {
            d = dminus;
            System.out.println("D = " + d);
        }

        if (dalpha > d) {
            System.out.println("Given numbers uniformity accepted");
        } else {
            System.out.println("Given numbers uniformity rejected");
        }

    }
}

public class Main {


    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter total numbers : ");
        int n = Integer.parseInt(br.readLine());

        double a[] = new double[n];

        System.out.println("Enter numbers : ");
        for (int i = 0; i < n; i++) {
            a[i] = Double.parseDouble(br.readLine());
        }

        double da;

        System.out.println("Enter critcal value : ");
        da = Double.parseDouble(br.readLine());

        KSTest ks = new KSTest();
        ks.kst(a, da);

    }
}