Encrypt data with gnu gpg.

To encrypt a bash script install the shc download from http://www.datsi.fi.upm.es/~frosal/ .
To encrypt a folder read the encfs, to encrypt files read this article.
GNUpg is a utility that encrypt the files and disk images, to encrypt the folder first compress it with the tar and bzip2 then encrypt the archives with GNU gpg, install the gpg with with aptitude.

Step 1: Install gnupg package
apt install gnupg
gnu gpg working as user base unless you share the key with others, for this first create your own key pairs:
gpg –gen-key
Follow the insructions when complete you will see the following message:
gpg: key BB599FA8 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/BB599FA8 2013-05-05
      Key fingerprint = 47AD 5E09 0430 1738 A163  13E5 DF22 7039 BB59 9FA8
uid                  user (This is user gpg keys with mail [email protected]) 
sub   2048R/7FC66994 2013-05-05
Step 3: Important bits of information
Please note the following information from the above output which will be required as start playing around with gpg keys (as in Step 5).
Key ID: BB599FA8
Real Name: user
E-mail: [email protected]
Key fingerprint: 47AD 5E09 0430 1738 A163  13E5 DF22 7039 BB59 9FA8
Step 4: Check if your key was properly generated
gpg --list-keys

/home/shams/.gnupg/pubring.gpg
------------------------------
pub   2048R/BB599FA8 2013-05-05
uid                  user (This is user gpg keys with mail [email protected]) 
sub   2048R/7FC66994 2013-05-05
Step 5: Generate a public key
Now we will generate a public version of the private key that we generated just above. You can distribute this key freely to everyone and even post it on your website:
gpg -armor --output pubkey.txt --export shams  or
gpg --armor --output pubkey.txt --export [email protected]  or
gpg --armor --output pubkey.txt --export BB599FA8
Step 6: Send your key to the public server
Instead of sending your key to everybody individually, we can register our key to the GPG keyservers so that anyone can download it without contacting us. This is high recommended step.
gpg --send-keys --keyserver hkp://subkeys.pgp.net BB599FA8
Step 1: Backing up your private/public keys
This is one of the most important part of the entire series. We will learn how to manage our GPG keys – Backing it up, Restoring and Revoking (if need arises).
List your keys first and Select the KeyID which belogs to you, to backup your Public key give the following command:
gpg -ao mypub.key --export BB599FA8
This will create a file called “mypub.key”, to backup your Private key give the following command:
gpg -ao myprivate.key --export-secret-keys BB599FA8
Now store these two files (mypub.key and mypub.key) on a floppy disk, CD or USB drive and put it away to some secure and safe place.
Generate a revocation key
I will explain later (step 3) why we need to do this step. For now simply give the following command:
# gpg --output myrevoke.key --gen-revoke BB599FA8
and answer the few questions that will be presented to you, save this revocation key and a safe place.
Step 2: Restoring your GPG key
Now a time comes where you lost your GPG keys and you should like to restore it on another or same machine. To restore the keys give the following commands:
gpg --import myprivate.key
gpg --import mypub.key
Step 3: Revoking the GPG keys
Just pray that you don’t have to do this step ever because doing this step usually means that:
a) Your private key has been compromised
b) You lost your backup keys
c) You forgot your passphrase (password)
Now suppose you wan’t to revoke your key which basically means that you are not longer going to be using this key in future and would like to inform people on the Internet also.
First we need to revoke the key locally on your machine:
gpg --import myrevoke.key
Now we need to inform everybody on the Internet that we are revoking this key and people should not use this key to send you messages. this can be done by informing the keyservers just like when we informed about your newly created public key. To send the revoking information to the keyserver give the following command:
gpg ---send-keys --keyserver hkp://subkeys.pgp.net BB599FA8
Now anyone who tries to send you a message using your key which has NOW been revoked will get a message. However one needs to refresh their GPG keyring database to get the latest information on the keys. Also it is a good idea to refresh your keys on a regular basis so that you have up-to-date information. You can refresh your keys by following command:
gpg --refresh-keys --keyserver hkp://subkeys.pgp.net
Step 4: Deleting a key
Suppose you created too many keys while experimenting with GPG and now you are confused with all the keys around. You would like to delete all but one key. Here is how you can delete the extra keys.
First list keys and then delete the keys you want:
gpg –delete-secret-and-public-key BB599FA8
The above command removes the key from the private and public keyring.
Encrypting and decrypting documents
The public key is like an open safe, with a lock that can be open with the private key, The procedure for encrypting and decrypting documents is straightforward with this mental model. If you want to encrypt a message to shams, you encrypt it using user’s public key, and he decrypts it with his private key. If user wants to send you a message, he encrypts it using your public key, and you decrypt it with your private key.
gpg --output doc.gpg --encrypt --recipient [email protected] doc
gpg -e -r user TEST.tgz
The -recipient option is used once for each recipient and takes an extra argument specifying the public key to which the document should be encrypted. The encrypted document can only be decrypted by someone with a private key that complements one of the recipients’ public keys. In particular, you cannot decrypt a document encrypted by you unless you included your own public key in the recipient list.
gpg --output doc --decrypt doc.gpg
gpg -d -o TEST.tgz TEST.tgz.gpg


<< Previous Next >>