0% found this document useful (0 votes)
16 views

Lab Program 6 2Kl07Cs046

This document contains the code for implementing RSA encryption and decryption. It first defines variables and includes necessary header files. It then defines functions to find prime numbers, generate the encryption key, validate prime numbers, and calculate the decryption key. The main function prompts the user to input two prime numbers, generates the public and private keys, encrypts a plaintext string by exponentiation modulo n, and decrypts the ciphertexts using the decryption key to retrieve the original plaintext. It prints the encryption process and decrypted text.

Uploaded by

sunilsmcs
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Program 6 2Kl07Cs046

This document contains the code for implementing RSA encryption and decryption. It first defines variables and includes necessary header files. It then defines functions to find prime numbers, generate the encryption key, validate prime numbers, and calculate the decryption key. The main function prompts the user to input two prime numbers, generates the public and private keys, encrypts a plaintext string by exponentiation modulo n, and decrypts the ciphertexts using the decryption key to retrieve the original plaintext. It prints the encryption process and decrypted text.

Uploaded by

sunilsmcs
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB PROGRAM 6 2KL07CS046

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int prime,j,i,k,z,x,plain,cipher,found,p,q,product,e,n,d,h;
long int mult;
void findprime(int y)
{
prime=1;
for(j=2;j<=(y/2);j++)
{
if(y%j==0)
{ prime=0;
return;
}
}
}
void encryption()
{
for(i=2;i<=product/2;i++)
{
findprime(i);
if(prime==1)
{
if(product%i!=0)
{ e=i;
return;
}
}
}
}
void isvalid(int x)
{
for(i=2;i<=x/2;i++)
{
if(x%i==0)
{
printf("%d is not a prime number\n",x);
exit(1);
}
}
}

void decryption()
{

for(k=0;;k++)
{
if((product*k+1)%e==0)
{ d=(product*k+1)/e;
return;
}
}
}

char txt[20],plaintext[20];
LAB PROGRAM 6 2KL07CS046

int main()
{

int pos=0;
printf("\nAt transmitter side...\n");
printf("Enter the prime numbers\n");
scanf("%d %d",&p,&q);
isvalid(p);
isvalid(q);
printf("\nPrime numbers selected are: %d %d",p,q);
n=p*q;
product=(p-1)*(q-1);
encryption();
printf("\nEncryption key:%d",e);
decryption();
printf("\nDecryption key:%d",d);
printf("\nAt transmitter side...");
printf("\nEnter plain text to be transmitted:");
scanf("%s",txt);

for(h=0;h<strlen(txt);h++)
{
plain=txt[h];
mult=1;
for(z=1;z<=e;z++)
mult=(mult*plain)%n;
cipher=mult;
printf("\t%d",cipher);

mult=1;
for(z=1;z<=d;z++)
mult=(mult*cipher)%n;

plaintext[pos++]=(char)mult;

printf("\nRECEIVED Plain text after calculation: %s\n",plaintext);


return 0;

You might also like