Showing posts with label SECURITY. Show all posts
Showing posts with label SECURITY. Show all posts

Friday, October 25, 2013

TO IMPLEMENT CHECKSUM IN JAVA.

/*

TOPIC:TO IMPLEMENT CHECKSUM.

*/
import java.util.*;
class checksum
{
static int check[]=new int[4];
static int code[]=new int[12];
public static void add( int a[],int n)
{
int i,j=0;
for(i=0;i<4;i++)
{
if((a[i]==1 &&a[i+4]==1)||(a[i]==0&&a[i+4]==0))
{
check[i]=0;
}
else
{
check[i]=1;
}
}
for(i=0;i<n;i++)
{
code[i]=a[i];
}
while(j<4)
{
code[i]=check[j];
i++;j++;
}
}
public static void readd(int code[],int n)
{
int i,j=0,flag=0;
for(i=0;i<4;i++)
{
if((code[i]==1 && code[i+4]==1)||(code[i]==0&& code[i+4]==0))
check[i]=0;
else
check[i]=1;
}
i=i+4;
while(j<4)
{
if((code[i]==1&& check[j]==0)||(code[i]==0&&check[j]==1))
{
flag=1;
break;
}
i++;j++;
}
if(flag==1)
System.out.println("error present");
else
System.out.println("no error");
}

public static void main(String args[])
{
int a[]=new int[8];
Scanner sc=new Scanner(System.in);
System.out.println("enter 8bit word 1 by 1");
for(int i=0;i<8;i++)
{
a[i]=sc.nextInt();
}
add(a,a.length);
System.out.println("code sent");
for(int i=0;i<12;i++)
System.out.println("  "+code[i]);
System.out.println("\n\n enter recieved code 1 by 1");
for(int i=0;i<12;i++)
code[i]=sc.nextInt();
readd(code,code.length);
}
}
/*
OUTPUT:
enter 8bit word 1 by 1
1
1
1
1
1
1
1
1
code sent
  1
  1
  1
  1
  1
  1
  1
  1
  0
  0
  0
  0


 enter recieved code 1 by 1
1
1
1
1
1
1
1
1
1
0
0
0
error present
*/

TO IMPLEMENT HAMMING CODE IN JAVA.

/*

TITLE: TO IMPLEMENT HAMMING CODE IN JAVA.


*/

import java.io.*;
import java.util.*;
 class hamming1
 {
  public static void main(String args[])
{
  int i=0,j=0,d=0,sum=0;
  int a[]=new int[7];
  int b[]=new int[11];
  int c[]=new int[11];
  int p[]=new int[4];
  Scanner sr=new Scanner(System.in);
  System.out.println("Enter the data code");
  for(i=0;i<7;i++)
  {
   a[i]=sr.nextInt();
  }
  System.out.println("");
  System.out.println("data code is");
  for(i=0;i<7;i++)
  {
   System.out.println(a[i]);
  }
  for(j=0;j<11;j++)
  {
   b[j]=0;
  }
    if((a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6])%2==0)
    b[0]=0;
    else
    b[0]=1;
   
    if((a[0]+a[2]+a[3]+a[5]+a[6])%2==0)
    b[1]=0;
    else
    b[1]=1;
   
    if((a[1]+a[2]+a[3])%2==0)
    b[3]=0;
    else
    b[3]=1;
   
    if((a[4]+a[5]+a[6])%2==0)
    b[7]=0;
    else
    b[7]=1;
   
    System.out.println("");
    for(j=0,i=0;j<11;)
    {
     if(j==0||j==1||j==3||j==7)
     j++;
     else
      {
        b[j]=a[i];
         j++;
         i++;
      }
    }
    System.out.println("");
    System.out.println("Hamming code for even parity is:");
    for(j=0;j<11;j++)
    {
     System.out.println(b[j]);
    }
    System.out.println("");
    System.out.println("");
    System.out.println("Enter hamming code");
    for(i=0;i<11;i++)
    {
     c[i]=sr.nextInt();
    }
    p[0]=c[0];
    p[1]=c[1];
    p[2]=c[3];
    p[3]=c[7];
   
    if((((c[2]+c[4]+c[6]+c[8]+c[10])%2)==0 && p[0]==0||(((c[2]+c[4]+c[6]+c[8]+c[10])%2)!=0 && p[0] ==1)))
    p[0]=0;
    else
    p[0]=1;
   
   
    if((((c[2]+c[5]+c[6]+c[9]+c[10])%2)==0 && p[1]==0||(((c[2]+c[5]+c[6]+c[9]+c[10])%2)!=0 && p[1] ==1)))
    p[1]=0;
    else
    p[1]=1;
   
   
    if((((c[4]+c[5]+c[6])%2)==0 && p[2]==0||(((c[4]+c[5]+c[6])%2)!=0 && p[2] ==1)))
    p[2]=0;
    else
    p[2]=1;
   
   
    if((((c[8]+c[9]+c[10])%2)==0 && p[3]==0||(((c[8]+c[9]+c[10])%2)!=0 && p[3] ==1)))
    p[3]=0;
    else
    p[3]=1;
   
    for(i=3;i>=0;i--)
    {
     d=d+(p[i]*(int)Math.pow(2,i));
    }
    System.out.println("");
    if(d==0)
    System.out.println("The data code is correctly received");
    else
     System.out.println("The "+ d +"  bit is wrongly received");
    if(c[d-1]==0)
     c[d-1]=1;
      else
     c[d-1]=0;
       System.out.println("");
       System.out.println("correct hamming code is ");
      for(i=0;i<11;i++)
      {
          System.out.println(c[i]);
      }
     }
   }
   
