Encryption and Decryption

This is a discussion on Encryption and Decryption within the C Programming forums, part of the General Programming Boards category; I have a school assignment in which asks me to create a program that does the following on encryption and ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Post Encryption and Decryption

    I have a school assignment in which asks me to create a program that does the following on encryption and decryption:

    Write a program that encrypts and decrypts a text (string). The program reads a text into a character pointer: char * originaltext. Write a function that encrypts originaltext and store the encrypted text into: char * ciphertext. The encryption algorithm is replacing each character by a character having 10 added to its ASCII code. Write another function that decrypts ciphertext and store the result into: char * deciphertext. The decryption algorithm is replacing each character by a character having 10 subtracted from its ASCII code. Display the three strings originaltext, ciphertext,and deciphertext.


    It then gives the following hints to start the program:


    • Use malloc or calloc functions to allocate dynamically a memory space for strings.
    • Assume that the text has a maximum of 100 characters.
    • Donít forget to deallocate the memory using free function.



    Could somebody help me to write the program, I'm really stuck and don't know how to start.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by dbz8gokugohan
    Could somebody help me to write the program, I'm really stuck and don't know how to start.
    We don't do people's homework for them. You must at least know how to create a main() function. And I doubt the instructor would have you using dynamic memory allocation without going over it first. Come on, at least try.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The good news is that even as an utmost beginner this shouldn't take you more than 2 hours to write, debug and test.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    First attempt

    So far this is what I have:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main (void) 
    {
        char *originaltext;
        char *ciphertext;
        char *deciphertext;
        
        printf("Type In Your String:");
        scanf("%100s",originaltext);
        
        int encryption ();
        {
            *ciphertext = *originaltext + 10;
            return 1;
        };
        
        int decryption ();
        {
            *deciphertext = *ciphertext - 10;
            return 2;
        };
    
        
        
        if ((originaltext=(char *)malloc(100))==NULL) 
        {
                            printf("Memory allocation error !\n");
                            exit(1);
        }
        
        printf("Original String:\n %s , \nLength of Original String: %i\n",originaltext, strlen(originaltext));
        printf("Encrypted String:\n %s , \nLength of Encrypted String: %i\n",ciphertext, strlen(ciphertext));
        printf("Decrypted String:\n %s , \nLength of Decrypted String: %i\n",deciphertext, strlen(deciphertext));
        free(originaltext);
        
        getchar();
        getchar();
        
        return 0;
    }
    It's most likely wrong since it gives me a blank window screen

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) originalString is a pointer pointing to neverland. It is never initialized to point to a memory location that has enough space to hold 100 characters. Therefore you can't read in it. Also, don't use scanf to read strings. Use fgets().

    2) don't cast malloc

    3) other functions don't belong inside the main function, they belong above.

    4) *ciphertext = *originaltext + 10;

    This is wrong, I'll let you figure out what *ciphertext and *originaltext really are. (Hint: What is their type?)

    "It's most likely wrong since it gives me a blank window screen"

    This is not a trial and error exercise. You have to tell us what is wrong by COMPILING THE CODE. If you were a bridge engineer you wouldn't go: try to drive over, although this is most likely wrong and you will die trying.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    You cant nest functions like that in C. Move them out of main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Modified Encryption Attempt

    Ok so I modified the program slightly and got it to display the original string I type, but I can't get the encrypt and decrypt functions

    Code:
    #include <stdio.h>     // Calls for the libraries
    #include <string.h>
    #include <stdlib.h>
    
    int encrypt (char * originaltext)     // Function to encrypt a string
    {
        char * ciphertext;
        
        if (ciphertext)   
        {
            ciphertext = (char *)malloc(100);
             *ciphertext = *originaltext + 10;
             return *originaltext + 10; // Exits encrypt function 
        }
    }
        
    int decrypt (char * originaltext)     // Function to decrypt a string
    {
        char * ciphertext;
        char * deciphertext;
        if (deciphertext)   
        {
             deciphertext = (char *)malloc(100);
             *ciphertext = *originaltext + 10;
             *deciphertext = *ciphertext - 10;
              return *ciphertext - 10; // Exits decrypt function  
        } 
    }
    
    int main (void)     // Start of program
    {
        char * originaltext;     // Defines pointers
        char * ciphertext; 
        char * deciphertext;
        
        if ((originaltext=(char *)malloc(100))==NULL)   // Sets the maximum number of characters for string
        {
            printf("\nMemory allocation error!\n");
            exit(1);
        }
    
        printf("Type In Your String:\n\n");        // Asks for the string to be typed
        scanf("%100s",originaltext);               // Allows user to type string
        
        encrypt (originaltext);     // Calls for the encrypt function
        decrypt (originaltext);     // Calls for the decrypt function
        
        printf("\n\nOriginal String:\n\n %s \n\nLength Of Original String: %i",originaltext, strlen(originaltext));     // Displays the original string
        printf("\n\nEncrypted String:\n\n %s \n\nLength Of Encrypted String: %i",ciphertext, strlen(ciphertext));       // Displays the encrypted string
        printf("\n\nDecrypted String:\n\n %s \n\nLength Of Decrypted String: %i",deciphertext, strlen(deciphertext));   // Displays the decrypted string
    
        free(originaltext);               // Frees the original string
        
        getchar();
        getchar();
        
        return 0;          // End of program
    }
    I'm using *ciphertext = *originaltext + 10; for encrypt because it's supposed to lead to each character of a string replaced by a character having 10 added, and I'm using *deciphertext = *ciphertext - 10: for decrypt as it's supposed to be the previous *ciphertext subtracted by 10 (I would think that this means *deciphertext points the same as *originaltext)

  8. #8
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    int encrypt (char * originaltext)
    {
        char * ciphertext;
        
        if (ciphertext)   
        {
             ciphertext = (char *)malloc(100);
             *ciphertext = *originaltext + 10;
             return *originaltext + 10;
        }
    }
    Your if statement doesn't make sense; not where it is at. If you're checking to see if your malloc() succeeded, you should do something like this.
    Code:
    char* string_ptr;
    string_ptr = malloc( sizeof( *string_ptr ) * string_length );       // no casting malloc(), m'kay?
    if( string_ptr == NULL )
    {
        fprintf( stderr, "malloc() for string_ptr failed\n" );
        exit( EXIT_FAILURE );
    }
    You're not doing what you think you are, here.
    Code:
    *ciphertext = *originaltext + 10;
    What this will do is assign the decimal char value of the first character in originaltext string + 10 to the memory location pointed to by ciphertext. Observe what the following code does.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int
    main( void )
    {
        char* string_ptr;
        string_ptr = malloc( sizeof( *string_ptr ) * 100 );
        strncpy( string_ptr, "Quick red fox jumps over lazy brown dog.", 100 );
    
        fprintf( stdout, "Printing original string:\n%s\n\n", string_ptr );
    
        *string_ptr = *string_ptr + 10;     // 'Q' just became '['
        fprintf( stdout, "Printing modified string:\n%s\n\n", string_ptr );
    
        return 0;
    }
    What you don't seem to realize is that originaltext and ciphertext are both pointers to the beginning of the block you malloc()'ed.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 05:45 AM
  2. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. Encryption and Decryption Program in C++ using a class
    By goron350 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 09:29 PM
  5. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM

Tags for this Thread


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