I want to encrypt a file using AES-256. How can I do that quickly and easily, and how can I - or someone else -decrypt it again?
I want to encrypt a file using AES-256. How can I do that quickly and easily, and how can I - or someone else -decrypt it again?
Unfortunately, there is no easy solution to securing your stuff. Think about your use-case, maybe something other than plain AES is better suited.
If you want very simple platform independent encryption, you can use openssl.
Please note: You can use this to hide birthday-gift-ideas.txt from your roommate, but don't expect it to be secure against a determined attacker!
If you still want to use openssl:
Encryption:
openssl aes-256-cbc -in attack-plan.txt -out message.enc
Decryption:
openssl aes-256-cbc -d -in message.enc -out plain-text.txt
You can get openssl to
base64
-encode the message by using the-a
switch on both encryption and decryption. This way, you can paste the ciphertext in an email message, for example. It'll look like this:Note that you have a choice of ciphers and modes of operation. For normal use, I recommend aes 256 in CBC mode. These are the ciphers modes you have available (only counting AES):
See also:
Please note:
OpenSSL will ask you for a password. This is not an encryption key, it is not limited to 32 bytes! If you're going to transfer files with someone else, your shared secret should be very strong. You can use this site to get a sense of how good your password is:
Warning: I have checked that these sites don't send your password to the server, but that can change at any time. Use these sites with dev tools / inspector and check if they send anything before typing in your strong password.
I like to use the
gpg
command:Encrypt:
Shorthand:
This will ask for a passphrase.
Decrypt:
Shorthand:
You can also add
cipher-algo AES256
to~/.gnupg/gpg.conf
to make AES256 the default. (According to manpage it is CAST5)7z (when the password option is used) uses a 256bit AES encryption (with SHA256 key stretching).
Install it (
p7zip-full
), right click on a file or directory you want to encrypt, and choose Compress, .7z and Other options /Password.For decryption, right click on the .7z file and choose Extract here.
aescrypt
The linked website contains an open-source 256-bit aes encrypt/decrypt tool and is multiplatform - MacOs, Windows, Linux and others through Java.
Encrypt:
aescrypt -e <file>
Decrypt:
aescrypt -d <file>
You could backup and encrypt your home folder using the syntax:
ubuntu installation
Download and extract the source
other platforms
Download the binaries or source-code from the website.
A lot of the suggestions I would have made have already been put forth in this thread. Basically, openssl is really the easiest way to go about encrypting a file or script. However, I would caution against using AES-256 just because it is not available in all versions of openssl on some platforms. Most newer OSes...i.e. Linux have it. But others such as AIX 5.3 do not (i think HP-UX as well). If you intend to use your file or script across different platforms, I strongly recommend using AES-128 because this is available everywhere.
How can you "quickly and easily" encrypt a file using AES-128?
A site like www.ShellScrypt.com uses openssl AES-128 quite intensely to encrypt shell scripts and then makes the encrypted copies of the scripts executable. All you have to do is paste the script to the site, and a zip file will be generated for you. That zip file will contain the encrypted (and executable if it is a script) version of your file. This allows you to "easily" and "conveniently" encrypt a file/script without having to satisfy any package or module requirement on every system you intend to use the script on or run several complex and confusing incantations of openssl commands.
Shown below is a basic encrypt / decrypt openssl command that uses AES-128:
Mentioning recent option added to
openssl
since OpenSSL 1.1.0. Building on stefano-palazzo answer:It uses a Key Derivation Function, lacking it would make bruteforcing password a lot easier.
For decrypting use:
Explaining arguments:
enc
stands for encryption-aes-256-cbc
is a good way of using aAES
cipher-a
base64 your data after encryption or before decryption-d
decrypt-e
encrypt-in
input file-out
output file-pbkdf2
streches the key to it would be hard to break Key Derivation Function-iter
iterations to strech the key, more means more security and adequate number is described here-md sha512
is replacing the hash function of PBKDF2 with SHA512 which is more secure than the default SHA256-salt
not mentioned in the command because it is set by default and is a very good way to increase security, why is that is described hereAdding to Stefano Palazzo's answer, I created a little bash function that works similarly to the base64 command.
It will aes256 encrypt a file, and then base64 encode it. When doing the reverse, it will base64 decode, decrypt, and then spit out the original plaintext.
Usage: