Proof of concept how to secure your messaging with RSA encryption. Using default Mac OS systems console terminal.
Generate private key with custom path
openssl genpkey -algorithm RSA -out ~/Desktop/private_key_user_A.pem -pkeyopt rsa_keygen_bits:4096
Generate public key from this private key
openssl rsa -pubout -in ~/Desktop/private_key_user_A.pem -out ~/Desktop/public_key_user_A.pem
To view them
cat private_key_user_A.pem
cat public_key_user_A.pem
Generate private key with custom path
openssl genpkey -algorithm RSA -out ~/Desktop/private_key_user_B.pem -pkeyopt rsa_keygen_bits:4096
Generate public key from this private key
openssl rsa -pubout -in ~/Desktop/private_key_user_B.pem -out ~/Desktop/public_key_user_B.pem
To view them
cat private_key_user_B.pem
cat public_key_user_B.pem
openssl pkeyutl -encrypt -in ~/Desktop/message.txt -out ~/Desktop/encrypted_message.bin -inkey ~/Desktop/public_key_user_B.pem -pubin
openssl pkeyutl -decrypt -in ~/Desktop/encrypted_message.bin -out ~/Desktop/decrypted_message.txt -inkey ~/Desktop/private_key_user_B.pem
For long text, more than a few words or lines.
openssl rand -out ~/Desktop/aes_key.bin 32
openssl enc -aes-256-cbc -salt -pbkdf2 -in ~/Desktop/message.txt -out ~/Desktop/encrypted_message.enc -pass file:/Users/admin/Desktop/aes_key.bin
openssl pkeyutl -encrypt -in ~/Desktop/aes_key.bin -out ~/Desktop/encrypted_aes_key.bin -inkey ~/Desktop/public_key_user_B.pem -pubin
openssl pkeyutl -decrypt -in ~/Desktop/encrypted_aes_key.bin -out ~/Desktop/decrypted_aes_key.bin -inkey ~/Desktop/private_key_user_B.pem
openssl enc -d -aes-256-cbc -pbkdf2 -in ~/Desktop/encrypted_message.enc -out ~/Desktop/decrypted_message.txt -pass file:/Users/admin/Desktop/decrypted_aes_key.bin