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

PRG 8, 9 - CN Ia

Uploaded by

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

PRG 8, 9 - CN Ia

Uploaded by

Manik Kalhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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.
client.java

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

class UdpClient {

public static void main(String[] args) {

try {

DatagramSocket datagramSocket = new DatagramSocket(1234);

byte[] buffer;

DatagramPacket datagramPacket;

System.out.println("Messages Received");

while (true) {

buffer = new byte[65535];

datagramPacket = new DatagramPacket(buffer, buffer.length);

datagramSocket.receive(datagramPacket);

String received = new String(buffer).trim();

System.out.println(received);

if (received.equalsIgnoreCase("exit")) {

datagramSocket.close();

break;

} catch (IOException e) {

e.printStackTrace();

}
Server.java

mport java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.util.Scanner;

class UdpServer {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

try {

DatagramSocket datagramSocket = new DatagramSocket();

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

String line;

byte[] buffer;

DatagramPacket datagramPacket;

System.out.println("Enter Messages to Send");

while (true) {

line = scanner.nextLine();

buffer = line.getBytes();

datagramPacket = new DatagramPacket(buffer, buffer.length, clientAddress, 1234);

datagramSocket.send(datagramPacket);

if (line.equalsIgnoreCase("exit")) {

datagramSocket.close();

break;

} catch (IOException e) {

e.printStackTrace();

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

import java.math.BigInteger;

import java.security.SecureRandom;

import java.util.Scanner;

class RSA {

static BigInteger p, q, n, phi_n, e, d;

static SecureRandom secureRandom;

static int bitLength = 64;

static String encrypt(String msg) {

return new BigInteger(msg.getBytes()).modPow(e, n).toString();

static String decrypt(String cipher) {

BigInteger bi = new BigInteger(cipher).modPow(d, n);

return new String(bi.toByteArray());

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

secureRandom = new SecureRandom();

p = BigInteger.probablePrime(bitLength, secureRandom);

q = BigInteger.probablePrime(bitLength, secureRandom);

n = p.multiply(q);
phi_n = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));

e = BigInteger.probablePrime(bitLength / 2, secureRandom);

while (e.gcd(phi_n).compareTo(BigInteger.ONE) != 0 && e.compareTo(phi_n) < 0) {

e = e.add(BigInteger.ONE);

d = e.modInverse(phi_n);

System.out.println("P assigned as: " + p);

System.out.println("Q assigned as: " + q);

System.out.println("N assigned as: " + n);

System.out.println("PHI_N assigned as: " + phi_n);

System.out.println("\nEnter Message");

String msg = scanner.nextLine();

String encryptedMessage = encrypt(msg);

System.out.println("Encrypted Message: " + encryptedMessage);

String decryptedMessage = decrypt(encryptedMessage);

System.out.println("Decrypted Message: " + decryptedMessage);

You might also like