Tag: Encrypt

  • Encrypt a file using gpg

    To encrypt a file using gpg command, run

    gpg -o myfile.tar.gpg --symmetric myfile.tar
    

    Now it will ask for a password. Once you enter password 2 times, file will be encrypted.

    Decrypt

    To decrypt a file, run

    gpg myfile.tar.gpg
    

    It will ask for the password, once you enter password, get file decrypted.

    Example

    gpg decrypt

    This encryption process will also compress the file.

    -rw-r--r--   1 root root 1559767040 Oct  1 08:35 home-boby.tar
    -rw-r--r--   1 root root  666947870 Oct  1 08:24 home-boby.tar.gpg
    

    In above example, 1.5 GB tar file get compressed into 600 MB file when encrypted with gpg

  • Encrypting a file using OpenSSL

    To encrypt a file, run

    openssl enc -aes-256-cbc -in FILE -out FILE.enc
    

    To decrypt, use

    openssl enc -d -aes-256-cbc -in FILE.enc -out FILE
    

    You can use -k option to provide password in commend line itself so it won’t prompt for password.

    Here is a script i use to take backup

    boby@hon-pc-01:~$ cat ~/bin/backup-my-files 
    #!/bin/bash
    
    cd $HOME/work/
    rm -f myfiles.tar.gz myfiles.tar.gz.openssl
    tar --exclude='MY_FILES/.git' -zcvf myfiles.tar.gz MY_FILES
    openssl enc -aes-256-cbc -in /home/boby/work/myfiles.tar.gz -out myfiles.tar.gz.openssl
    
    echo "Backup available in folder $HOME/work"
    echo "You can decrypt file using"
    echo ""
    echo "openssl enc -d -aes-256-cbc -in myfiles.tar.gz.openssl -out myfiles.tar.gz"
    
    boby@hon-pc-01:~$ 
    

    openssl

    Encrypt