Thread: how do I use encrypt in the code?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    14

    Question how do I use encrypt in the code?

    Hello,

    I am having trouble figuring out where to put the encrypt function. The purpose is to encrypt a four-digit integer. However, I don't know how to call the function "encrypt" to encrypt the integer.
    Any help will be appreciated.
    Here is my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int
    main( )
    {
       char input,e,d,t,ch;
       int done = 0,e1,e2,e3,e4,n,temp1,temp2,encrypt (int n),num;
    
       while (done != 1)
       {
           printf ("Please Enter an 'e' to encrypt, a 'd' to decrypt, or a 't' to terminate the program: \n");
           scanf ("%c" ,&input);
           while ((ch = getchar()) != EOF && ch != '\n');
    
           if (input == 't')
           {
              done = 1;
              printf ("**** Program Terminated ****");
           }
    
           else if (input == 'e')
           {
              printf ("Enter a four digit number integer to be encrypted: ");
              scanf ("%d",&n);
              while ((ch = getchar()) != EOF && ch != '\n');
    
              printf ("Encrypted number is %d", encrypt (num));
    
              done = 1;
              
            else if (input == 'd')
            {
             printf ("Enter a four digit number integer to be encrypted: ");
             scanf ("%d",&n);
             while ((ch = getchar()) != EOF && ch != '\n');
            }
           }
       }
       return 0;
    }
    
    int encrypt (int num)
    
    {
    int e1,e2,e3,e4,temp1,temp2,n;
              e1 = (n / 1000 +7) %10;
              e2 = (n / 100 %10 +7) %10;
              e3 = (n / 10 %10 +7) %10;
              e4 = (n %10 +7) %10;
    
              temp1 = e1;
              e1 = e3;
              e3 = temp1;
    
              temp2 = e2;
              e2 = e4;
              e4 = temp2;
    
              n = e3*1000 + e4*100 + e1*10 + e2;
              printf ("Encrypted number is: %d", n);
    
              return (n);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The else if 'e' block of code, is missing a closing }, so you'll need to add that. The brace on line #39 should be removed.

    You can tell from the nice indentation style, that is in the wrong place.

    And just before that closing brace, would be where you want to call the encrypt function. After you get the number you want to encrypt, and before the closing brace you will be adding.

    And welcome to the forum!

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Thank you! I got those braces mistakes fixed. According to what you said, I called the encrypt function at the right place (line 29) then? Right now I am able to run my code, but for some reason the output is 8737 instead of 189 when I entered 1234 as the input. I am suspecting there is something wrong with the variable "n" that is assigned to represent the input. Here is the code I have now.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int
    main( )
    {
       char input,e,d,t,ch;
       int done = 0,n,encrypt (int n),num;
    
    
       while (done != 1)
       {
           printf ("Please Enter an 'e' to encrypt, a 'd' to decrypt, or a 't' to terminate the program: ");
           scanf ("%c" ,&input);
           while ((ch = getchar()) != EOF && ch != '\n');
    
    
           if (input == 't')
           {
              done = 1;
              printf ("**** Program Terminated ****");
           }
    
    
           else if (input == 'e')
           {
              printf ("Enter a four digit number integer to be encrypted: ");
              scanf ("%d",&n);
              while ((ch = getchar()) != EOF && ch != '\n');
    
    
              printf ("Encrypted number is %d", encrypt (num));
    
    
              done = 1;
           }
    
    
           else if (input == 'd')
           {
              printf ("Enter a four digit number integer to be decrypted: ");
              scanf ("%d",&n);
              while ((ch = getchar()) != EOF && ch != '\n');
              done = 1;
           }
    
    
           else
           {
               printf ("Wrong Input Value");
           }
       }
    
    
       return 0;
    }
    
    
    int encrypt (int num)
    
    
    {
       int e1,e2,e3,e4,temp1,temp2,n;
            e1 = ((n / 1000) +7) %10;
            e2 = ((n / 100) %10 +7) %10;
            e3 = ((n / 10) %10 +7) %10;
            e4 = ((n %10) +7) %10;
    
    
            temp1 = e1;
            e1 = e3;
            e3 = temp1;
    
    
            temp2 = e2;
            e2 = e4;
            e4 = temp2;
    
    
            n = e3*1000 + e4*100 + e1*10 + e2;
    
    
            return (n);
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    n is returned from encrypt, after being encrypted. Problem is, what is n? n was never given a starting value, so it's just a garbage value, and has nothing to do with int num.

    I can't see that the encrypting is working right or not, but n is definitely a problem. Your compiler should be giving you a warning about this, as well.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    n is supposed to be 4 digit input, and I have saved that integer in the memory as n in the main function, but I don't really know how to take that n from the main function and use it in the encrypt function.

    My compiler didn't give me any warning. I am using code block compiler. For some reason, when the outputs for both 2222 and 1234 are the same. I have removed the two sections of the codes that contained temp1 and temp2 because I realized that this line "n = e3*1000 + e4*100 + e1*10 + e2;" already arranges the numbers in the order that I wanted.

    Just to give you a little bit detail on what I want to do, I am trying to encrypt a 4 digit number by adding 7 to each integer, using mod10 on each integer, and have digit 1(e1) switched with e3 and e2 with e4. So the input 1234 should yield 0189, and 2222 yields 9999.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest reading the input as 4 characters. Your encrypt function should take this array, loop over the characters, convert each to the number value, perform the computation, then convert back to the char value and store it back. Then, after the loop, you swap the characters accordingly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, I'm confused. In the encrypt option, in main(), you are sending num to the encrypt function:
    Code:
     printf ("Encrypted number is %d", encrypt (num));
    And you are receiving "num" as num, with this parameter spec:
    Code:
    int encrypt (int num)
    And then you never use "num" in the encrypt function, you use another variable: "n".

    If you want to work with the digits, and you seem to know what you want, then add some print statements and getchar()'s in there, and see just what's going on. Or use your debugger and step through it, watching the values.

    Why are you making the prototype for the encrypt function, in main(), up on the same line with the variable definitions? Maybe the compiler is not confused (apparently not), but that is quite irregular to me.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Thanks for your responses. I have fixed the problem, and actually got the program working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. encrypt C++ code
    By nik2 in forum C++ Programming
    Replies: 13
    Last Post: 09-13-2010, 06:34 PM
  2. AES-256 Encrypt for .xml
    By Requnael in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2010, 05:30 PM
  3. When to encrypt?
    By Sentral in forum C++ Programming
    Replies: 12
    Last Post: 12-24-2006, 11:53 AM
  4. encrypt
    By hoodiemama in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2002, 12:29 AM
  5. Encrypt
    By vasanth in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 07:14 AM