Skip to content

MacOS openssl command

The MacOS openssl command allows users to perform a wide range of cryptographic operations, such as generating RSA key pairs, creating digital signatures, and encrypting data. It enables secure communication over networks by implementing various encryption algorithms like AES, DES, and RSA. With openssl, users can also verify the authenticity of digital certificates, convert certificate formats, and troubleshoot SSL/TLS connections. This powerful tool provides a comprehensive suite of functions for managing cryptographic tasks on MacOS systems, making it indispensable for secure data transmission and encryption.

openssl Syntax:

Terminal window
openssl [command] [options] [parameters]

Options:

OptionDescription
-helpDisplay this summary of options
-ciphersList of all supported ciphers
-randLoad the file(s) into the random number generator
-statePrint the SSL session state
-versionDisplay version information

Parameters:

ParameterDescription
inputfileInput file for reading data
outputfileOutput file for writing data
certificatefileFile containing one or more certificates
privatekeyfileFile containing a private key
ciphersuiteCipher suite to use for the connection
destinationWhere to send the output

openssl Usage:

Generate a new RSA private key and certificate

Terminal window
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

This command generates a new RSA private key and self-signed certificate, storing the key in “key.pem” and certificate in “cert.pem”, valid for 365 days.

Verify the fingerprint of a certificate

Terminal window
openssl x509 -noout -fingerprint -in cert.pem

Verifies the fingerprint of a certificate file “cert.pem” without displaying the certificate details.

Encrypt a file using AES-256-CBC

Terminal window
openssl enc -aes-256-cbc -salt -in plaintext.txt -out ciphertext.enc -pass pass:mypassword

Encrypts the file “plaintext.txt” using AES-256-CBC algorithm, storing the result in “ciphertext.enc” with a password “mypassword”.

Decrypt a file using AES-256-CBC

Terminal window
openssl enc -d -aes-256-cbc -in ciphertext.enc -out plaintext_decrypted.txt -pass pass:mypassword

Decrypts the file “ciphertext.enc” previously encrypted with AES-256-CBC, saving the decrypted content in “plaintext_decrypted.txt” with the correct password “mypassword”.

How do I generate a new RSA private key using openssl in MacOS?

To generate a new RSA private key using openssl in MacOS, execute the following command:

Terminal window
openssl genrsa -out private_key.pem 2048

How do I create a self-signed SSL certificate using openssl in MacOS?

To create a self-signed SSL certificate using openssl in MacOS, execute the following command:

Terminal window
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365

How do I encrypt a file using openssl in MacOS?

To encrypt a file using openssl in MacOS, execute the following command:

Terminal window
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.txt

How do I decrypt a file using openssl in MacOS?

To decrypt a file using openssl in MacOS, execute the following command:

Terminal window
openssl enc -d -aes-256-cbc -in encrypted.txt -out decrypted.txt

How do I generate a SHA-256 hash of a file using openssl in MacOS?

To generate a SHA-256 hash of a file using openssl in MacOS, execute the following command:

Terminal window
openssl dgst -sha256 filename.txt

How do I convert a certificate file from PEM to PKCS#12 format using openssl in MacOS?

To convert a certificate file from PEM to PKCS#12 format using openssl in MacOS, execute the following command:

Terminal window
openssl pkcs12 -export -out certificate.pfx -inkey private_key.pem -in cert.pem

How do I verify the digital signature of a file using openssl in MacOS?

To verify the digital signature of a file using openssl in MacOS, execute the following command:

Terminal window
openssl dgst -sha256 -verify public_key.pem -signature signature.bin file.txt

Applications of the openssl command

  1. Check SSL certificate expiration date: openssl x509 -enddate -noout -in certificate.crt
  2. Generate a new private key and certificate signing request (CSR): openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout privateKey.key
  3. Generate a self-signed certificate: openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt
  4. Verify a certificate chain: openssl verify -CAfile ca-bundle.crt certificate.crt
  5. Encrypt a file using a password: openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc
  6. Decrypt an encrypted file: openssl enc -d -aes-256-cbc -in file.txt.enc -out file.txt
  7. Convert a certificate from PEM to DER format: openssl x509 -outform der -in certificate.pem -out certificate.der
  8. Create a hash digest of a file: openssl dgst -sha256 file.txt