Please help me in this Q: Q1: Covert the follwing code from c++ to c, The code i
ID: 3854425 • Letter: P
Question
Please help me in this Q:
Q1: Covert the follwing code from c++ to c, The code is using RSA algorithm
#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
main()
{
long int i,j,ff,p,q,n,h,d,m,c=0,e,df,prime[1000],dv[100];
clrscr();
cout<<"Enter a Value for p :";
cin>>p;
cout<<" Enter a Value for q :";
cin>>q;
cout<<" Enter a value for m :";
cin>>m;
//======== Calculate n and (p-1)(q-1) Values
n=p*q;
h=(p-1)*(q-1);
//======== Generate the Prime Array
for (i=2;i<100;i++)
{
int f=0;
for(j=2;j<i-1;j++)
if(i%j==0)
f=1;
if (f==0)
{
prime[c++]=i;
}
}
int f=0;
for(i=0;i<c && f==0;i++)
{
ff=h%prime[i];
if( ff!=0)
{
e=prime[i];
f=1;
}
}
//============= Generate the Possible Values of ed
dv[0]=h+1;c=0;
for(i=1;i<100;i++)
dv[i]=dv[i-1]+h;
f=0;
for(i=0;i<100 && f==0;i++)
if(dv[i]%e==0)
{
d=dv[i]/e;
f=1;
}
//============= Calculate the value of c
df=1;
for(i=0;i<e;i++)
df=df*m;
c=df%n;
cout<<" ******** The Results ******** ";
cout<<" The Value of e is :"<<e;
cout<<" The Value of d is :"<<d;
cout<<" The Public Key ("<<e<<","<<n<<")";
cout<<" The Private Key ("<<d<<","<<n<<")";
cout<<" The Value of c is :"<<c;
getch();
return 0;
}
Sample of the Result
Enter a Ualue for p :7 Enter a walue for q 11 Enter a value for m :6 The Results The value of e is :7 The Value of d is :43 The Public Key C7,77) The Private Key (43,77) The Value of c is :41Explanation / Answer
C++ PROGRAM CONVERTED TO C CODE
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
long int i,j,ff,p,q,n,h,d,m,c=0,f=0,e,df,prime[1000],dv[100],flag=0;
clrscr();
printf("Enter the Value for p :");
scanf("%ld",&p);
printf("Enter the Value for q :");
scanf("%ld",&q);
printf("Enter the Value for m :");
scanf("%ld",&m);
//======== Calculate n and (p-1)(q-1) Values
n=p*q;
h=(p-1)*(q-1);
//======== Generate the Prime Array
for (i=2;i<100;i++)
{
flag=0;
for(j=2;j<i-1;j++)
if(i%j==0)
flag=1;
if (flag==0)
{
prime[c++]=i;
}
}
for(i=0;i<c && f==0;i++)
{
ff=h%prime[i];
if( ff!=0)
{
e=prime[i];
f=1;
}
}
//============= Generate the Possible Values of ed
dv[0]=h+1;c=0;
for(i=1;i<100;i++)
dv[i]=dv[i-1]+h;
f=0;
for(i=0;i<100 && f==0;i++)
if(dv[i]%e==0)
{
d=dv[i]/e;
f=1;
}
//============= Calculate the value of c
df=1;
for(i=0;i<e;i++)
df=df*m;
c=df%n;
printf(" ******** The Results ******** ");
printf(" The Value of e is :%ld",e);
printf(" The Value of d is :%ld",d);
printf(" The Public Key (%ld,%ld)",e,n);
printf(" The Private Key (%ld,%ld)",d,n);
printf(" The Value of c is :%ld",c);
getch();
return 0;
}