I'm in the middle of programming for RSA encryption, but gcd==1
in the if statement does not make it true even if I enter e with the appropriate value.This is a program that I want the maximum common denominator of f and e to be 1
#include<stdio.h>
# include <math.h>
int gcd(intf, inte){
intr;
while(r!=0){
r = f%e;
f=e;
e=r;
}
return f;
}
int main(void){
int hirabun;
intp;
intq;
int flag = 0;
inti;
intn;
inte;
intf;
intr;
printf("Please enter a plaintext number:");
scanf("%d", & hirabun);
printf("Please enter p:");
scanf("%d", &p);
printf("Please enter q:");
scanf("%d", & q);
for(i=2;i<p;++i){
if(p%i==0){
flag = 1;
break;
}
}
if(flag==0)
printf("%d is a prime number.\n", p);
else
printf("%d is not a prime number.\n", p);
// return 0;
for(i=2;i<q;++i){
if(q%i==0){
flag = 1;
break;
}
}
if(flag==0)
printf("%d is a prime number.\n",q);
else
printf("%d is not a prime number.\n",q);
n = p*q;
f = (p-1)*(q-1);
printf("gcd(f,e)=Enter an e with 1 :");
scanf("%d", & e);
if(gcd(f,e)==1)
printf("gcd(f,e)=1\n";
else
printf("No, it's not.");
return 0;
printf("e value is %d",e);
printf("%d", gcd(f,e)));
}
The reason is that r
in gcd()
is uninitialized.
r
must be initialized to a value other than zero before the while(r!=0)
processing will not take place.
When I wrote the code in question to q.c and compiled it (clang-Wall q.c), I got a warning.
It is recommended that you try to clean compilation, including warnings.
[Compilation results]
q.c:6:7:warning: variable 'r' is used initialized whenever function 'gcd' is called [-Wsometimes-uninitialized]
intr;
~~~~^
q.c:8:9:note:uninitialized use occur here
while(r!=0){
^
q.c:6:8:note:initialize the variable 'r' to silence this warning
intr;
^
= 0
q.c:28:5:warning:unused variable 'r' [-Wunused-variable]
intr;
^
2 warnings generated.
© 2024 OneMinuteCode. All rights reserved.