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

Refer Manual Refer Manual: Computer Networks Lab (CBCS 2018 Aug-Dec)

This document contains code snippets for several computer network programs: 1. CRC and Bellman-Ford algorithms (referenced but not shown) 2. A TCP/IP client-server program to transfer a file contents 3. A basic client-server socket programming example 4. RSA encryption/decryption program to encrypt and decrypt strings 5. Leaky bucket algorithm is referenced but code not shown

Uploaded by

Farhiz Pasha
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)
52 views4 pages

Refer Manual Refer Manual: Computer Networks Lab (CBCS 2018 Aug-Dec)

This document contains code snippets for several computer network programs: 1. CRC and Bellman-Ford algorithms (referenced but not shown) 2. A TCP/IP client-server program to transfer a file contents 3. A basic client-server socket programming example 4. RSA encryption/decryption program to encrypt and decrypt strings 5. Leaky bucket algorithm is referenced but code not shown

Uploaded by

Farhiz Pasha
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

Computer Networks Lab (CBCS 2018 Aug-Dec)

PART-B Programs

1.CRC (refer manual)

2.BELLMAN FORD (refer manual)

3.TCP/IP
import java.net.*;
import java.io.*;
public class ContentsClient
{
public static void main( String args[ ] ) throws Exception {Socket sock =
new Socket( "localhost",
12004);
System.out.print("Enter the file name\n");
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
String fname = keyRead.readLine();
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);
InputStream istream = sock.getInputStream();
BufferedReader socketRead = new BufferedReader(new
InputStreamReader(istream));
String str;
while((str = socketRead.readLine()) != null)
{
System.out.println(str);
}
pwrite.close();
socketRead.close();
keyRead.close(); s
}

import java.net.*;
import java.io.*;
public class ContentsServer
{
public static void main(String args[]) throws Exception
{
ServerSocket sersock = new ServerSocket(12004);
SJCIT
System.out.println("Server ready for connection");
Socket sock = sersock.accept();
System.out.println("Connection is successful and wating for chatting");
InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new
InputStreamReader(istream));
String fname = fileRead.readLine( );
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
while((str = contentRead.readLine()) != null)
{
pwrite.println(str);
}
sock.close(); sersock.close();
pwrite.close(); fileRead.close(); contentRead.close();
}
}

4.CLIENT SERVER (refer manual)


5.RSA PROGRAM
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Random;

public class RSA


{
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private Random r;

public RSA()
{
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
SJCIT
N = p.multiply(q);
phi =
p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength / 2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 &&
e.compareTo(phi) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}

public RSA(BigInteger e, BigInteger d, BigInteger N)


{
this.e = e;
this.d = d;
this.N = N;
}

@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException
{
RSA rsa = new RSA();
//BufferedReader in =
// new BufferedReader(new
InputStreamReader(System.in));
DataInputStream in = new DataInputStream(System.in);
String teststring;
System.out.println("Enter the plain text:");
teststring = in.readLine();
System.out.println("Encrypting String: " + teststring);
System.out.println("String in Bytes: "
+ bytesToString(teststring.getBytes()));
// encrypt
byte[] encrypted = rsa.encrypt(teststring.getBytes());

// decrypt
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Decrypting Bytes: " +
bytesToString(decrypted));
System.out.println("Decrypted String: " + new
String(decrypted));
}

private static String bytesToString(byte[] encrypted)


{
String test = "";
for (byte b : encrypted)

SJCIT
{
test += Byte.toString(b);
}
return test;
}

// Encrypt message
public byte[] encrypt(byte[] message)
{
return (new BigInteger(message)).modPow(e, N).toByteArray();
}

// Decrypt message
public byte[] decrypt(byte[] message)
{
return (new BigInteger(message)).modPow(d, N).toByteArray();
}
}
Output:
Enter the plain text:
sjcit
Encrypting String: sjcit
String in Bytes: 11510699105116
Decrypting Bytes: 11510699105116
Decrypted String: sjcit

6.LEAKY BUCKET (refer manual)

SJCIT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy