Thread: Gurus, How to put Openssl command line into system()?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    9

    Question Gurus, How to put Openssl command line into system()?

    To all C Gurus,

    I want to run this openssl command line inside C.
    openssl aes-256-cbc -d -pass pass:"password" -a -salt -in system.aes -out system.txt

    Therefore, I type in below source code.
    Code:
    01  #include <stdlib.h> 
    02  #include <stdio.h> 
    03  #include <string.h> 
    04  int main()
    05  {
    06    char child2[BUFSIZ];
    07    strcpy (child2, "openssl aes-256-cbc -d -pass pass:");
    08    strcat (child2, ""password" ");
    09    strcat (child2, "-a -salt -in system.aes -out system.txt ");
    10    printf ("Executing %s\n", child2);
    11    system (child2);
    12    return 0;
    13  }
    Then error message came out, debugger point to line 08 because of ""password" " error.
    Please guide me how to solve this problem. I am new bird to C world. Thanks...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    When you give us a compiler error, please cut and paste it verbatim, it's easier for everybody.

    Code:
    strcat (child2, ""password" ");
    A double quote " is used to mark the beginning and end of a string in C. So when the compiler sees your first quote in that line, it says "this is the start of a string". Then it sees the second quote (red above), and says ok, here's the end of the string, there's nothing in it. Then it sees password. That password is not inside a string, so the compiler thinks it's some sort of identifier, and complains, since strcat has exactly 2 parameters, and is expecting a closing ). You can use single ' around password if you want (your shell shouldn't care), or you can escape the double quote by using \" instead.

    EDIT: If you're using an editor with syntax highlighting (which you should), this line would look weird. the word "password" wont have the normal color of a string.
    Last edited by anduril462; 06-02-2011 at 10:07 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru anduril462,

    I will try it and get back to you. Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru anduril462,

    Code:
    strcat (child2, "'password' ");
    Compile OK.
    When executed it appear error message - command not found!
    I try to run the openssl command line in prompt $ .... 'password' .... OK.
    Did I forgot something ..... ! please guide, thanks.
    Last edited by john6696; 06-02-2011 at 11:31 AM. Reason: typo error

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It probably means that the location of the openssl command isn't in your $PATH environment variable. Try
    Code:
    strcpy (child2, "/path/to/openssl aes-256-cbc -d -pass pass:")
    Of course, replace /path/to with the actual path to your openssl executable.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru anduril462,

    Code:
    strcpy (child2, "/usr/bin/openssl aes-256-cbc -d -pass pass:");
    compile OK. when run same error! command not found!
    Please guide, thanks.

    I use Ubuntu 10.04 LTS as my OS plateform (the Lucid Lynx - released in April 2010).

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What is the output of the printf("Executing...") line?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru anduril462,

    printf("Executing...") exactly like I type manually into the $prompt. It work manually. In C fail!

    so sad!

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No, anduril62 wants you to copy and paste the whole output of that line, which according to your original code should print the command line being executed.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru rags_to_riches,

    Sorry for being late reply to your thread!

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <string.h> 
    int main()
    {
      char child2[BUFSIZ];
      strcpy (child2, "./usr/bin/openssl aes-256-cbc -d -pass pass:");
      strcat (child2, "'password' ");
      strcat (child2, "-a -salt -in system.aes -out system.txt ");
      printf ("Executing %s\n", child2);
      system (child2);
      return 0;
    }

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're not getting it! What does this line:

    Code:
    printf ("Executing %s\n", child2);
    print to the screen when you run the program???

    I think you want this:

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <string.h> 
    int main()
    {
      char child2[BUFSIZ];
      strcpy (child2, "./usr/bin/openssl aes-256-cbc -d -pass pass:");
      strcat (child2, "\"password\" ");
      strcat (child2, "-a -salt -in system.aes -out system.txt ");
      printf ("Executing %s\n", child2);
      system (child2);
      return 0;
    }
    Last edited by rags_to_riches; 06-03-2011 at 05:14 AM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru rags_to_riches,

    Thanks! It's worked! By the way, how to give you a rate.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by rags_to_riches View Post
    Code:
      strcat (child2, "\"password\" ");
    @rags_to_riches: So the only difference is single vs double quotes. I mentioned both methods in my first post, thinking it shouldn't make a difference. Do you know why the shell actually cares about this when invoked via system, but not when it's a login shell? The following simple test on my system seems to suggest the shell doesn't care:

    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        printf("Testing single quotes: ls 'filename with spaces'\n");
        system("ls 'filename with spaces'");
        printf("Testing double quotes: ls \"filename with spaces\"\n");
        system("ls \"filename with spaces\"");
    
        return 0;
    }
    
    $ touch "filename with spaces"
    $ ls
    filename with spaces  foo.c
    $ gcc -Wall foo.c
    $ ./a.out
    Testing single quotes: ls 'filename with spaces'
    filename with spaces
    Testing double quotes: ls "filename with spaces"
    filename with spaces

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Guru anduril462,

    My fault. When I executed the command, I forgot to put dot in front of backslash. Sorry!

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    @anduril462: You're right, it doesn't matter. I rarely if ever use single quotes in the shell, so that was what jumped out at me, rather than the fact the path to openssl was relative to the current directory by use of the leading . rather than the correct path.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  2. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  3. system() command-line no window
    By yahn in forum C++ Programming
    Replies: 13
    Last Post: 05-05-2008, 11:38 AM
  4. Replies: 3
    Last Post: 06-01-2004, 04:29 PM
  5. system command
    By vk in forum C Programming
    Replies: 13
    Last Post: 04-03-2002, 07:44 PM