6

How can I trick the -in argument of the OpenSSL command line tool in order to get data from string instead a file?

Normally, I could use echo command to do it:

echo 'test string 1' | openssl enc -aes-256-cbc -a -salt -pass pass:mypassword

Is there a way I can do it without echo and pipe? Similar to the -pass pass: argument?

Thanks in advance!

2 Answers 2

6

If your shell is bash and your OS supports it, you can use process substitution:

openssl enc -in <(echo 'test string 1') -aes-256-cbc -a -salt -pass pass:mypassword
3
  • Hi, it works fine from my terminal, the problem is that I'm using NSTask in Objective-C to call the tool and that does not work there... thanks.
    – TCB13
    Jan 20, 2012 at 3:32
  • You can use "/bin/bash -c ..." to feed a command to bash. This has other issues (shell metacharacters leading to security holes etc.), but it would let the process substitution work.
    – Nemo
    Jan 20, 2012 at 4:36
  • yes... "security holes" I'm using OpenSSL here... so as you might understand I care about having some security. (At least security provided by an NSPipe). Thanks.
    – TCB13
    Jan 20, 2012 at 4:42
3

I found a way to go around this! Instead of passing everything before and since openssl has an interactive mode, it's possible to run the command without input:

openssl enc -aes-256-cbc -a -salt -pass pass:mypassword

And OpenSSL will be waiting for data to encrypt. This can be also useful for streams!

Then type in the string or data you want to encrypt and send a EOT (End of Transmission) in Terminal is usual ^D Control+D it it will output to stdout the encrypted string!

Hope this may help someone some day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.