Extremely basic encryption program

This is a discussion on Extremely basic encryption program within the C Programming forums, part of the General Programming Boards category; Hey guys! This is one of those end-of-year deals, so I am getting over my head trying to learn some ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Extremely basic encryption program

    Hey guys!
    This is one of those end-of-year deals, so I am getting over my head trying to learn some new things to impress the teacher. We have not gone over strings yet, but for the code I have it seems that woul be the best choice to use. However, when I run the code I am not allowed to type spaces or it drops off. So, I tried changeing the cut off option at new line, but I get junk in remaining spaces to 30!! Can someone give some advice on how to fix it to allow spaces?

    Thanks in advance!
    CJ

    Code:
    #include <stdio.h>
    #define N 30
    
    main() 
    {
    
    char orig_text[N];
    int i = 0, choice = 0;
    
    	printf("\nThis program allows you to encrypt/decrypt");
    	printf(" lines of text under %d characters. \n", N);
    	printf("---------------------------------------------------");
    	printf("-----------------------------");
    
    	printf("Please enter a number: 1-Encrypt, 2-Decrypt,");
    	printf(" 3-Exit: ");
    	
    while (	scanf("%d", &choice) == choice < 1 || choice > 3)
    	{
    		printf("\nThat is not a valid option!! 1-Encrypt, 2-Decrypt,");
    		printf(" 3-Exit: ");
    	}
    	
    if (choice == 1)
    {	
    	{
    		printf("Please enter the text you wish to encrypt: ");
    		scanf("%s", orig_text);
    	}
    	
    	while ( orig_text[i] != '\0' ) 
    		{
    		orig_text[i] = orig_text[i] + 3;
    		i++;
    		}
    	
    		printf("Your encrypted text is: %s \n", orig_text);
    
    	return;
    } // End of choice 1 'if' statement
    
    if ( choice == 2 )
    {	
    	{
    		printf("Please enter the text you wish to decrypt: ");
    		scanf("%s", orig_text);
    	}
    	
    	while ( orig_text[i] != '\0' ) 
    		{
    		orig_text[i] = orig_text[i] - 3;
    		i++;
    		}
    	
    		printf("Your decrypted text is: %s \n", orig_text);
    
    		return;
    } // End of choice 2 'if' statement
    
    else
    	printf("\nThank you for using my encryption program! Have a nice day!\n");
    
    
    } // End of main statement

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    impress the teacher
    Then don't use defines and use int main(), not main().
    Do not make direct eye contact with me.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read either a line at a time, using something like fgets, or read a character at a time until you reach a newline. At which point you'll know they've hit enter.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Lurker
    Then don't use defines and use int main(), not main().
    Well my teacher told us she did not want us to hardcode stuff into the program, but to define it so it could be changed. But I'll change the main. From my understanding though, doesn't main give an int value as a default?

    Also I tried using fgets (after reading the search engine stuff this time ), however it does not break after I press enter. It actually does the exact opposite and keeps letting the user input values!

    This is how it should be setup correct?
    Code:
    {
    		printf("Please enter the text you wish to encrypt: ");
    	}
    	
    	while ( fgets( orig_text, sizeof (orig_text), stdin ) != NULL ) 	
    	{
    		orig_text[i] = orig_text[i] + 3;
    		i++;
    	}
    	
    		printf("Your encrypted text is: %s \n", orig_text);
    
    	return;
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because you're using fgets wrong. Well actually, it in itself is being used right. You're just looping wrong. fgets will only return NULL on error or end of file (EOF).

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by quzah
    That's because you're using fgets wrong. Well actually, it in itself is being used right. You're just looping wrong. fgets will only return NULL on error or end of file (EOF).

    Quzah.
    OK. So would that mean I have to put something inside the statement to control that? Because I can't figure out what part of the while loop I would need to change.

    And you would figure with having THREE C text books in front of me that one would...

    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >And you would figure with having THREE C text books in front of me that one would...

    ...not to mention an FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Dave_Sinkula
    >And you would figure with having THREE C text books in front of me that one would...

    ...not to mention an FAQ.
    I know, I know...Frustrating b/c that code from the FAQ looks like mine yet mine does not work. I've read the chapter over AGAIN and see what quzah meant by there being no Null w/o a error. So, my assumption is my before if statement is causeing the damage?

    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    <hint>

    Take fgets() statement out of the loop.
    $ENV: FreeBSD, gcc, emacs

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Well I did that, but then it just runs through all the printf's. It doesn't allow any input at all!!

    Code:
    if (choice == 1)
    	printf("Please enter the text you wish to encrypt: ");
    	
    	fgets(buf, sizeof(buf), stdin);	
    		if ( buf[i] != NULL )
    			{
    				buf[i] = buf[i] + 3;
    				i++;
    			}
    		
    		printf("\nYour encrypted text is: %s \n", buf);
    	
    		return;
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Sounds to me like the usual problem you get when you mix scanf() with fgets()

    Try replacing things like
    scanf( "%d", &choice );

    With
    char temp[100];
    fgets( temp, sizeof temp, stdin );
    sscanf( temp, "%d", &choice );

    > if ( buf[i] != NULL )
    1. you should be comparing with '\0', not NULL
    The value may be the same, but the underlying concept is different
    2. what is the value of i?
    When is it reset to 0, how do you count up (there is no loop here)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Salem
    Sounds to me like the usual problem you get when you mix scanf() with fgets()

    Try replacing things like
    scanf( "%d", &choice );

    With
    char temp[100];
    fgets( temp, sizeof temp, stdin );
    sscanf( temp, "%d", &choice );
    That was it!!! Stupid scanf...lol

    OK. My questions (which is most likely very simple to all of you) is after I changed that I am not able to use my original error checking method. So,....

    1) I want to give the user another chance to do the program. I have used the while loop before to accomplish this, but now when I use it sometimes it messes up. Is there something out of place?

    2) If I run the program and first encrypt, then choose decrypt. It will echo back the text I entered. I tried using fflush(stdin) (as you can see), but that didn't work. Is there a way to clear the string so it doesn't do that?

    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Current code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() 
    {
    
    char buf[BUFSIZ];
    char temp[100];
    int i = 0, choice = 0;
    
    	printf("\nThis program allows you to encrypt/decrypt");
    	printf(" lines of text under %d characters. \n", sizeof(buf) );
    	printf("---------------------------------------------------");
    	printf("-----------------------------");
    
    	printf("Please enter a number: 1-Encrypt, 2-Decrypt,");
    	printf(" 3-Exit: ");
    
    	fgets( temp, sizeof temp, stdin );
    	sscanf( temp, "%d", &choice );
    	 
    while ( choice != 0 )
    	{
    
    if (choice == 1)
    {
    	printf("\nPlease enter the text you wish to encrypt: ");
    	
    	fgets(buf, sizeof(buf), stdin);
    	{	
    		while ( buf[i] != '\0' )
    			{
    				buf[i] = buf[i] + 3;
    				i++;
    			}
    	}
    		printf("\n Your encrypted text is: %s \n", buf);
    	
    	printf("\nWould you like to encrypt/decrypt something else?\n");
    	printf("1-Encrypt, 2-Decrypt, 3-Exit: ");
    
    	fgets( temp, sizeof temp, stdin );
    	sscanf( temp, "%d", &choice );
    	fflush(stdin);
    
    } // End of choice 1 'if' statement
    
    if ( choice == 2 )
    {	
    		printf("\nPlease enter the text you wish to decrypt: ");
    	
    	
    	fgets(buf, sizeof(buf), stdin);
    	{
    		while ( buf[i] != '\0' ) 
    			{
    				buf[i] = buf[i] - 3;
    				i++;
    			}
    	}
    		printf("\nYour decrypted text is: %s \n", buf);
    
    	printf("\nWould you like to encrypt/decrypt something else?\n");
    	printf("1-Encrypt, 2-Decrypt, 3-Exit: ");
    
    	fgets( temp, sizeof temp, stdin );
    	sscanf( temp, "%d", &choice );
    	fflush(stdin);
    
    } // End of choice 2 'if' statement
    
    if ( choice == 3)
    {	
    	printf("\nThank you for using my encryption program! Have a nice day!\n");
    	return 0;
    }
    
    else 
    {
    	printf("\nThat is not a valid option!! 1-Encrypt, 2-Decrypt,");
    	printf(" 3-Exit: ");
    
    	fgets( temp, sizeof temp, stdin );
    	sscanf( temp, "%d", &choice );
    	fflush(stdin);
    }
    
    } // End of while loop
    } // End of main statement
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Life can be made much easier if you start to use functions to break the problem down into smaller steps, or you find you're repeating yourself (like you are with the menu).

    Here's an outline, you add your encrypt and decrypt to the two empty functions provided.

    But satisfy yourself that the outline provided is going to do the right thing no matter what you type.
    Code:
    #include <stdio.h>
    
    void menu ( void ) {
        printf("Please enter a number: \n"
               "1-Encrypt\n"
               "2-Decrypt\n"
               "3-Exit\n"
               "prompt > ");
        fflush( stdout );
    }
    
    int getchoice ( void ) {
        char buff[BUFSIZ];
        int  choice = 0;
        do {
            menu();
            if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
                /* success reading a line, does it make sense? */
                if ( sscanf( buff, "%d", &choice ) != 1 ) {
                    printf( "Enter a number\n" );
                }
            } else {
                /* user EOF, just exit now */
                choice = 3;
            }
        } while ( choice < 1 || choice > 3 );
        return choice;
    }
    
    void encrypt ( void ) {
        printf( "Doing encrypt\n" );
    }
    void decrypt ( void ) {
        printf( "Doing decrypt\n" );
    }
    
    int main ( ) {
        int choice;
        while ( (choice=getchoice()) != 3 ) {
            if ( choice == 1 ) {
                encrypt();
            } else
            if ( choice == 2 ) {
                decrypt();
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Yes my teacher is going over now how we are suppose to go about seperateing the functions. And I see how you did everything but this one part (which is in code tags). I'm sorry if I seem like a a$$ by asking you to explain. Cuz you have really already went out of your way to help. I just don't wanna turn something in that I don't understand!

    Thanks again Salem!

    CJ

    Code:
    int getchoice ( void ) {
        char buff[BUFSIZ];
        int  choice = 0;
        do {
            menu();
            if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
                /* success reading a line, does it make sense? */
                if ( sscanf( buff, "%d", &choice ) != 1 ) {
                    printf( "Enter a number\n" );
                }
            } else {
                /* user EOF, just exit now */
                choice = 3;
            }
        } while ( choice < 1 || choice > 3 );
        return choice;
    }
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  5. Encryption program writing
    By Finchie_88 in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2004, 04:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21