Showing posts with label ENCRYPTION. Show all posts
Showing posts with label ENCRYPTION. Show all posts

Tuesday, April 1, 2014

TO STUDY HASH FUNCTIONS IN JAVA


package hashfn;
import java.util.*;
import java.security.MessageDigest;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        Scanner sc=new Scanner(System.in);
        String s;
            System.out.println("Enter the string");
            s=sc.nextLine();
            MessageDigest md=MessageDigest.getInstance("MD5");
            byte[] databytes=new byte[1024];
            databytes=s.getBytes();
            md.update(databytes);
            byte[] mdbytes=md.digest();
            StringBuffer hexString = new StringBuffer();
for (int i=0;i<mdbytes.length;i++) {
  String hex=Integer.toHexString(0xff & mdbytes[i]);
  if(hex.length()==1) hexString.append('0');
  hexString.append(hex);
}
            System.out.println(hexString);
    }

}

TO IMPLEMENT GENERAL CAESAR CIPHER IN JAVA

import java.util.*;

public class gencaesar
{
public static void main(String[] args) throws Exception
{
String input,output;
String a,b;
Scanner sc= new Scanner(System.in);

System.out.println("Enter the message");
input=sc.nextLine();
char array[]=input.toCharArray();
int key,i=0;
System.out.println("Enter the key");
key=sc.nextInt();
//adding the key
for(i=0;i<array.length;i++)
{
array[i]+=key;
if(array[i]>=90)
{
array[i]-=26;
}
if(array[i]>=129)
{
array[i]-=64;
}

}
//printing output
System.out.println("General Caesar Cipher: \n");
for(i=0;i<array.length;i++)
{
System.out.print(array[i]);

}
System.out.println();

//decyption
for(i=0;i<array.length;i++)
{
array[i]-=key;
if(array[i]>=129)
{
array[i]-=64;
}
if(array[i]>='Z' && array[i]<='a')
{
array[i]-=26;
}

}

//printing output
System.out.println("DECRYPTED PLAIN: \n");
for(i=0;i<array.length;i++)
{
//System.out.print(array[i]);

}
System.out.println(input);

//PROGRAM IS CODED BY 6483
}
}
/*
bash-3.00$ javac gencaesar.java
Enter the message
bash-3.00$ javac gencaesar.java
bash-3.00$ java gencaesar
Enter the message
xyz
Enter the key
5
General Caesar Cipher:

cde
DECRYPTED PLAIN:

xyz

*/

Friday, October 25, 2013

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
*/