Showing posts with label encryption. Show all posts
Showing posts with label encryption. Show all posts

Tuesday, 27 November 2012

Diffie Hellman (Java)

Diffie Hellman: Receiver

import java.io.*;
import java.net.*;
import java.util.*;

public class DeffieHellmanRcv {

DatagramSocket theSocket = null;

int serverPort = 9999;
Scanner sc;
int y,g,p,r2;

DatagramPacket theRecievedPacket;
DatagramPacket theSendPacket;
InetAddress clientAddress;
int clientPort;
byte[] outBuffer;
byte[] inBuffer;

public DeffieHellmanRcv()
{
try {
// create the server UDP end point
theSocket = new DatagramSocket(serverPort);

System.out.println("Bob");
} catch (SocketException ExceSocket)
{
System.out.println("Socket creation error : "+ ExceSocket.getMessage());
}
}

public void keyGen()
{

sc=new Scanner(System.in);

System.out.print("Enter p=");
p=sc.nextInt();

System.out.print("Enter g=");
g=sc.nextInt();


System.out.print("Enter y=");
y=sc.nextInt();

r2= (int)(Math.pow(g,y)) % p;
//System.out.println("R2= "+r2);

}

public void send()
{
try
{
String message =r2+"";
System.out.println("Message Send:"+message);
//System.out.println("Message  sent: "+message);
outBuffer=message.getBytes();

System.out.println(message);
// send some data to the client
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
theSocket.send(theSendPacket);
}

catch (Exception e)
{
System.out.println("Error with client request : "+e);
}
// close the server socket


}

public void receive()
{


// create some space for the text to send and recieve data
outBuffer = new byte[500];
inBuffer = new byte[50];

try
{
// create a place for the client to send data too
theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);

// wait for a client to request a connection
theSocket.receive(theRecievedPacket);
//System.out.println("Client connected");

// get the client details
clientAddress = theRecievedPacket.getAddress();
clientPort = theRecievedPacket.getPort();



String message = new String(theRecievedPacket.getData(),0,theRecievedPacket.getLength());


String sendData = message+"";
System.out.println("Message Recieved: "+message);
System.out.println(sendData);
outBuffer = sendData.getBytes();


int k2=(int)(Math.pow((Integer.parseInt(message)),y))%p;


System.out.println("\n\nk2= "+k2);

}
catch(Exception e)
{
System.out.println("E2"+e);
}

}





public static void main(String[] args)
{
DeffieHellmanRcv dr = new DeffieHellmanRcv();
dr.keyGen();
dr.receive();
dr.send();
}
}

Download the code from here

Knapsack Cryptosystem: Decryption (Java)


Knapsack Cryptosystem: Decryption


import java.util.*;
class KSD
{
public int inverse(int a,int n)
{
int r1,r2,r,q,t1,t2,t;
r1=n;r2=a;
t1=0;t2=1;
while(r2>0)
{
q=r1/r2;
   r=r1-q*r2;
   t=t1-q*t2;

   r1=r2;r2=r;
   t1=t2;t2=t;
}
 if(r1==1)
  return t1;
 else
  return 0;
}
public void invKnapSack(int a[],int s,int p[])
{
int s2,sum=0;
int x[]=new int[7];
int t[]=new int[7];

System.out.println("\n\n i\ta\ts\tx\ts\n");
for(int h=6;h>=0;h--)
{
if(s>=a[h])
{
x[h]=1;
}
s2=s-(a[h]*x[h]);
System.out.println(h+"\t"+a[h]+"\t"+s+"\t"+x[h]+"\t"+s2);
s=s2;
}

/*for(int h=0;h<7;h++)
System.out.print(x[h]);*/
for(int h=0;h<7;h++)
t[h]=x[p[h]-1];
System.out.println();
for(int h=0;h<7;h++)
{
int temp;
if(t[h]==1)
{
temp=(int)(Math.round((Math.pow(2,7-h-1))));
sum+=temp;
}
}
char ch=(char)sum;
System.out.println("The plain text is: "+ch);
}
}
class KnapSackDecrypt
{
public static void main(String args[])
{
int b,n,r;
int a[]=new int[7];
int p[]=new int[7];
Scanner se=new Scanner(System.in);
System.out.println("Enter the super increasing tuple: ");
for(int h=0;h<7;h++)
a[h]=se.nextInt();
System.out.println("Enter the permutation tuple: ");
for(int h=0;h<7;h++)
p[h]=se.nextInt();
System.out.println("Enter the cipher text (sum) : ");
b=se.nextInt();
se.nextLine();
System.out.println("Enter the N: ");
n=se.nextInt();
se.nextLine();
System.out.println("Enter the R: ");
r=se.nextInt();
//System.out.println(b);
KSD ksd=new KSD();
int s=ksd.inverse(r,n);
s*=b;
s%=n;
System.out.println(s);
ksd.invKnapSack(a,s,p);
}

}

