How to ask a valid input in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amskape
    New Member
    • Apr 2010
    • 56

    How to ask a valid input in C

    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:

    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);
    }
    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

    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);
        }
    I got error as



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

    Thanks,
    Anes
    Attached Files
    Last edited by amskape; Oct 15 '13, 12:53 AM. Reason: To Edit a commented code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You have removed the input parameter prompt from your function check so at

    Code:
        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'

    Comment

    • amskape
      New Member
      • Apr 2010
      • 56

      #3
      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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is odd:

        Code:
        for(i=3;e%i==0&&phi%i==0;[B]i+2[/B])
             {
        etc...
        I+2 doesn't do anything. Did you mean I+= 2 ?

        Comment

        • amskape
          New Member
          • Apr 2010
          • 56

          #5
          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

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            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, @weaknessforcat s is asking you about line 13 in your second [RSA] code listing.

            Comment

            • amskape
              New Member
              • Apr 2010
              • 56

              #7
              Dear @Don,@weaknessf orcats,

              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

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                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.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  It looks like the code listings originally posted have been altered, which invalidates my comments.

                  Comment

                  • amskape
                    New Member
                    • Apr 2010
                    • 56

                    #10
                    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

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      It's not that it's bad but it means that the thread no longer makes sense to a new person reading it.

                      Comment

                      Working...