CN 7,8,9 Programs.

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

7.Implementation of data encryption and decryption.

AIM:

Program:
Program to implement RSA(Asymmetric Key algorithm)
#include<stdio.h>
#include<string.h>
#include<math.h>
void main( )
{ int n,i,s,n2,k1,p,q,d,m1,e1,l5,z,p2[30],s1,c[30];
unsigned long int l3,m,l4,k2;
double l2,l1,l6;
float e,l;
char p1[30];
clrscr();
printf(“Enter two prime numbers p and q \n”);
scanf(“%d%d”,&p,&q);
n=p*q;
z=((p-1)*(q-1));
printf(“Enter the value of d:\n”);
scanf(“%d”,&d);
l=l*(abs(z));
e=ceil(l/d);
printf(“%d%d%f\n”,n,z,e);
printf(“ENCRYPTION-CIPHER TEXT”);
printf(“Enter the plaintext\n”);
scanf(“%s”,p1);
for(i=0;i<strlen(p1);i++)
{
s=p1[I]-64;
printf(“%d”,s);
e1=(int)e;
l1=pow(((double)s),((double)e1));
l4=(unsigned long int)l1;
k2=l4%n;
c[i]=(int)k2;
printf(“cipher:%d\n”,c[i]);
}
printf(“\n”);
for(i=0;i<strlen(p1);i++)
{
l2=(pow(((double)c[i]),((double)d)));
l5=(int)(l2/n);
l6=l5*33;
m=l2-l6;
m1=(int)m;
k1=m1+64;
printf(“%c\n”,k1);
}
getch( );
}
Output:
Enter two prime numbers p and q
3 11
Enter the value of d:
7
33 20 3.000000
ENCRYPTION CIPHER TEXT
Enter the plain text
SUZANNE
19cipher:28
21cipher:21
26cipher:20
1cipher:1
14cipher:5
14cipher:5
5cipher:26
S
U
Z
A
N
N
E
8.Write a program for congestion control using Leaky bucket algorithm.
AIM:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

#define NOF_PACKETS 10

int rand(int a)
{
int rn = (random() % 10) % a;
returnrn == 0 ? 1 :rn;
}

int main()
{
intpacket_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:
9.Write a program for frame sorting technique used in buffers.
AIM:
Program for frame sorting:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define FSize 3
typedef struct packet{int SeqNum; char Data[FSize+1];}packet;
struct packet *readdata, *transdata;
int divide(char *msg) {
int msglen, NoOfPacket, i, j;
msglen = strlen(msg);
NoOfPacket = msglen/FSize;
if ((msglen%FSize)!=0) NoOfPacket++;
readdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket);
for(i = 0; i<NoOfPacket; i++) {
readdata[i].SeqNum = i + 1;
for (j = 0; (j <FSize) && (*msg != '\0'); j++, msg++)
readdata[i].Data[j] = *msg;
readdata[i].Data[j] = '\0';
}
printf("\nThe Message has been divided as follows\n");
printf("\nPacket No. Data\n\n");
for (i = 0; i<NoOfPacket; i++)
printf(" %2d %s\n", readdata[i].SeqNum,
readdata[i].Data);
return NoOfPacket;
}
void shuffle(int NoOfPacket) {
int *Status;
int i, j, trans;
randomize();
Status=(int * )calloc(NoOfPacket, sizeof(int));
transdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket);
for (i = 0; i<NoOfPacket;) {
trans = rand()%NoOfPacket;
if (Status[trans]!=1) {
transdata[i].SeqNum = readdata[trans].SeqNum;
strcpy(transdata[i].Data, readdata[trans].Data);
i++; Status[trans] = 1;
}
}
free(Status);
}
void sortframes(int NoOfPacket) {
packet temp;
int i, j;
for (i = 0; i<NoOfPacket; i++)
for (j = 0; j <NoOfPacket – i-1; j++)
if (transdata[j].SeqNum>transdata[j + 1].SeqNum) {
temp.SeqNum = transdata[j].SeqNum;
strcpy(temp.Data, transdata[j].Data);
transdata[j].SeqNum = transdata[j + 1].SeqNum;
strcpy(transdata[j].Data, transdata[j + 1].Data);
transdata[j + 1].SeqNum = temp.SeqNum;
strcpy(transdata[j + 1].Data, temp.Data);
}}
void receive(int NoOfPacket) {
int i;
printf("\nPackets received in the following order\n");
for (i = 0; i<NoOfPacket; i++) printf("%4d", transdata[i].SeqNum);
sortframes(NoOfPacket);
printf("\n\nPackets in order after sorting..\n");
for (i = 0; i<NoOfPacket; i++) printf("%4d", transdata[i].SeqNum);
printf("\n\nMessage received is :\n");
for (i = 0; i<NoOfPacket; i++) printf("%s", transdata[i].Data);
}
void main() {
char *msg;
int NoOfPacket;
clrscr();
printf("\nEnter The message to be Transmitted :\n");
scanf("%[^\n]", msg);
NoOfPacket = divide(msg);
shuffle(NoOfPacket);
receive(NoOfPacket);
free(readdata);
free(transdata);
getch();
}
Output:
Enter The message to be Transmitted :
hi, it was nice meeting u on sunday
The Message has been divided as follows
Packet No. Data
1 hi,
2 it
3 wa
4sn
5 ice
6 me
7 eti
8 ng
9uo
10 n s
11 und
12 ay
Packets received in
the following
order 4 2 6 3 5 1 8
9 11 7 12 10
Packets in
order
after
sorting..
12345
6789
10 11 12
Message received is :
hi, it was nice meeting u on Sunday

You might also like