user541686
user541686

Reputation: 210445

Why can't I encrypt with OpenSSL?

I'm having trouble creating a public/private key pair and using it to encrypt a file.

First I generate the private key:

openssl genrsa -out private.pem 2048

Then I extract the public key:

openssl rsa -in private.pem -out public.pem -outform PEM -pubout

so that I can encrypt my file:

openssl rsautl -encrypt -inkey public.pem -in myfile.txt -out file.ssl

but then I'm told:

Loading 'screen' into random state - done
unable to load Private Key
7064:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:696:
    Expecting: ANY PRIVATE KEY

I don't understand... why is it trying to load a private key? How do I encrypt a file using the public key?

Upvotes: 3

Views: 5689

Answers (1)

emboss
emboss

Reputation: 39620

You forgot to specify that it's a public key, it needs to be done explicitly. This works (I'm using pkeyutl, the specific "utl"s are deprecated):

openssl pkeyutl -encrypt -in myfile.txt -pubin -inkey public.pem -out file.ssl

You have to be careful where you put the -pubin - if it is specified after the -inkey public.pem for example, you will again receive the same error as before. Yeah, I know.

Upvotes: 7

Related Questions