Encrypt And Decrypt Files With A 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||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.|
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
|-in||Specifies the data to decrypt.|
|-out||Specifies the file to put the decrypted data in.|
Base64 Encode and 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.
To encrypt file in Base64-encode, you should add -a option :
|-a||Tells OpenSSL that the encrypted data is in Base64-ensode.|
Option -a should also be added while decryption :
Non interactive Encrypt / Decrypt
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.
Encrypt a file using a supplied password :
Decrypt a file using a supplied password :