4

I have some .SQL files in specific folder, so I'd like to encrypt them using OpenSSL.

This apparently doesn't work:

openssl aes-256-cbc -in *.sql -out *.sql.enc -pass ...
9

Use "find"

OpenSSL is very finicky with its command line parameters. It does not give good error messages at all. And it's easy to mess things up and not notice.

Try this:

Generate some input files

$ for i in $(seq 9); do echo "This-is-file-#$i." > $i.sql; done


$ tail *.sql
==> 1.sql <==
This-is-file-#1.

==> 2.sql <==
This-is-file-#2.

==> 3.sql <==
This-is-file-#3.

==> 4.sql <==
This-is-file-#4.

==> 5.sql <==
This-is-file-#5.

==> 6.sql <==
This-is-file-#6.

==> 7.sql <==
This-is-file-#7.

==> 8.sql <==
This-is-file-#8.

==> 9.sql <==
This-is-file-#9.

Store password in environment variable

(I have a bad feeling about this. No idea how bad this is. But this will have to do for this demo. I clean Bash shell command history but I suspect that this is not enough. -- Someone else will have to answer that in another question.)

$ export mypasswordvariable='Pa$$w0rd'

$ history -c

$ echo -n $mypasswordvariable | xxd
00000000: 5061 2424 7730 7264                      Pa$$w0rd

Encrypt

$ find -maxdepth 1 -type f -iname '*.sql' -exec openssl aes-256-cbc -pass env:mypasswordvariable -base64 -salt -in \{\} -out \{\}.enc \;



$ tail *.enc
==> 1.sql.enc <==
U2FsdGVkX19njV2FbnxtQNEZfOZM49YmCSYCE4cOeKeLZDrfovbzad+dFoxds+uN

==> 2.sql.enc <==
U2FsdGVkX189IIeDm3mJEA8EG56I/mAzasXrtNhEDY8MT1/IOkHS/WKDHH7h69Fo

==> 3.sql.enc <==
U2FsdGVkX1/y3eRAFIIbsreix9nhpg611t3w0jn19Px8lBTjeE44rItSwwaaCudt

==> 4.sql.enc <==
U2FsdGVkX1/jHEXUfqzr0mdtChCIeQhFpRe+P0cOSeJf0Er96fDKD0nKV0nwPpL3

==> 5.sql.enc <==
U2FsdGVkX1/ask3K8Zt0bgvP/u5SL6hiFBz7u6Msc3LgRQXwa9Qgr4o04A8melkl

==> 6.sql.enc <==
U2FsdGVkX1+YRUF/nBKBdV4XIcyyyZBd7slaXMIQCSIvUnEeSkaR6GPYdE57Z1ng

==> 7.sql.enc <==
U2FsdGVkX1/zJSXlPNNJmMO4dfSqyGkweD+wAftmgOHgpT0NIEoexX0pRKQLPDkR

==> 8.sql.enc <==
U2FsdGVkX1/7i3jxHVTbqjA8JycR0+/46nWpJZHQ9HhGAFjRuszi/YLFe+2cWSMG

==> 9.sql.enc <==
U2FsdGVkX1/Zg7v5fgAJWUuBmb1mz/2cGwTwLSNbTHeSsCs3YtBe1fKpjY5CFIJ/

Decrypt

$ find -maxdepth 1 -type f -iname '*.sql.enc' -print -exec openssl aes-256-cbc -d -pass env:mypasswordvariable -base64 -salt -in \{\} \;
./1.sql.enc
This-is-file-#1.
./2.sql.enc
This-is-file-#2.
./3.sql.enc
This-is-file-#3.
./4.sql.enc
This-is-file-#4.
./5.sql.enc
This-is-file-#5.
./6.sql.enc
This-is-file-#6.
./7.sql.enc
This-is-file-#7.
./8.sql.enc
This-is-file-#8.
./9.sql.enc
This-is-file-#9.

Yup. Decryption works.

  • +1 for your effort, but I expected one line answer, lol. – Mirsad Dec 25 '16 at 15:09
  • 1
    @mirsad why it needs to be in "one line"? – Braiam Dec 25 '16 at 16:50
  • 3
    You don't really need find for this, unless you actually need to recurse through multiple directories. For files in a single directory, a simple for f in *.sql; do openssl enc -in "$f" -out "$f.enc" ...; done loop would work just as well. – Ilmari Karonen Dec 25 '16 at 17:02
  • 5
    @mirsad under the big header Encryption you can easily spot the loop "find -type f -iname '*.sql' -exec openssl", which is one line. The answer you got is exactly why i love this site, people that go above and beyond to try and enable the reader to do what they wanted, and more. Things they did not know the program could also do. But if spotting the one-liner was too much effort, trying a few shell loops before asking other people to do it for you probably was too. – J.A.K. Dec 25 '16 at 17:31
  • It's all cool. I use these answers to learn the stuff myself. So I made it a full proof of concept. ^^ – StackzOfZtuff Dec 26 '16 at 8:14

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