Sunteți pe pagina 1din 17

ASSIGNMENT-I

REG.NO : 18CS506
Name : GAJALAKSHMI
COURSE : MCA / II YEAR
SUBJECT : COMPUTER NETWORKS
OUTPUT
ENCRYPTION AND DECRYPTION

CLIENT

import java.io.*;

import java.util.*;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.util.Scanner;

public class client

static Scanner sc=new Scanner(System.in);

static BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

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

DatagramSocket ds=new DatagramSocket();

InetAddress ip=InetAddress.getLocalHost();

String a="abcdefghijklmnopqrstuvwxyz";

String b="zyxwvutsrqponmlkjihgfedcba";

System.out.println("Enter any string:");

String str=br.readLine();
String decrypt=" ";

char c;

for(int i=0;i<str.length();i++)

c=str.charAt(i);

int j=a.indexOf(c);

decrypt=decrypt+b.charAt(j);

System.out.println("The encrypted data is: "+decrypt);

byte[] b1=decrypt.getBytes();

ds.send(new DatagramPacket(b1,decrypt.length(),ip,1234));

}
SERVER

import java.io.*;

import java.util.*;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.util.Scanner;

public class server

static Scanner sc=new Scanner(System.in);

static BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

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

DatagramSocket ds=new DatagramSocket(1234);

byte b[]=new byte[1600];

DatagramPacket p=new DatagramPacket(b,b.length);

ds.receive(p);

String mes=new String(p.getData(),0,p.getLength());

String mes1=mes.trim();

System.out.println("The received encrypted message is"+mes1);


String a="abcdefghijklmnopqrstuvwxyz";

String h="zyxwvutsrqponmlkjihgfedcba";

String decrypt=" ";

char c;

for(int i=0;i<mes1.length();i++)

c=mes1.charAt(i);

int j=h.indexOf(c);

decrypt=decrypt+a.charAt(j);

System.out.println("The Decrypted data is: "+decrypt);

}
FINDING PARITY USING CLIENT/SERVER
RECEIVER:

import java.io.*;

import java.net.*;

public class receiver

ServerSocket receiverSocket;

Socket connection = null;

ObjectOutputStream out;

ObjectInputStream in;

String message,parity1, parity2;

receiver(){}

void run()

try{

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

receiverSocket = new ServerSocket(2004, 10);

System.out.println("Waiting for connection");

connection = receiverSocket.accept();

System.out.println("Connection received from " +


connection.getInetAddress().getHostName());

out = new ObjectOutputStream(connection.getOutputStream());


out.flush();

in = new ObjectInputStream(connection.getInputStream());

sendMessage("Connection successful ...");

try{

message = (String)in.readObject();

System.out.println("client>" + message);

parity1=message.substring(0,1);

parity2=String.valueOf(parity(message.substring(1)));

message=(parity1.equals(parity2))?"correct data":"corrupted data";

sendMessage(message);

catch(ClassNotFoundException classnot){

System.err.println("Data received in unknown format");

//}while(!message.equals("bye"));

catch(IOException ioException){

ioException.printStackTrace();

finally{

try{

in.close();

out.close();
receiverSocket.close();

catch(IOException ioException){

ioException.printStackTrace();

int parity(String str){

int n=Integer.valueOf(str);

int len=str.length();

int sum=0;

for(int i=0;i<len;i++){

sum+=n%10;

n/=10;

return (sum%2);

void sendMessage(String msg)

try{

out.writeObject(msg);

out.flush();

System.out.println("server>" + msg);
}

catch(IOException ioException){

ioException.printStackTrace();

public static void main(String args[])

receiver server = new receiver();

while(true){

server.run();

}
Sender:

import java.io.*;

import java.net.*;

public class sender{

Socket requestSocket;

ObjectOutputStream out;

ObjectInputStream in;

String message;

boolean check=true;

sender(){}

void run()

try{

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

//1. creating a socket to connect to the server

requestSocket = new Socket("localhost", 2004);

System.out.println("Connected to localhost in port 2004");

//2. get Input and Output streams

out = new ObjectOutputStream(requestSocket.getOutputStream());

out.flush();

in = new ObjectInputStream(requestSocket.getInputStream());

//3: Communicating with the server


message = (String)in.readObject();

System.out.println("server>" + message);

do{

try{

System.out.println("Enter binary number to send:");

message = br.readLine();

if(checkbinary(message)){

check=false;

System.out.println("Parity bit >"+parity(message) );

message=String.valueOf(parity(message)).concat(message);

sendMessage(message);

}else{

System.out.println("Entered data is not in binary format.\n Re" );

catch(Exception classNot){

System.err.println("data received in unknown format");

}while(check);

try{

System.out.println("Data sent" );

message = (String)in.readObject();

System.out.println("server>" + message);
}catch(Exception e){}

catch(UnknownHostException unknownHost){

System.err.println("You are trying to connect to an unknown host!");

catch(IOException ioException){

ioException.printStackTrace();

catch(Exception e){}

finally{

//4: Closing connection

try{

in.close();

out.close();

requestSocket.close();

catch(IOException ioException){

ioException.printStackTrace();

boolean checkbinary(String str){

int n=Integer.valueOf(str);
int len=str.length();

int sum=0;

for(int i=0;i<len;i++){

sum=n%10;

if(sum>1) return false;

return true;

int parity(String str){

int n=Integer.valueOf(str);

int len=str.length();

int sum=0;

for(int i=0;i<len;i++){

sum+=n%10;

n/=10;

return (sum%2);

void sendMessage(String msg)

try{

out.writeObject(msg);

out.flush();
System.out.println("client>" + msg);

catch(IOException ioException){

ioException.printStackTrace();

public static void main(String args[])

sender client = new sender();

client.run();

}
OUTPUT:

S-ar putea să vă placă și