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: - #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);
-
}
-
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 - #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);
-
}
-
-
I got error as
Please advise a solution to this issue or an alternative way to find a solution.
Thanks,
Anes
10 2586 Banfa 9,065
Expert Mod 8TB
You have removed the input parameter prompt from your function check so at -
while (fputs(prompt, stdout) != EOF && fgets(line, sizeof(line), stdin) != 0){
-
if (sscanf(line, "%d", &value) == 1)
-
where you try to fputs prompt the compiler has not heard of the symbol prompt and can not continue. In fact exactly what your compiler error said 'prompt undefined'
Hi Dear Banfa,
Your quick answer avoid error from me . But still my logic not work. I need to check prime number in that loop. please advise
Thanks,
Anes
This is odd: - for(i=3;e%i==0&&phi%i==0;i+2)
-
{
-
etc...
I+2 doesn't do anything. Did you mean I+= 2 ?
Dear weaknessforcats,
I don't get your answer , do you can explain , I don't use i+2
as you mention in code . Please advise me.
Thanks,
Anes
You need to check if p and q are prime.
You need to check if e and phi(n) are coprime with each other.
I suggest you write two new functions: isPrime (that takes one argument) and isCoprime (that takes two arguments).
By the way, @weaknessforcats is asking you about line 13 in your second [RSA] code listing.
Dear @Don,@weaknessforcats,
That line is deleted it's a commented code , just test it and comment .
@ Don - Do you can provide that 2 functions which suite my need if possible , else I will look it and inform you soon.
Thanks,
Anes
You need to produce these functions if you agree that they are worth doing. We can help you if you get stuck. The first step is to determine how to accomplish these functions if you were using paper and pencil instead of a computer -- that is, pick your algorithms.
It looks like the code listings originally posted have been altered, which invalidates my comments.
Ok don I will look it ,
@Weaknessfor : Yes I just remove that comment from my code , don't feel it as bad ...
Waiting for your advise
Thanks,
Anes
It's not that it's bad but it means that the thread no longer makes sense to a new person reading it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JR |
last post by:
Hey all,
I have read part seven of the FAQ and searched for an answer but can
not seem to find one.
I am trying to do the all too common verify the data type with CIN.
The code from the FAQ...
|
by: Fao, Sean |
last post by:
Hello all,
As stated in another message, it's been a long time since I've done any
C coding and I'm not feeling comfortable that I'm doing this correctly.
Basically, I'd like to verify that my...
|
by: santa19992000 |
last post by:
can I use scanf to get input (some times user enters input sometimes
not, just hit keyboard)?.
It displays to enter IP address, if user wants to change, then he
enters, otherwise he hits keyboard,...
|
by: Dianne Siebold |
last post by:
I'm not sure if I can use regex for this or how to do it, but here's what I
need to do: I want to check that a string contains only the characters A-Z,
a-z, 0-9, -(hypen) or _ (underscore).
If...
|
by: Martin Jørgensen |
last post by:
Hi,
Since I'm a newbie I have some small but quick (probably) stupid
questions also :-)
This is my "get_double" function which takes a default argument also of
type double. The function...
|
by: SLH |
last post by:
hi people. im trying to validate input received via a text area on an ASP
page before writing it to a database. i cant use client side javascript due
to policy, so it all has to happen on the...
|
by: Rancid Buttchutney |
last post by:
I've tried searching the web but I've had little success finding a
cross-browser(IE, Firefox and Opera anyway) implementation of a
restricted text INPUT that works in realtime before a character...
|
by: Petyr David |
last post by:
Just looking for the simplest. right now my perl script returns an
error messge to the user if the date string is invalid. would like to
do this before accessing the server.
TX
|
by: =?ISO-2022-JP?B?GyRCJD8kKxsoQg==?= |
last post by:
Hi everyone,
I am developing the console which has the embedded Python interactive
interpreter. So, I want to judge whether current command is complete
or not. Below is good example to solve...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |