Jay Taylor's notes

back to listing index

Encrypt & Decrypt Files With Password Using OpenSSL - ShellHacks

[web search]
Original source (www.shellhacks.com)
Tags: openssl command-line public-private-key-cryptography cryptography file-encryption www.shellhacks.com
Clipped on: 2020-02-05

ShellHacks
Linux Hacks and Guides

Encrypt & Decrypt Files With Password Using OpenSSL

OpenSSL is a powerful cryptography toolkit that can be used for encryption of files and messages.

If you want to use the same password for both encryption of plaintext and decryption of ciphertext, then you have to use a method that is known as symmetric-key algorithm.

From this article you’ll learn how to encrypt and decrypt files and messages with a password from the Linux command line, using OpenSSL.

HowTo: Encrypt a File

$ openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc
Options Description
openssl OpenSSL command line tool
enc Encoding with Ciphers
-aes-256-cbc The encryption cipher to be used
-salt Adds strength to the encryption
-in Specifies the input file
-out Specifies the output file.

Interesting fact: 256bit AES is what the United States government uses to encrypt information at the Top Secret level.

Warning: The -salt option should ALWAYS be used if the key is being derived from a password.

Without the -salt option it is possible to perform efficient dictionary attacks on the password and to attack stream cipher encrypted data.

The reason for this is that without the salt the same password always generates the same encryption key.

When the salt is being used the first eight bytes of the encrypted data are reserved for the salt: it is generated at random when encrypting a file and read from the encrypted file when it is decrypted.

HowTo: Decrypt a File

$ openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt
Options Description
-d Decrypts data
-in Specifies the data to decrypt
-out Specifies the file to put the decrypted data in

Base64 Encode & Decode

Base64 encoding is a standard method for converting 8-bit binary information into a limited subset of ASCII characters.

It is needed for safe transport through e-mail systems, and other systems that are not 8-bit safe.

By default the encrypted file is in a binary format.

If you are going to send it by email, IRC, etc. you have to save encrypted file in Base64-encode.

Cool Tip: Want to keep safe your private data? Create a password protected ZIP file from the Linux command line. Really easy! Read more →

To encrypt file in Base64-encode, you should add -a option:

$ openssl enc -aes-256-cbc -salt -a -in file.txt -out file.txt.enc
Option Description
-a Tells OpenSSL that the encrypted data is in Base64-ensode

Option -a should also be added while decryption:

$ openssl enc -aes-256-cbc -d -a -in file.txt.enc -out file.txt

Non Interactive Encrypt & Decrypt

Warning: Since the password is visible, this form should only be used where security is not important.

By default a user is prompted to enter the password.

If you are creating a BASH script, you may want to set the password in non interactive way, using -k option.

Cool Tip: Need to improve security of the Linux system? Encrypt DNS traffic and get the protection from DNS spoofing! Read more →

Public key cryptography was invented just for such cases.

Encrypt a file using a supplied password:

$ openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc -k PASS

Decrypt a file using a supplied password:

$ openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt -k PASS

16 Replies to “Encrypt & Decrypt Files With Password Using OpenSSL”

  1. Image (Asset 1/15) alt= error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:

  2. Image (Asset 2/15) alt= openssl enc -aes-256-cbc -d -in /Users/huntert/Desktop/IMPT.dmg -out /Users/huntert/Desktop/IMPT.dmg
    enter aes-256-cbc encryption password:
    Verifying – enter aes-256-cbc encryption password:

    When I tried to decrypt it, I received the folllowing messages:
    enter aes-256-cbc decryption password:
    error reading input file

    I should’ve been more cautious and tried it on a rubbish file. Lesson learned.

  3. Image (Asset 3/15) alt= -pass pass:
    or
    -pass file:
    works for ecoding & decoding

  4. Image (Asset 4/15) alt= I have a problem to decrypt a file
    I have encrypted my file after I used this file in matlab (modulation ..) and then I cannot decrypt the extracted file from matlab
    error is ” error reading file ”
    what I have to do ?

  5. Image (Asset 5/15) alt= thank you for this, very helpful.
    one thing I ask, where do I get the openssl (I assume executable) from to run the encrypt and decrypt?
    Neil

  6. Image (Asset 6/15) alt= If you want a solution i’m here
    Thanks
    Kurtis

  7. Image (Asset 7/15) alt= But how do we do this programmatically? I’ve a situation where I need to use a password to login to a server programmatically. We cannot hard code the password to decrypt the file. One option I know is to store the password in a vault but that takes too long/expensive to implement. Please suggest. thanks

    1. Image (Asset 8/15) alt= I used to deploy encrypted bash programs to production using openssl.
      I suggest looking at https://linuxconfig.org/using-openssl-to-encrypt-messages-and-files-on-linux
      Or,
      If your program is compiled code, then why not encrypt secret in a file (manually) then decrypt it in your code into a variable? Then, you can programmatically use it as input to your authentication process.
      Hope this helps.

      Here is a sample code in GNU-C:

      //------------------------------------
      int main (int argc, char *argv[], char *envp[]) {
        char *cmdstr = (char *) malloc (254);
        memset(cmdstr, '\0', sizeof(254));
        /* build the command string for encryption */
        strcpy(cmdstr, "openssl enc -aes-256-cbc -in file.txt -out file.txt.enc -iter 29 -pass pass:mysecret");
      
        /* encrypt */
        if (run_my_script (cmdstr) == -1)
          fprintf(stderr, "Failed to encrypt file\n");
      
        /* rebuilt the command string for decryption */
        strcpy(cmdstr, "openssl enc -aes-256-cbc -d -in file.txt.enc -out decrypted_file.txt -iter 29 -pass pass:mysecret");
      
        /* decrypt */
        if (run_my_script (cmdstr) == -1)
          fprintf(stderr, "Failed to decrypt file.txt.enc\n");
      
        free(cmdstr);
      
        return 0;
      }
      
      int run_my_script (const char *command) {
         int status;
         pid_t pid;
         pid = fork ();
         if (pid == 0) {
            /* This is the child process.  Execute the shell command. */
            execl (SHELL, SHELL, "-c", command, NULL);
            _exit (EXIT_FAILURE);
         }
         else if (pid < 0)
            /* The fork failed.  Report failure.  */
            status = -1;
         else
            /* This is the parent process.  Wait for the child to complete.  */
            if (waitpid (pid, &status, 0) != pid)
               status = -1;
         return status;
      }
  8. Image (Asset 9/15) alt= e.g.
    ENCRYPT (interactive):

    openssl enc -aes-256-cbc -in file.txt.enc -out file.txt  -iter 29 -k PASS

    DECRYPT (interactive):

    openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt -iter 29 -k PASS

    ENCRYPT (non-interactive):

    openssl enc -aes-256-cbc -in file.txt.enc -out file.txt  -iter 29 -pass pass:mysecret

    DECRYPT (non-interactive):

    openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt -iter 29 -pass pass:mysecret

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011-2019 ShellHacks. All rights reserved.
};