How To Encrypt And Decrypt Files
If you store sensitive data on your computer, it is always good to encrypt those files.
Imagine you have some sensitive data stored in a text file
$ cat foo.txt This is a secret. This is another secret.
How can we encrypt and decrypt that file?
Do not use this solution as it is possible to run a brute force attack on your encrypted files. Use GPG instead.
openssl aes-256-cbc -a -in foo.txt -out foo.txt.enc
You will be prompted to enter a password and verify it at the second prompt.
enter aes-256-cbc encryption password: Verifying - enter aes-256-cbc encryption password:
openssl command will create a new, encrypted file.
$ head foo.* ==> foo.txt <== This is a secret. This is another secret. ==> foo.txt.enc <== U2FsdGVkX1/J5wu+34jZ1dB0doEyZdKF9CEQQIfWJ+TaE7KTT/7iCrM6+hdhPOBU CQYPz2UYKIBUpauVwRk9ww==
You will have to delete the original, unencrypted file with
rm foo.txt yourself, as OpenSSL will leave it untouched.
If you want to decrypt the encrypted file
foo.txt.enc you can use the following command.
openssl aes-256-cbc -d -a -in foo.txt.enc -out foo.txt enter aes-256-cbc decryption password: $ head foo.* ==> foo.txt <== This is a secret. This is another secret. ==> foo.txt.enc <== U2FsdGVkX1+93Z76m1FHYpNaJd6jZCgIdAMPvZPMPY2v0fYbOComi/+Dk219YVGD Mk5xHrweewH/9kUneYh8oQ==
Again, do not use OpenSSL, use GPG instead.
gpg command works pretty much the same.
gpg --output foo.txt.enc --symmetric --cipher-algo AES256 foo.txt
Again, a new file
foo.txt.enc will be created, and the original file will remain untouched.
To decrypt the file, you can use the following command.
gpg --output foo.txt --decrypt foo.txt.enc
Please, remember that you have to delete the unencrypted file yourself in a safe manner as soon as you have the encrypted data.
Posted on CuteMachine.