Trying to encrypt a text message via command line on OSX Yosomite 10.10.2
Created public .pem
key like this:
ssh-keygen -f ~/.ssh/id_rsa.pub
I faced this problem also and think a good hint is here:
How can I transform between the two styles of public key format, one "BEGIN RSA PUBLIC KEY", the other is "BEGIN PUBLIC KEY"
It seems that the OpenSSL encryption command wants a SSL public key instead of a RSA public key.
We now know enough to tweak the example to make it work. A SSL public key can be generated from a RSA public key with
openssl rsa -in id_rsa.pem -RSAPublicKey_in -pubout > id_pub.pem
It is then possible to do the encryption step with
openssl rsautl -encrypt -inkey id_pub.pem -pubin -in ~/Desktop/myMessage.txt -out ~/Desktop/encrypted.txt
The default OpenSSL command in MacOSX Yosemite as of this writing appears to be 0.9.8zg. The rsa command in this version does not support the capability to run the first command above. I worked around this by installing OpenSSL 1.0.1p.