{"id":2131,"date":"2013-06-27T11:56:42","date_gmt":"2013-06-27T03:56:42","guid":{"rendered":"http:\/\/rmohan.com\/?p=2131"},"modified":"2013-06-27T12:04:02","modified_gmt":"2013-06-27T04:04:02","slug":"gpg-file-encryption-command-line-in-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2131","title":{"rendered":"GPG file encryption command line in Linux"},"content":{"rendered":"<p>GPG file encryption using key pair<br \/>\nGPG basic file encryption doesn\u2019t required public\/private key. But to make a encrypted file more secure you can use RSA\/DSA algorithms. These algorithms generates public and private keys to encrypt file.<\/p>\n<p>There are three criteria to use GPG file encryption.<\/p>\n<p>1. Encrypt\/Decrypt file locally for same user account.<br \/>\n2. Encrypt file for other user.<br \/>\n3. Decrypt other users file.<\/p>\n<p>The very first step is to generate key pair using below command.<\/p>\n<p># gpg &#8211;gen-key<br \/>\nOutput:<\/p>\n<p>gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.<br \/>\nThis program comes with ABSOLUTELY NO WARRANTY.<br \/>\nThis is free software, and you are welcome to redistribute it<br \/>\nunder certain conditions. See the file COPYING for details.<\/p>\n<p>Please select what kind of key you want:<br \/>\n(1) DSA and Elgamal (default)<br \/>\n(2) DSA (sign only)<br \/>\n(5) RSA (sign only)<br \/>\nYour selection?<br \/>\nDSA keypair will have 1024 bits.<br \/>\nELG-E keys may be between 1024 and 4096 bits long.<br \/>\nWhat keysize do you want? (2048)<br \/>\nRequested keysize is 2048 bits<br \/>\nPlease specify how long the key should be valid.<br \/>\n0 = key does not expire<br \/>\n= key expires in n days<br \/>\nw = key expires in n weeks<br \/>\nm = key expires in n months<br \/>\ny = key expires in n years<br \/>\nKey is valid for? (0)<br \/>\nKey does not expire at all<br \/>\nIs this correct? (y\/N) y<\/p>\n<p>You need a user ID to identify your key; the software constructs the user ID<br \/>\nfrom the Real Name, Comment and Email Address in this form:<br \/>\n&#8220;Heinrich Heine (Der Dichter) &#8221;<\/p>\n<p>Real name: Mohan Ramadoss<br \/>\nEmail address: rmohan@rmohan.com<br \/>\nComment: System Admin<br \/>\nYou selected this USER-ID:<br \/>\n&#8220;Mohan Ramadoss (System Admin) &#8221;<\/p>\n<p>Change (N)ame, (C)omment, (E)mail or (O)kay\/(Q)uit? o<br \/>\nYou need a Passphrase to protect your secret key.<\/p>\n<p>We need to generate a lot of random bytes. It is a good idea to perform<br \/>\nsome other action (type on the keyboard, move the mouse, utilize the<br \/>\ndisks) during the prime generation; this gives the random number<br \/>\ngenerator a better chance to gain enough entropy.<br \/>\n++++++++++++++++++++..+++++++++++++++.++++++++++++++++++++.+++++.+++++.+++++++++++++++++++++++++.+++++.++++++++++++++++++++++++++++++&#8230;+++++&gt;+++++..+++++&gt;+++++&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..+++++<\/p>\n<p>Not enough random bytes available. Please do some other work to give<br \/>\nthe OS a chance to collect more entropy! (Need 284 more bytes)<\/p>\n<p>We need to generate a lot of random bytes. It is a good idea to perform<br \/>\nsome other action (type on the keyboard, move the mouse, utilize the<br \/>\ndisks) during the prime generation; this gives the random number<br \/>\ngenerator a better chance to gain enough entropy.<br \/>\n+++++..+++++.++++++++++..++++++++++.++++++++++..++++++++++..+++++++++++++++.+++++..+++++.++++++++++.+++++.++++++++++.++++++++++..++++++++++++++++++++++++++++++..+++++&gt;++++++++++.&gt;+++++&gt;+++++.&lt;+++++&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&gt;+++++&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;+++++^^^<br \/>\ngpg: \/root\/.gnupg\/trustdb.gpg: trustdb created<br \/>\ngpg: key 2AE39E50 marked as ultimately trusted<br \/>\npublic and secret key created and signed.<\/p>\n<p>gpg: checking the trustdb<br \/>\ngpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model<br \/>\ngpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u<br \/>\npub 1024D\/2AE39E50 2013-03-14<br \/>\nKey fingerprint = 0D89 4697 E22A A6CC 3017 5EA1 0389 ED6D 2AE3 9E50<br \/>\nuid Mohan Ramadoss (System Admin)<br \/>\nsub 2048g\/9102AC9C 2013-03-14<br \/>\n1. Encrypt\/Decrypt file locally for same user account.<\/p>\n<p>Encrypt file for single user only. No one other can decrypt this file.<\/p>\n<p># gpg &#8211;encrypt &#8211;recipient &#8216;Mohan Ramadoss&#8217; rmohan.txt<br \/>\n\u2013recipient name should be same as used in key generation. Above command will automatically generate a encrypted file named rmohan.txt.gpg<\/p>\n<p>Decrypt your own file rmohan.txt.gpg<\/p>\n<p># gpg &#8211;output rmohan2.txt &#8211;decrypt rmohan.txt.gpg<br \/>\n\u2013output or -o is used to specify output filename. Above command will prompt for passphrase used in key pair.<\/p>\n<p>2. Encrypt file for other user.<\/p>\n<p>Use #1 steps to generate encrypted file. Finally share your public key and encrypted file ( rmohan.txt.gpg ) with other users. To export your public key use following command.<\/p>\n<p># gpg &#8211;armor &#8211;output pubkey.txt &#8211;export &#8216;Mohan Ramadoss&#8217;<\/p>\n<p>Check your public key. It should be like below<\/p>\n<p># cat pubkey.txt<br \/>\n&#8212;&#8211;BEGIN PGP PUBLIC KEY BLOCK&#8212;&#8211;<br \/>\nVersion: GnuPG v1.4.5 (GNU\/Linux)<\/p>\n<p>mQGiBFFBPwsRBADWAxKxCxLsZ1ZJ512auBkEw51HlF5+k18Yp1giOqIYtbRUPVeq<br \/>\nY5o5knVKjlJDlVA0\/rGh18fbKgubjZl1PL1R+tT0bMIDIs5+hg+S60nSlxBGOhYz<br \/>\n8h+nu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h9LBBRDbpSl7UPy9s+BRpuTUq8lk69yn6tb63TWATcAAwUH\/i7wnh4Vx\/0HBYWo<br \/>\n8AjY3iqaIjTW63H70PXKwn7yxLJW2xqSQ2Sre9h9J\/arZUwerky3tS3xdcEEGTSP<br \/>\noXgeg2passygEAnlGUEws2BZ+3XVbSVhh+vMmqOMwz1J1GUKTOWH5vVCsfYdMmb\/<br \/>\n4GnxV2iAKeskPRwZFHujaHA0tvlGtRXjFXqxiBuSZjgv0W93sbZQamArCYaJtwT0<br \/>\n2ZgrrUAVEs8JMTbSfrQEmXpfhk2mFcrf2ocAC6LwxMYZESDW6YhlY+1utEqpN6al<br \/>\nMefh1g42JK2g4OV42iP7op0JeQp6emJFywumlTrnihycRXEElxur+23NSLQpeNaw<br \/>\nM6gUQPeISQQYEQIACQUCUUE\/EAIbDAAKCRADie1tKuOeUB+VAJ9e8PprKcX3qvnW<br \/>\nrU8MhhXMB\/G0PQCfehvC8uWR\/TLtHyu5pjzCEtcsX\/E=<br \/>\n=k3yQ<br \/>\n&#8212;&#8211;END PGP PUBLIC KEY BLOCK&#8212;&#8211;<br \/>\n3. Decrypt other users file.<\/p>\n<p>To decrypt other users file, It required public key of that user. Import that public key to your account using below command. For example other users public file is otherpub.txt<\/p>\n<p># gpg &#8211;import otherpub.txt<br \/>\nMake sure that file has been imported successfully using below command.<\/p>\n<p># gpg &#8211;list-keys<br \/>\nAbove command will show all public key in your account. Make sure the other users public key also exist there.<\/p>\n<p>Now you can decrypt the other users file using below command<\/p>\n<p># gpg &#8211;output otheruserfile.txt otheruserfile.txt.gpg<br \/>\nGPG file encryption command line in Linux<\/p>\n<p>AS we know, now a days it\u2019s not safe to send and receive data over internet. There are many option available to secure your data while traveling over internet. GnuPG also provides you to encrypt your data on key basis. GPG is an encryption and signing tool for UNIX\/LINUX like OS.<\/p>\n<p>GnuPG provides many methods for file encryption and decryption.<\/p>\n<p>Method (A): Use Basic GPG encryption<\/p>\n<p>Step 1: First create a file to encrypt using GPG.<\/p>\n<p># echo &#8220;Enter file content here&#8221; &gt; secureit.txt<br \/>\nStep 2: Encypt above created file using gpg.<\/p>\n<p># gpg -c secureit.txt<br \/>\nAbove command will create a encrypted file named secureit.txt.gpg. Original file will remain same.<\/p>\n<p># ls -l<br \/>\ntotal 8<br \/>\n-rw-r&#8211;r&#8211; 1 root root 24 Mar 9 21:36 secureit.txt<br \/>\n-rw-r&#8211;r&#8211; 1 root root 74 Mar 9 21:36 secureit.txt.gpg<br \/>\nStep 3: Finally how to decrypt file again.<\/p>\n<p># gpg -o secureit-new.txt -d secureit.txt.gpg<br \/>\ngpg: CAST5 encrypted data<br \/>\nEnter passphrase:<br \/>\nYou will get a new decypted file named secureit-new.txt.<\/p>\n<p># ls -l secureit-new.txt<br \/>\n-rw-r&#8211;r&#8211; 1 root root 24 Mar 9 21:56 secureit-new.txt<br \/>\nThanks for using this article.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GPG file encryption using key pair GPG basic file encryption doesn\u2019t required public\/private key. But to make a encrypted file more secure you can use RSA\/DSA algorithms. These algorithms generates public and private keys to encrypt file.<\/p>\n<p>There are three criteria to use GPG file encryption.<\/p>\n<p>1. Encrypt\/Decrypt file locally for same user account. 2. [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2131"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2131"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2135,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2131\/revisions\/2135"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}