Thread: C encryption code (Please help me fix it?)

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    C encryption code (Please help me fix it?)

    Hello, I am a Batch programmer who is slowly trying to move back to C which was my first language. I am experienced with C, but I have trouble with properly initializing variables as the correct data types. I seem to run into many warnings/errors when I do, and end up incorrectly trying to cast things. I have been working on my encryption software recently, and I have had the problems explained above. I have tried and tired, but have failed to fix anything. The problem seems to be that all of my program works except the most important part. The storing the alphabet in an array code works, the store symbols for encryption in an array code works, the getting the user input string works, and the printing of the user string works. The only thing that doesn't work is the actual encryption! Please help me fix this, as I'm not good with data types.

    Below is the code:

    Code:
    #include <stdio.h>
    
    
    #include <stdlib.h>
    
    
    #include <string.h>
    
    
    #include <ctype.h>
    
    
    #define MAXSTRING 51
    
    
    /****************************************************************/
    
    
     int main(){
         char   Alphabet[27];
            char  EncryptionSymbols[27];
              char  UserInput[MAXSTRING];
    
    
    /****************************************************************/
    
    
         int i = 0;
          int letter = 65;
    
    
    do {
    (int)(Alphabet[i] = letter++);
    
    
        i++;
    } while (letter <= 90);
    
    
    
    
    if (Alphabet[26] != 0x00) {
    Alphabet[26]=0x00;
    }
    printf("%s", Alphabet);
    i++;
    
    
           printf(" \n Press any key to continue \n");
      getc( stdin );
    /****************************************************************/
    
    
     int x = 0;
    char symbol = 0x50;
    
    
    while (x < 26)  {
    
    
        EncryptionSymbols[x] = symbol;
    symbol += 2;
    x++;
    }
    
    
    if (Alphabet[26] != 0x00) {
    Alphabet[26]=0x00;
    }
    
    
    
    
    printf("%s", EncryptionSymbols);
    
    
            printf("\n Press any key to continue \n");
      getc( stdin );
    /****************************************************************/
    char *InputPtr = UserInput;
    
    
     printf("Enter in what you want encrypted(%i character limit ) \n", (int)MAXSTRING);
        fgets(InputPtr, MAXSTRING, stdin);
    /****************************************************************/
         printf("\n Press any key to continue \n");
      getc( stdin );
     x = 0;
    int ctr=0;
     int ctr2 = 0;
    char *Inputp;
      Inputp = &UserInput[0];
    for (ctr = 0; ctr < strlen(UserInput) && Inputp != 0x00; ctr++) {
    printf("%c", tolower(UserInput));
     while (ctr2 < 26) {
        if ((char *)Inputp == (char *)Alphabet[ctr2] || (char *)Inputp == tolower((char *)Alphabet[ctr2])) {
     UserInput[x]=EncryptionSymbols[x];
      x++;
      }
        ctr2++;
        }
     ctr++;
     Inputp++;
     }
    
    
        printf(" \n Press any key to continue \n");
      getc( stdin );
    
    
      if (UserInput[MAXSTRING-1] != 0x00) {
    UserInput[MAXSTRING-1]=0x00;
    }
    
    
    printf("You result is: %s \n", UserInput);
    
    
    
    
        printf("Press any key to exit \n");
      getc( stdin );
      exit(1);}

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    First of all, your code is not compile-able and it has a few syntax errors
    (like missing end } of main..?) perhaps this is just a paste failure? At any rate no one is going to take the time to analyze your code unless you put it in a reasonably indented and structured format.

    There are numerous tools online to automatically indent code for you, use google find one and use it if you can't do it, vim as well as many other editors will also automatically indent your code for you, again google this.

    Now, Here is the first tip you get, before I bother analyzing any more of your code. You can initialize a character buffer when you declare it (and ONLY when you declare it), like so:
    Code:
    char Alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    /* now the size is also easy to find: */
    size_t Alphabet_sz = sizeof Alphabet;
    
    /* Now do something with it (just print it in this case) C99 style */
    for (size_t i = 0; i < Alphabet_sz-1; ++i)
      putchar(Alphabet[i]);
    /* ... */
    Last edited by nonpuz; 01-08-2013 at 08:58 PM. Reason: Made example code clearer with a comment

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Give an example of what you are trying to do between lines 93 and 104, as far as the encryption, and a description.

    @Nonpuz - already did that (fixing the indentation, etc.)!
    Last edited by Adak; 01-08-2013 at 09:06 PM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by BatchProgrammer View Post
    The storing the alphabet in an array code works, the store symbols for encryption in an array code works, the getting the user input string works, and the printing of the user string works. The only thing that doesn't work is the actual encryption!
    If only encryption is the problem, it is better to separate this functionality into it's own function, with a declaration something like

    void encrypt(const char *inputString, char *outputString);

    You can "stub" the function just by writing a function that initially does nothing, like this

    Code:
    void encrypt(const char *inputString, char *outputString) 
    { 
        strcpy(outputString, inputString);
    }
    With this, you can replace your encryption logic with a simple call to "encrypt(UserInput, UserOutput);" and then it should still work, although nothing would be encrypted yet. However, if not even the stub version works, then something else is wrong with the program structure or logic.

    Once you have a working structure, then try to come back to your encrypt function to get it to work as you want.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    Problems with my new multiple functions

    I tried to rewrite some of my code as suggested in different functions and with more comments, but now I can't seem to even make it so that it works with different functions anymore.

    I get errors and warnings such as these:

    |In function 'main':|
    |28|warning: passing argument 1 of 'StoreSymbols' makes pointer from integer without a cast
    |20|note: expected 'char *' but argument is of type 'char'|
    |28|error: invalid initializer
    |29|warning: passing argument 1 of 'GetInput' makes pointer from integer without a cast
    |21|note: expected 'char *' but argument is of type 'char'
    |29|error: invalid initializer
    |30|warning: passing argument 1 of 'Output' makes pointer from integer without a cast|
    |22|note: expected 'char *' but argument is of type 'char'|
    |In function 'Output':|
    |79|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    |In function 'StoreSymbols':|
    |83|error: expected declaration or statement at end of input|
    ||=== Build finished: 3 errors, 4 warnings ===|

    Please help me with these, as I do not know how to fix these properly. Here is the code:

    Code:
    #include <stdio.h>
    
    
    #include <stdlib.h>
    
    
    #include <string.h>
    
    
    #include <ctype.h>
    
    
    #define MAXSTRING 51
    
    
    #define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"
    
    
    
    
                int x, i = 0;
                char symbol = '$';
                char UserInput[MAXSTRING];
    
    
              /* Function prototypes are below */
    
    
      char  StoreSymbols( char  EncryptionSymbols[27]);
         char  GetInput( char  UserInputInput[MAXSTRING]);
          void Output( char  StringInput[MAXSTRING]);
    
    
    /****************************************************************/
    
    
       int main(){
    
    
            char EncryptionSymbols[27] = StoreSymbols(EncryptionSymbols[27]);
              char StringInput[MAXSTRING] = GetInput(UserInput[MAXSTRING]);
                Output(StringInput[MAXSTRING]);
    
    
         /* Code above initializes variables and calls the functions in order */
     }
    
    
    
    
    
    
       char  StoreSymbols( char  EncryptionSymbols[27]) {
    
    
        while (x < 26)  {
        EncryptionSymbols[x] = symbol;
     symbol += 2;
      x++;
    }
    
    
      /* Code above loops until EncryptionSymbols is filled with symbols ready
                           for the actual encryption code.  */
    
    
        printf("%s", EncryptionSymbols);
    
    
      if (EncryptionSymbols[26] != 0x00) {
    EncryptionSymbols[26]=0x00;
    
    
        return(EncryptionSymbols[27]);
    
    
    }
    
    
    
    
    
    
       char  GetInput( char  UserInput[MAXSTRING]) {
    
    
    char *InputPtr = UserInput;
    
    
     printf("Enter in what you want encrypted(%i character limit ) \n", (int)MAXSTRING);
    
    
        fgets(InputPtr, MAXSTRING, stdin);
    
    
    
    
      if (UserInput[MAXSTRING-1] != 0x00) EncryptionSymbols[26]{
    UserInput[MAXSTRING-1]=0x00;
    
    
    return (UserInput[MAXSTRING]);
    
    
    }
    
    
        }
    
    
    
    
     void Output( char  StringInput[MAXSTRING]) {
    printf("You result is: %s \n", StringInput[MAXSTRING]);
    
    
    
    
        printf("Press any key to exit \n");
      getc( stdin );
      exit(1);
    
    
    return;
      }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You really need to learn to indent your code properly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSTRING 51
    #define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"
    
    int x, i = 0;
    char symbol = '$';
    char UserInput[MAXSTRING];
    
    /* Function prototypes are below */
    char  StoreSymbols( char  EncryptionSymbols[27]);
    char  GetInput( char  UserInputInput[MAXSTRING]);
    void Output( char  StringInput[MAXSTRING]);
    
    
    /****************************************************************/
    int main()
    {
    
    
        char EncryptionSymbols[27] = StoreSymbols(EncryptionSymbols[27]);
        char StringInput[MAXSTRING] = GetInput(UserInput[MAXSTRING]);
        Output(StringInput[MAXSTRING]);
    
    
        /* Code above initializes variables and calls the functions in order */
    }
    
    
    char  StoreSymbols( char  EncryptionSymbols[27])
    {
        while (x < 26)
        {
            EncryptionSymbols[x] = symbol;
            symbol += 2;
            x++;
        }
    
    
        /* Code above loops until EncryptionSymbols is filled with symbols ready
                             for the actual encryption code.  */
    
    
        printf("%s", EncryptionSymbols);
    
    
        if (EncryptionSymbols[26] != 0x00)
        {
            EncryptionSymbols[26]=0x00;
    
    
            return(EncryptionSymbols[27]);
    
    
        }
    
    
    
    
    
    
        char  GetInput( char  UserInput[MAXSTRING])
        {
    
    
            char *InputPtr = UserInput;
    
    
            printf("Enter in what you want encrypted(%i character limit ) \n", (int)MAXSTRING);
    
    
            fgets(InputPtr, MAXSTRING, stdin);
    
    
    
    
            if (UserInput[MAXSTRING-1] != 0x00) EncryptionSymbols[26]
            {
                UserInput[MAXSTRING-1]=0x00;
    
    
                return (UserInput[MAXSTRING]);
    
    
            }
    
    
        }
    
    
    
    
        void Output( char  StringInput[MAXSTRING])
        {
            printf("You result is: %s \n", StringInput[MAXSTRING]);
    
    
    
    
            printf("Press any key to exit \n");
            getc( stdin );
            exit(1);
    
    
            return;
        }
    Code:
    ||=== testc2, Debug ===|
    main.c||In function 'main':|
    main.c|24|warning: passing argument 1 of 'StoreSymbols' makes pointer from integer without a cast [enabled by default]|
    main.c|14|note: expected 'char *' but argument is of type 'char'|
    main.c|24|error: invalid initializer|
    main.c|25|warning: passing argument 1 of 'GetInput' makes pointer from integer without a cast [enabled by default]|
    main.c|15|note: expected 'char *' but argument is of type 'char'|
    main.c|25|error: invalid initializer|
    main.c|26|warning: passing argument 1 of 'Output' makes pointer from integer without a cast [enabled by default]|
    main.c|16|note: expected 'char *' but argument is of type 'char'|
    main.c||In function 'GetInput':|
    main.c|80|warning: statement with no effect [-Wunused-value]|
    main.c|81|error: expected ';' before '{' token|
    main.c|91|warning: no return statement in function returning non-void [-Wreturn-type]|
    main.c||In function 'Output':|
    main.c|98|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]|
    main.c||In function 'StoreSymbols':|
    main.c|109|error: expected declaration or statement at end of input|
    main.c|109|warning: control reaches end of non-void function [-Wreturn-type]|
    main.c||In function 'main':|
    main.c|30|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 4 errors, 8 warnings (0 minutes, 11 seconds) ===|
    StoreSymbols function is missing the closing "}" brace.

    In C, you can not use a function to initialize a variable/array.
    Code:
    char EncryptionSymbols[27] = StoreSymbols(EncryptionSymbols[27]);
    Last edited by stahta01; 01-09-2013 at 08:07 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Edit: You need to review this lesson http://www.cprogramming.com/tutorial/c/lesson9.html

    I re-wrote the code in your main function so it is valid C; it may or may not do the right thing.

    Code:
    int main()
    {
        char EncryptionSymbols[27];
        char StringInput[MAXSTRING];
    
        StoreSymbols(EncryptionSymbols);
        GetInput(UserInput);
        Output(StringInput);
    
        /* Code above initializes variables and calls the functions in order */
    }
    Tim S.
    Last edited by stahta01; 01-09-2013 at 08:32 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Ok, good effort here, this is actually starting to look like a real program structure. There is an issue with the way you are passing information to/from
    your functions however. You don't want to specify the size of the array in the function declaration, even if that were correct syntax, you would be locking yourself down to a single size array that is acceptable to the function and nothing else. You want to make the function general enough that is useful enough to perhaps reuse in another program later or perhaps even this one in a different way/on a different string (size) in the future.

    So, instead you should declare the function like this:
    Code:
    char * StoreSymbols(char * s, size_t size)
    {
        int symbol = 0x50;
        size_t i;
        for (i = 0; i < size; ++i)
            s[i] = symbol++;
        
        return s;
    }
    Now my function can work on any string of any size, I just pass the string and the size of the string to the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-08-2012, 05:37 PM
  2. Replies: 16
    Last Post: 11-09-2011, 01:43 PM
  3. AES Encryption error code.
    By deevrielk in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2008, 08:37 PM
  4. Encryption
    By L_U_K_E in forum C++ Programming
    Replies: 12
    Last Post: 04-30-2006, 01:27 PM

Tags for this Thread