Download the code from here

Multiplicative Cipher (C++)

Multiplicative Cipher : Encryption

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class MulCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
};
void MulCiph :: gettext()
{
  cout<<"\nEnter the string: ";
  cin>>str;
}
void MulCiph :: getkey()
{
  cout<<"\nEnter the multiplicative cipher key: ";
  cin>>key;
}
void MulCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    char c=(((toascii(str[i])-97)*key)%26)+97;
    cout<<str[i]<<"\t"<<((toascii(str[i])-97)*key)%26<<"\t"<<c<<endl;
  }
}
void main()
{
  clrscr();
  MulCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download the code from here

Additive Cipher (C++)


Encryption using Additive Cipher in C++


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class AddCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
};
void AddCiph :: gettext()
{
  cout<<"\nEnter the plain string: ";
  cin>>str;
}
void AddCiph :: getkey()
{
  cout<<"\nEnter the additive cipher key: ";
  cin>>key;
}
void AddCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    char c=(((toascii(str[i])-97)+key)%26)+97;
    cout<<str[i]<<"\t"<<((toascii(str[i])-97)+key)%26<<"\t"<<c<<endl;
  }
}
void main()
{
  clrscr();
  AddCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download code from here


Thursday, 8 November 2012

Knapsack Cryptosystem: Encryption (Java)


import java.util.*;
class KnapSack
{
public void calculateKnapSack(char ch,int b[],int p[],int n,int r)
{
int pt=ch;
int sum=0;
String str=Integer.toBinaryString(pt);
int t[]=new int[7];
int a[]=new int[7];
int x[]=new int[7];
for(int h=0;h<7;h++)
t[h]=(b[h]*r)%n;
System.out.print("\nTemparory Tuple:\t\t\t");
for(int h=0;h<7;h++)
System.out.print(t[h]+",");
for(int h=0;h<7;h++)
a[h]=t[p[h]-1];
System.out.print("\nAfter Permuting Temporary Tuple:\t");
for(int h=0;h<7;h++)
System.out.print(a[h]+",");
for(int h=0;h<7;h++)
x[h]=str.charAt(h)-48;
System.out.print("\nAfter (b*n)%r:\t\t\t\t");
for(int h=0;h<7;h++)
System.out.print(a[h]*x[h]+",");
for(int h=0;h<7;h++)
sum+=a[h]*x[h];
System.out.println("\n\nThe Sum and the Cipher Text is:"+sum);
System.out.println();
}
public static void main(String args[])
{
String ch;
Scanner se=new Scanner(System.in);
System.out.println("\nEnter the plain text: ");
ch=se.nextLine();
char pt=ch.charAt(0);
int b[]=new int[7];
int p[]=new int[7];
System.out.print("Enter the value of N: \n");
int n=se.nextInt();
System.out.print("Enter the superincreasing tuple upto 7: \n");
for(int i=0;i<7;i++)
b[i]=se.nextInt();
System.out.print("Enter the permutation tuple upto 7: \n");
for(int i=0;i<7;i++)
p[i]=se.nextInt();
KnapSack l=new KnapSack();
l.calculateKnapSack(pt,b,p,n,41);
}
}



Download the code from here