0% found this document useful (0 votes)
10 views4 pages

Lab 8-9

Uploaded by

likhith180li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Lab 8-9

Uploaded by

likhith180li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

Write a program on datagram socket for client/server to display the messages on client side, typed
at the server side.

Source Code:

Client:

import java.net.*;

import java.util.Scanner;

public class Dsender

public static void main(String args[])throws Exception

System.out.println("Sender");

DatagramSocket ds=new DatagramSocket();

Scanner scanner=new Scanner(System.in);

System.out.println("enter the message");

while(true)

String msg=scanner.nextLine();

InetAddress ip=InetAddress.getByName("127.0.0.1");

DatagramPacket dp=new DatagramPacket(msg.getBytes(),msg.length(),ip,3000);

ds.send(dp);

Server:

import java.util.Scanner;

import java.net.*;

public class Dreciever


{

public static void main(String[] args)throws Exception

byte[]buf=new byte[1024];

System.out.println("Reciever");

DatagramSocket ds=new DatagramSocket(3000);

while(true)

DatagramPacket dp=new DatagramPacket(buf,1024);

ds.receive(dp);

String msg=new String(dp.getData(),0,dp.getLength());

System.out.println(msg);

9. Write a program for simple RSA algorithm to encrypt and decrypt the data.

Source Code:

import java.util.*;

import java.math.*;

import java.util.Random;

import java.util.*;

class RSA2

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("enter two prime numbers");


int p=sc.nextInt();

int q=sc.nextInt();

int n=p*q;

int fi=(p-1)*(q-1);

System.out.println("enter the value for e");

int e=sc.nextInt();

while(true)

if(findGCD(fi,e)!=1)

System.out.println("enter a valid value for e");

e=sc.nextInt();

else

break;

int d=findD(e,fi);

System.out.println("d value is "+d);

System.out.println("enter plain text(number):");

int m=sc.nextInt();

int c=(int)(Math.pow(m,e)%n);

System.out.println("cipher text is:" +c);

int x=(int)(Math.pow(c,d)%n);

System.out.println(" "+x);

if(x==m)

System.out.println("input matched");

else

System.out.println("input mismatched");

}
static int findD(int e,int fi)

int b;

for(int i=1;i<=fi;i++)

b=(i*e)%fi;

if(b==1)

return i;

return 0;

static int findGCD(int number1,int number2)

if(number2==0)

return number1;

} return findGCD(number2,number1%number2);

}}

You might also like