openssl this way can only encrypt small files:

openssl rsautl -encrypt -pubin -inkey public_key.pem -in secret.txt -out secret.enc

openssl as I found suggested here throws an error:

openssl smime -encrypt -aes-256-cbc -binary -in secret.txt -outform DER -out secret.txt.der public_key.pem

not that you're supposed to be using smime because that's for mail but still see the Error:

unable to load certificate
140222726453056:error:0909006C:PEM routines:get_name:no start line:crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE

Why is openssl complaining about a trust certificate? That's my business. I know it means that it wants the PEM to be of a different format rather than taking it personally.

What do I want?

I want to use bash to encrypt any file with strong encryption using a public PEM file (or other public key) so that my project counterpart can use their private key to decrypt it. It would be awesome if I could use powershell native tools as well but that's a big ask and just thought I'd throw it in, in hope of avoiding gitbash for Windows-homed recipients.

I could use gpg but we do not want to introduce generating password generated keys.

  • 1
    (1) openssl smime with -*form der actually does CMS, not SMIME, and is not limited to email, but both of those require an X.509 certificate, not a bare public key, for encrypt or verify. openssl supports two PEM formats for certs, the standard label CERTIFICATE or X.509 CERTIFICATE, see rfc7468 and a private format with some trust info added to the cert, labelled logically enough TRUSTED CERTIFICATE. PEM_read* can show only one label in the error message, so it uses the latter, even though the former is/are also valid and much more common. – dave_thompson_085 Oct 19 '20 at 2:03
  • 1
    (2) I don't know what you mean by 'password generated keys'. GPG/PGP keypaire are no more password generated than OpenSSL ones are. In fact, except for ElGamal which is used in PGP and not in OpenSSL, the keys are semantically equivalent although the representations are quite different, and they can be converted with a little effort; see e.g.. my unix.stackexchange.com/questions/276317/… . (The nonkey info in a cert -- identity and trust and usage -- are not equivalent.) – dave_thompson_085 Oct 19 '20 at 2:08
  • 1
    (0) If you want to use openssl, just use a dummy selfsigned cert to carry the publickey, like everybody else. If this not perfectly elegant workaround offends you esthetically, go buy some artwork or music or something. – dave_thompson_085 Oct 19 '20 at 2:12

Well, the first problem is that you really don't want to encrypt a large file with an asymmetric cipher. Nobody does this, as they are slow, and limited in size in any case. What you do is create a session key for a symmetric cipher, encrypt the session key with the asymmetric cipher, encrypt the large file with the symmetric cipher, and then package things together. This is what both SSL and PGP/GPG do.

The rsautil command you found is a low level tool about doing the asymmetric encoding. But you would need several other commands to do the full process.

There are two basic solutions.

  1. Sign your public key with your private key to create a certificate. This should let openssl smime work.
  2. Use GPG. This is what it is designed for. I recommend this for what you've described. Probably gpg -se, or gpg -sea if you are emailing. (Don't do gpg -c, which I think is your "password generated keys".)

Using either SSL or GPG, both parties must generate public and private keys, sign the public key with the private key, and send the signed public key to the other party. Generally, the only passwords involved are about encrypting the private key so that somebody who can see your files can't decrypt your data.

The major difference between SSL and PGP/GPG is the certification model. SSL uses "Certificate Authorities". PGP/GPG uses a chain of certificates, where Anne signs Bob's key, and Bob signs Carol's key, and thus Anne can trust that Carol's key is belongs to Carol.

Finally, if you are emailing the files, you might just look to see if you can get either SMIME or PGP integration in your email client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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