0% found this document useful (0 votes)
60 views7 pages

Practical: 8: Aim: - Implement RSA Encryption and Decryption Algorithm

This document describes an RSA encryption and decryption algorithm. It includes code to: 1) Get prime numbers p and q from the user and calculate n and t 2) Calculate possible values of the encryption key e and decryption key d 3) Encrypt the message by exponentiating each character by e modulo n 4) Decrypt the encrypted message by exponentiating each character by d modulo n. The code is tested by encrypting and decrypting a message and displaying the results.

Uploaded by

hello
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)
60 views7 pages

Practical: 8: Aim: - Implement RSA Encryption and Decryption Algorithm

This document describes an RSA encryption and decryption algorithm. It includes code to: 1) Get prime numbers p and q from the user and calculate n and t 2) Calculate possible values of the encryption key e and decryption key d 3) Encrypt the message by exponentiating each character by e modulo n 4) Decrypt the encrypted message by exponentiating each character by d modulo n. The code is tested by encrypting and decrypting a message and displaying the results.

Uploaded by

hello
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/ 7

Information And Network Securities[2170709] Practical :- 8

Practical: 8
Aim: - Implement RSA encryption and decryption algorithm.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();

int main()
{
printf("\n Enter first prime number:-\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{
printf("\n Wrong input.\n");

161040107027(Patel Janki) Page 1


Information And Network Securities[2170709] Practical :- 8

getch();
exit(1);
}
printf("\n Enter another prime number:-\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\n Wrong input.\n");
getch();
exit(1);
}
printf("\n Enter message:-");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPossible values of e and d are \n");
for(i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();

161040107027(Patel Janki) Page 2


Information And Network Securities[2170709] Practical :- 8

decrypt();
getch();
}
int prime(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;
}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)
{
if(t%i==0)
continue;
flag=prime(i);
if(flag==1 && i!=p && i!=q)

161040107027(Patel Janki) Page 3


Information And Network Securities[2170709] Practical :- 8

{
e[k]=i;
flag=cd(e[k]);
if(flag>0)
{
d[k]=flag;
k++;
}
if(k==99)
break;
}
}
}
long int cd(long int x)
{
long int k=1;
while(1)
{
k=k+t;
if(k%x==0)
return(k/x);
}
}
void encrypt()

161040107027(Patel Janki) Page 4


Information And Network Securities[2170709] Practical :- 8

{
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
pt=m[i];
pt=pt-96;
k=1;
for(j=0;j<key;j++)
{
k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
}
en[i]=-1;
printf("\n The ENCRYPTED message is \n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}

161040107027(Patel Janki) Page 5


Information And Network Securities[2170709] Practical :- 8

void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
ct=temp[i];
k=1;
for(j=0;j<key;j++)
{
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\n The DECRYPTED message is \n");
for(i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}
Output:

161040107027(Patel Janki) Page 6


Information And Network Securities[2170709] Practical :- 8

Fig 1(Encryption and dDecryption of message)

161040107027(Patel Janki) Page 7

You might also like