Dear friends,
I am using C , to implement my RSA Code , but I cannot
validate the public key 'e' entry . So I find a code which work fine in validation as shown below:
which do addition of 2 numbers with no validation
as shown in image

But when I try to implement same in my RSA code as shown
I got error as

Please advise a solution to this issue or an alternative way to find a solution.
Thanks,
Anes
I am using C , to implement my RSA Code , but I cannot
validate the public key 'e' entry . So I find a code which work fine in validation as shown below:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
enum { MAX_LINELENGTH = 4096 };
enum { MAX_ATTEMPTS = 10 };
static void wait_at_exit(void)
{
getch();
}
static int get_a_number(const char *prompt)
{
int value;
char line[MAX_LINELENGTH];
int count = 0;
while (fputs(prompt, stdout) != EOF &&
fgets(line, sizeof(line), stdin) != 0)
{
if (sscanf(line, "%d", &value) == 1)
return value;
if (count++ > MAX_ATTEMPTS)
{
printf("I give in; I don't understand what you're typing\n");
exit(1);
}
printf("I said please enter a number.\n");
}
printf("Oops: I got EOF or an error; goodbye!\n");
exit(1);
}
int main(void)
{
atexit(wait_at_exit);
int val_a = get_a_number("Write a number A=");
int val_b = get_a_number("Write a number B=");
int result = val_a + val_b;
printf("%d + %d = %d\n", val_a, val_b, result);
return(0);
}
as shown in image
But when I try to implement same in my RSA code as shown
Code:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
enum { MAX_LINELENGTH = 4096 };
enum { MAX_ATTEMPTS = 10 };
static void wait_at_exit(void)
{
getch();
}
int phi,C,M,n,e,d,FLAG;
static int check() {
int value,i;
char line[MAX_LINELENGTH];
int count = 0;
while (fputs(prompt, stdout) != EOF && fgets(line, sizeof(line), stdin) != 0){
if (sscanf(line, "%d", &value) == 1)
{
if(value==1) {
printf("I give in; I don't understand what you're typing\n");
exit(1);
}
for(i=2;i<value/2;i++) {
if(value%i==0) {
printf("I give in; I don't understand what you're typing\n");
exit(1);
}
}
return value;
}
if (count++ > MAX_ATTEMPTS)
{
printf("I give in; I don't understand what you're typing\n");
exit(1);
}
printf("I said please enter a number.\n");
}
printf("Oops: I got EOF or an error; goodbye!\n");
exit(1);
}
void encrypt()
{
int i;
C = 1;
for(i=0;i<e;i++)
C=(C*M)%n;
C = C%n;
printf("\\n\tEncrypted keyword: %d",C);
}
void decrypt()
{
int i;
M=1;
for(i=0;i<d;i++)
M = (M*C)%n;
M=M%n;
printf("\n\tDecrypted keyword: %d",M);
}
int main(void)
{
int p,q,s;
atexit(wait_at_exit);
printf("Enter two prime numbers\t:");
scanf("%d%d",&p,&q);
n = p*q;
phi = (p-1)*(q-1);
printf("\n\tF(n)\t = %d",phi);
/* do
{
printf("\n\nEnter e\t:");
scanf("%d",&e);
check();
}while(FLAG==1); */
e = check("Enter e=");
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic key is \t: {%d, %d}",e,n);
printf("\n\tPrivate key is\t: {%d,%d}" ,d,n);
printf("\n\nEnter the Plain Text\t:");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
return(0);
}
Please advise a solution to this issue or an alternative way to find a solution.
Thanks,
Anes
Comment