0% found this document useful (0 votes)
5 views5 pages

Leaky Bucket and Data Encryption

Uploaded by

Avyuktha Raju
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)
5 views5 pages

Leaky Bucket and Data Encryption

Uploaded by

Avyuktha Raju
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/ 5

Program to implementation of Caesar Cypher

Algorithm to encrypt and decrypt the string


Description:

Encryption is a way of scrambling data so that only authorized parties can understand the information. In
technical terms, it is the process of converting plaintext to ciphertext.

Decrytion is a process of converting ciphertext to plaintext.

Encryption and decryption requires the use of a cryptographic key: a set of mathematical values that both
the sender and the recipient of an encrypted message agree on.

The Caesar Cipher technique is one of the earliest and simplest method of encryption technique. It’s simply
a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of
positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C,
and so on. The method is apparently named after Julius Caesar, who apparently used it to communicate with
his officials.
Thus to cipher a given text we need an integer value, known as shift which indicates the number of position
each letter of the text has been moved down

Algorithm:

Encryption of a letter by a shift n can be described mathematically as.

Decryptio of a letter described mathematically as

Program take

Program

#include <stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
int i, x;
char str[100];
printf("\nPlease choose following options:\n");
printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);

//using switch case statements


switch(x)
{
case 1:
printf("\nPlease enter a string:\t");
//fgets(str,100,stdin);
scanf("%s",str);
int length=strlen(str);
for (int i=0;i<length;i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(str[i]))
str[i]= (str[i]+3-65)%26 +65;

// Encrypt Lowercase letters


else
str[i]= (str[i]+3-97)%26 +97;
}
//for(i = 0; (i < 100 && str[i] != '\0'); i++)
// str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value

printf("\nEncrypted string: %s\n", str);


break;

case 2:

printf("\nPlease enter a string:\t");


//fgets(str,100,stdin);
scanf("%s",str);
length=strlen(str);
for (int i=0;i<length;i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(str[i]))
str[i]= (str[i]-3-65)%26 +65; ////the key for decryption is 3 that is substracted to ASCII value
// Encrypt Lowercase letters
else
str[i]= (str[i]-3-97)%26 +97;
}
printf("\nDecrypted string: %s\n", str);
break;
default:
printf("\nError\n");
}
return 0;
}

Program to implement Leaky Bucket algorithm


for traffic shaping
Aim: To Write C Program to implement Leaky bucket algorithm for Congestion control.

Description:

Traffic Shaping is a mechanism to control the amount and the rate of the traffic sent to the network. This
approach of congestion management is called Traffic shaping. Traffic shaping helps to regulate rate of data
transmission and reduces congestion..
There are 2 types of traffic shaping algorithms:
1. Leaky Bucket
2. Token Bucket
In networking, a technique called leaky bucket can smooth out bursty traffic. Bursty chunks are stored in the
bucket and sent out at an average rate.
Algorithm:

1. Start
2. Set the bucket size or the buffer size.
3. Set the output rate.
4. Transmit the packets such that there is no overflow.
5. Repeat the process of transmission until all packets are transmitted. (Reject packets where its size
is greater than the bucket size)
6. Stop

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
{
int rn = (random() % 10) % a;
return rn == 0 ? 1 : rn;
}

int main()
{
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
scanf("%d", &b_size);

for(i = 0; i<NOF_PACKETS; ++i)


{
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-PACKET
REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else
{
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rand(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0;
else
op = o_rate, p_sz_rm -= o_rate;
printf("\nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
}
}
}
}
}
Output:
packet[0]:30 bytes
packet[1]:10 bytes
packet[2]:10 bytes
packet[3]:50 bytes
packet[4]:30 bytes
packet[5]:50 bytes
packet[6]:10 bytes
packet[7]:20 bytes
packet[8]:30 bytes
packet[9]:10 bytes
Enter the Output rate:5
Enter the Bucket Size:30

Incoming Packet size: 30


Bytes remaining to Transmit: 30
Time left for transmission: 20 units
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 25
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 20

Incoming Packet size: 10


Bytes remaining to Transmit: 30
Time left for transmission: 30 units
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 25
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 20
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 15

Incoming Packet size: 10


Bytes remaining to Transmit: 25
Time left for transmission: 10 units
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 20

Incoming packet size (50bytes) is Greater than bucket capacity (30bytes)-PACKET REJECTED

Bucket capacity exceeded-PACKETS REJECTED!!

Incoming packet size (50bytes) is Greater than bucket capacity (30bytes)-PACKET REJECTED

Incoming Packet size: 10


Bytes remaining to Transmit: 30
Time left for transmission: 10 units
Packet of size 5 Transmitted----Bytes Remaining to Transmit: 25

Bucket capacity exceeded-PACKETS REJECTED!!

Bucket capacity exceeded-PACKETS REJECTED!!

Bucket capacity exceeded-PACKETS REJECTED!!

You might also like