How do i use openssl public key and private key to perform encryption on a file?



  • Man page for openssl rsa.
    Man page for openssl rsautl.

           -pubout
               By default a private key is output: with this option a public key
               will be output instead. This option is automatically set if the
               input is a public key.
    
          -pubin
               The input file is an RSA public key.
    

    First, create the private key using RSA.

    $ openssl genrsa -out private_key.pem 2048
    

    Create the public key using the private key.

    $ openssl rsa -in private_key.pem -out public_key.pem -outform pem -pubout
    

    Get a sample file ready. For example:

    $ cat sample_file.txt 
    Working from the Pacific ocean.
    

    Encrypt the file using the public key.

    $ openssl rsautl -encrypt -inkey public_key.pem -pubin -in sample_file.txt -out sample_file.dat
    

    Decrypt the file using the private key.

    $ openssl rsautl -decrypt -inkey private_key.pem -in sample_file.dat -out sample2_file.txt
    

Log in to reply
 

© Lightnetics 2024