473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to ask a valid input in C

56
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. enum { MAX_LINELENGTH = 4096 };
  5. enum { MAX_ATTEMPTS   = 10   };
  6.  
  7. static void wait_at_exit(void)
  8. {
  9.     getch();
  10. }
  11.  
  12. static int get_a_number(const char *prompt)
  13. {
  14.     int value;
  15.     char line[MAX_LINELENGTH];
  16.     int count = 0;
  17.     while (fputs(prompt, stdout) != EOF &&
  18.            fgets(line, sizeof(line), stdin) != 0)
  19.     {
  20.         if (sscanf(line, "%d", &value) == 1)
  21.             return value;
  22.         if (count++ > MAX_ATTEMPTS)
  23.         {
  24.             printf("I give in; I don't understand what you're typing\n");
  25.             exit(1);
  26.         }
  27.         printf("I said please enter a number.\n");
  28.     }
  29.  
  30.     printf("Oops: I got EOF or an error; goodbye!\n");
  31.     exit(1);
  32. }
  33. int main(void)
  34. {
  35.     atexit(wait_at_exit);
  36.     int val_a  = get_a_number("Write a number A=");
  37.     int val_b  = get_a_number("Write a number B=");
  38.     int result = val_a + val_b;
  39.     printf("%d + %d = %d\n", val_a, val_b, result);
  40.     return(0);
  41. }
  42.  
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

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include <stdlib.h>
  4.     enum { MAX_LINELENGTH = 4096 };
  5.     enum { MAX_ATTEMPTS   = 10   };
  6.     static void wait_at_exit(void)
  7.     {
  8.     getch();
  9.     }
  10.     int phi,C,M,n,e,d,FLAG;
  11.     static int check() {
  12.     int value,i;
  13.     char line[MAX_LINELENGTH];
  14.     int count = 0;
  15.     while (fputs(prompt, stdout) != EOF && fgets(line, sizeof(line), stdin) != 0){
  16.         if (sscanf(line, "%d", &value) == 1)
  17.         {
  18.             if(value==1) {
  19.              printf("I give in; I don't understand what you're typing\n");
  20.              exit(1);             
  21.  
  22.             }
  23.             for(i=2;i<value/2;i++) {
  24.              if(value%i==0) {
  25.              printf("I give in; I don't understand what you're typing\n");
  26.              exit(1);                            
  27.  
  28.              }                                       
  29.             }                       
  30.  
  31.  
  32.             return value;
  33.         }    
  34.         if (count++ > MAX_ATTEMPTS)
  35.         {
  36.             printf("I give in; I don't understand what you're typing\n");
  37.             exit(1);
  38.         }
  39.         printf("I said please enter a number.\n");
  40.     }
  41.  
  42.     printf("Oops: I got EOF or an error; goodbye!\n");
  43.     exit(1);
  44.  
  45.  
  46.  
  47.     }
  48.  
  49.     void encrypt()
  50.     {
  51.       int i;
  52.       C = 1;
  53.      for(i=0;i<e;i++)
  54.      C=(C*M)%n;
  55.       C = C%n;
  56.      printf("\\n\tEncrypted keyword: %d",C);
  57.     }
  58.     void decrypt()
  59.     {
  60.      int i;
  61.      M=1;
  62.     for(i=0;i<d;i++)
  63.     M = (M*C)%n;
  64.     M=M%n;
  65.     printf("\n\tDecrypted keyword: %d",M);
  66.     }
  67.     int main(void)
  68.     {
  69.     int p,q,s;
  70.     atexit(wait_at_exit);
  71.     printf("Enter two prime numbers\t:");
  72.     scanf("%d%d",&p,&q);
  73.     n = p*q;
  74.     phi = (p-1)*(q-1);
  75.     printf("\n\tF(n)\t = %d",phi);
  76.    /* do
  77.     {
  78.      printf("\n\nEnter e\t:");
  79.     scanf("%d",&e);
  80.     check();
  81.     }while(FLAG==1); */
  82.  
  83.     e  = check("Enter e=");
  84.  
  85.     d  = 1;
  86.     do
  87.     {
  88.      s = (d*e)%phi;
  89.      d++;
  90.     }while(s!=1);
  91.     d = d-1;
  92.     printf("\n\tPublic key is \t: {%d, %d}",e,n);
  93.     printf("\n\tPrivate key is\t: {%d,%d}" ,d,n);
  94.     printf("\n\nEnter the Plain Text\t:");
  95.     scanf("%d",&M);
  96.     encrypt();
  97.     printf("\n\nEnter the Cipher text\t: ");
  98.     scanf("%d",&C);
  99.     decrypt();
  100.     getch();
  101.     return(0);
  102.     }
  103.  
  104.  
I got error as



Please advise a solution to this issue or an alternative way to find a solution.

Thanks,
Anes
Attached Images
File Type: jpg working Code output.jpg (17.3 KB, 426 views)
File Type: jpg C-Code Error.jpg (34.0 KB, 457 views)
Oct 14 '13 #1
10 2586
Banfa
9,065 Expert Mod 8TB
You have removed the input parameter prompt from your function check so at

Expand|Select|Wrap|Line Numbers
  1.     while (fputs(prompt, stdout) != EOF && fgets(line, sizeof(line), stdin) != 0){
  2.         if (sscanf(line, "%d", &value) == 1)
  3.  
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'
Oct 14 '13 #2
amskape
56
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
Oct 14 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
This is odd:

Expand|Select|Wrap|Line Numbers
  1. for(i=3;e%i==0&&phi%i==0;i+2)
  2.      {
  3. etc...
I+2 doesn't do anything. Did you mean I+= 2 ?
Oct 14 '13 #4
amskape
56
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
Oct 14 '13 #5
donbock
2,426 Expert 2GB
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.
Oct 14 '13 #6
amskape
56
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
Oct 15 '13 #7
donbock
2,426 Expert 2GB
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.
Oct 15 '13 #8
weaknessforcats
9,208 Expert Mod 8TB
It looks like the code listings originally posted have been altered, which invalidates my comments.
Oct 15 '13 #9
amskape
56
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
Oct 15 '13 #10
weaknessforcats
9,208 Expert Mod 8TB
It's not that it's bad but it means that the thread no longer makes sense to a new person reading it.
Oct 15 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
3
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...
4
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,...
5
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...
17
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...
43
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...
1
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...
17
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
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.