Thread: Encrypt/Decrypt Message in C

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    12

    Encrypt/Decrypt Message in C

    Hello. I am trying to make a C program that for argv 2 the user enters a word and for argv 3 the user MUST specify whether they want the program to decrypt or encrypt the message. Program compiles fine but when I try to run it, I am getting a "segmentation fault" any ideas?
    Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char **argv)
    {
        int i;
        const char string = argv[1];                                    //here I set the entered string for argv[2]
        const int length = strlen(string);
    
    
        char encrypted_string[length];                                    //length of encrypted value
        char decrypted_string[length];
    
    
        const char *key_ch = argv[2];
        char key_int = atoi(key_ch);
    
    
    
    
         if(argc < 3)                                            //argument checking
             {
                printf("Not enough arguments\n");
                exit (1);
                }
    
    
            
        if (strcmp(argv[3], "encrypt") == 0)                                //argv[3] sees if it is encrypt or decrypt
        {
            i = 0;
            while(i <= strlen(string)-1)
            {
                encrypted_string[i] = string[i] + key_int;
                i++;
            }
            
        //    printf("Encrypted string: ");                                //print encrypted string
            i = 0;
            while (i <= strlen(string) -1)
            {
                printf("%c", encrypted_string[i]);
                i++;
            }
            printf("\n");
        }
        if (strcmp(argv[3], "decrypt") == 0)
        {
            i = 0;
            while(i <= strlen(string) -1)
            {
                decrypted_string[i] = string[i] - key_int;
                i++;
            }
            
        //    printf("Decrypted String: ");                                //print decrypted string
            i = 0;
            while (i <= strlen(string) -1)
            {
                printf("%c", decrypted_string[i]);
                i++;
            }
    
    
            printf("\n");
                    
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If your program is "compiling fine" there's something terribly wrong! It should be spitting out numerous warnings. If you're using gcc you should use the following flags -W -Wall -pedantic.

    Anyway, you're just missing the asterisk before "string" when you declare it. It should be a pointer, not just a char. Also, you need to make your arrays length+1 bytes long to account for the zero byte at the end of the string. Personally, I'd also make i a size_t (the return type of strlen, usually equivalent to an unsigned long) to remove any compiler complaints about comparing signed and unsigned integers (which gcc complains about with the -pedantic flag).

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    You actually got quite a lot of errors relating to invalid conversion to another type and invalid comparisons. Here are some of them to get you started.

    const char string = argv[1]; char to char *
    const int length = strlen(string); char to const char *

    while(i <= strlen(string)-1) invalid comparison between signed and unsigned
    {
    encrypted_string[i] = string[i] + key_int;
    i++;
    }
    Last edited by deathslice; 03-24-2016 at 08:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encrypt/Decrypt text
    By patneel in forum C Programming
    Replies: 6
    Last Post: 09-06-2011, 02:55 PM
  2. [Help Me]Very Simply Encrypt/Decrypt
    By ShiroAzure in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2011, 12:58 PM
  3. decrypt/encrypt
    By wonderpoop in forum C Programming
    Replies: 15
    Last Post: 10-18-2006, 06:10 PM
  4. Encrypt/Decrypt
    By bitWise in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 03:48 PM
  5. how to encrypt/decrypt
    By bitWise in forum C Programming
    Replies: 3
    Last Post: 10-13-2001, 01:02 PM

Tags for this Thread