RSA & Leaky Bucket Programs
RSA & Leaky Bucket Programs
The RSA (Rivest–Shamir–Adleman) algorithm is one of the most widely used cryptographic
algorithms for secure data transmission, particularly in computer networks. It is an asymmetric
encryption algorithm, meaning it uses a pair of keys: one for encryption and one for decryption.
Key Concepts of RSA Algorithm
Asymmetric Key Cryptography: RSA uses two keys:
1. Public Key: Used to encrypt data; it is publicly available and can be shared with
anyone.
2. Private Key: Used to decrypt the data; it is kept secret and should only be
accessible to the owner.
How RSA Works in Computer Networks
In computer networks, RSA is mainly used for:
1. Secure data transmission: Ensuring that sensitive information like passwords, messages,
or credit card details remain secure during transit.
2. Digital signatures: Used to verify the authenticity of the sender and integrity of the
message.
3. Key exchange: RSA is often used to exchange a symmetric key, which will then be used
for faster encryption/decryption (since symmetric algorithms are faster than RSA).
Steps in RSA Algorithm
Program:
package rsa;
import java.util.*;
import java.io.*;
public class RSA {
static int gcd(int m,int n)
{
while(n!=0)
{
int r=m%n;
m=n;
n=r;
}
return m;
}
public static void main(String args[])
{
int p=0,q=0,n=0,e=0,d=0,phi=0;
int nummes[]=new int[100];
int encrypted[]=new int[100];
int decrypted[]=new int[100];
int i=0,j=0,nofelem=0;
Scanner sc=new Scanner(System.in);
String message ;
System.out.println("Enter the Message to be encrypted:");
message= sc.nextLine();
System.out.println("Enter value of p and q\n");
p=sc.nextInt();
q=sc.nextInt();
n=p*q;
phi=(p-1)*(q-1);
for(i=2;i<phi;i++)
if(gcd(i,phi)==1) break;
e=i;
for(i=2;i<phi;i++)
if((e*i)%phi==1)
break;
d=i;
for(i=0;i<message.length();i++)
{
char c = message.charAt(i);
int a =(int)c;
nummes[i]=c-96;
}
nofelem=message.length();
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
System.out.println("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
System.out.print(encrypted[i]);
System.out.print((char)(encrypted[i]+96));
}
for(i=0;i<nofelem;i++)
{
decrypted[i]=1;
for(j=0;j<d;j++)
decrypted[i]=(decrypted[i]*encrypted[i])%n;
}
System.out.println("\n Decrypted message\n ");
for(i=0;i<nofelem;i++)
System.out.print((char)(decrypted[i]+96));
return;
}
}
10. Develop a program for congestion control using a leaky bucket algorithm.
Program:
package leaky;
import java.util.*;
public class Leaky {
static int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
public static void main(String[] args)
{
int drop=0,psent,nsec,cap,pleft=0,i,process;
int inp[]=new int[25];
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Bucket Size\n");
cap= sc.nextInt();
System.out.println("Enter The Operation Rate\n");
process= sc.nextInt();
System.out.println("Enter The No. Of Seconds You Want To Simulate\n");
nsec=sc.nextInt();
for(i=0;i<nsec;i++)
{
System.out.print("Enter The Size Of The Packet Entering at "+( i+1 )+ " sec");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Recieved | Packet Sent | Packet Left | Packet Dropped|\n");
System.out.println("------------------------------------------------------------------------\n");
for(i=0;i<nsec;i++)
{
pleft+=inp[i];
if(pleft>cap)
{
drop=pleft-cap;
pleft=cap;
}
System.out.print(i+1);
System.out.print("\t\t"+inp[i]);
psent=min(pleft,process);
System.out.print("\t\t"+psent);
pleft=pleft-psent;
System.out.print("\t\t"+pleft);
System.out.print("\t\t"+drop);
drop=0;
System.out.println();
}
for(;pleft!=0;i++)
{
if(pleft>cap)
{
drop=pleft-cap;
pleft=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
psent=min(pleft,process);
System.out.print("\t\t"+psent);
pleft=pleft-psent;
System.out.print("\t\t"+pleft);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}