Hi people im new here. I want to work with security but i got many issues with this simple program.

I want to encrypt and decrypt a msg with openssl cmds. Btw im using pipeline and process open (popen) function. Im using Fedora gcc to compile. Any help, recommendations will be appreciate. Thankss! =)

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main() {
    FILE * read_fp;
char buffer [BUFSIZ + 1];
int chars_read;
memset (buffer, '\0', sizeof(buffer));


int selection;
  printf("Menu Security\n");
  printf("\n\t1. Encrypt a message \n\t2. Decrypt a message\n\t3. Exit\n");
  printf("\nChoose an option: ");
  scanf("%d", &selection);

  switch(selection){
    case 1:
    read_fp = popen ("openssl enc -aes-256-cbc -salt -in file.txt -out file.enc","r");

      /*printf("Encrypted Successful\n");*/
      /*printf("Encrypted Failed, Password Doesn't Match. Please Try Again!\n");*/
      break;
    case 2:
    popen ("openssl enc -aes-256-cbc -salt -in file.txt -out file.enc","w");*/
printf("Decrypted Successful\n");
      /*printf("Decrypted Failed, Password Doesn't Match. Please Try Again!\n");*/
      break;
    case 3:
      exit(EXIT_SUCCESS);;
      break;
    default:
      printf("Invalid selection, Please Try Again!\n");
      break;
  }
  /*return 0;*/
/*END MENU*/


if (read_fp!=NULL){

chars_read = fread (buffer, sizeof(char), BUFSIZ, read_fp);

if (chars_read > 0){

buffer [chars_read -1] = '\0';
printf("Reading: -\n%s\n", buffer);

chars_read = fread(buffer,sizeof(char),BUFSIZ,read_fp);

}

/*while (chars_read > 0) {
            buffer[chars_read - 1] = '\0';
            printf("Reading:-\n %s\n", buffer);
            chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
        }*/


pclose(read_fp);

}
}

got many issues

So what are the issues?

Member Avatar for Mouche

For one, you have a rogue "*/" on line 28 which prevents you from compiling the code.

Second, you're not calling the openssl command to decrypt. You want something like this:

openssl enc -d -aes-256-cbc -in file.enc

Provided you enter the right encryption password, it will decrypt the message for you.

Finally, you don't have your menu in a loop, so it only runs once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.