/* OUTPUT
 Enter the data code
1
0
1
1
0
1
1

data code is
1
0
1
1
0
1
1


Hamming code for even parity is:
1
1
1
0
0
1
1
0
0
1
1


Enter hamming code
1
0
1
0
0
1
1
0
0
1
1

The 2  bit is wrongly received

correct hamming code is
1
1
1
0
0
1
1
0
0
1
1

*/

TO IMPLEMENT RSA ENCRYPTION ALGORITHM IN JAVA

/*

TITLE: TO IMPLEMENT RSA ENCRYPTION ALGORITHM IN JAVA
RSA.JAVA
*/
import java.util.*;
import java.io.*;
class RSA
{
         public static void main(String args[])
{
double i=0,e=0;

double p=7,q=11,n=p*q,fi=(p-1)*(q-1);
System.out.print("\nThe two prime numbers are :");
System.out.print("\np= "+p+"\tq= "+q);
System.out.print("\nn=p*q= "+n);
System.out.print("\nfi(n)= "+fi);
CoPrime c=new CoPrime();

for(i=2;!c.coprime(fi,i)&&i<fi;i++)
{
e=i;
}
e=13;
System.out.print("\n\nGenerated e= "+e);
double d=0;
while(!((e*d)%fi==1))
{
d++;
}
System.out.print("\n\nd="+d);

System.out.print("\n\n\nPublic Key= (" + e + " , " + n + ")");
System.out.print("\nPrivate Key= (" + d + ")");


System.out.print("\n\nEnter the plaintext: ");
Scanner in=new Scanner(System.in);
int plaintext=in.nextInt();
double C=Math.pow(plaintext,e)%n;
System.out.print("CipherText= "+C);


System.out.print("\n\n\nRECEIVER");

System.out.print("\nEnter the CipherText: ");
double Ci=in.nextDouble();
double m=1;
for(i=0;i<d;i++)
m=m*Ci%n;
m=m%n;
System.out.println("\nPlaintext= "+m);
}
}
class CoPrime
{
double hcf(double a,double h)
{
double temp;
while(true)
{
temp=a%h;
if(temp==0)
return h;
a=h;
h=temp;
}
}

boolean coprime(double c,double d)
{
double gcd=hcf(c,d);
if(gcd==1)
return true;

return false;
}
}
/*
OUTPUT:
The two prime numbers are :
p= 7.0  q= 11.0
n=p*q= 77.0
fi(n)= 60.0

Generated e= 13.0

d=37.0


Public Key= (13.0 , 77.0)
Private Key= (37.0)

Enter the plaintext: 9
CipherText= 58.0


RECEIVER
Enter the CipherText: 58

Plaintext= 9.0
*/