{"id":468,"date":"2012-07-04T16:33:18","date_gmt":"2012-07-04T08:33:18","guid":{"rendered":"http:\/\/rmohan.com\/?p=468"},"modified":"2013-03-28T17:37:22","modified_gmt":"2013-03-28T09:37:22","slug":"encrypt-files-using-gnupg","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=468","title":{"rendered":"Encrypt Files Using GnuPG"},"content":{"rendered":"<h1><a title=\"Permanent Link to How To Encrypt Files Using GnuPG\" href=\"http:\/\/www.sohailriaz.com\/how-to-encrypt-files-using-gnupg\/\" rel=\"bookmark\">Encrypt Files Using GnuPG <\/a><\/h1>\n<p>HowTo we will discuss to encypt files using GnuPG. Encryption is a method which protect data stored on your computer or sending over the network from compromise. It can be used to ensure and verify data comes from a rightful owner, and also to maintain confidentiality of the data. We will used a tool GnuPG (GNU Privacy Guard) to encrypt individual files or validate files.<\/p>\n<p>GnuPG is an opensource implementation of the OpenPGP public key encryption system. Public Key Encryption uses asymmetric encryption, in which a matching pair of public and private keys are used to encrypt or decrypt. A person who accomplished this has to generate two keys i.e Private Key and Public Key.<\/p>\n<p>Private Key is the one kept by owner secretly and what is encrypt by private key can decrypt by the one who has the matching public key or what is encrypt by the public key by anyone can decrypt by the private key owner. Beside encryption it also verify that messages comes from the holder of the private or public keys.<\/p>\n<h3>1) Generate Keys<\/h3>\n<p>Use following command to generate Public and Private Keys.<\/p>\n<blockquote>\n<p><span style=\"color: #ff0000;\">gpg \u2013gen-key<\/span><\/p>\n<p>It will ask series of questions, you can answer as per your need but this is what I used for example.<\/p>\n<blockquote>\n<p>Please select what kind of key you want:<br \/> (1) RSA and RSA (default)<br \/> (2) DSA and Elgamal<br \/> (3) DSA (sign only)<br \/> (4) RSA (sign only)<br \/> Your selection? Press Enter to have default RSA<\/p>\n<\/blockquote>\n<p>RSA keys may be between 1024 and 4096 bits long.<br \/> What keysize do you want? (2048) <span style=\"color: #ff0000;\">Press Enter<\/span><\/p>\n<p>Please specify how long the key should be valid.<br \/> 0 = key does not expire<br \/> = key expires in n days<br \/> w = key expires in n weeks<br \/> m = key expires in n months<br \/> y = key expires in n years<br \/> Key is valid for? (0) Press Enter<br \/> Key does not expire at all<br \/> Is this correct? (y\/N)<span style=\"color: #ff0000;\"> y<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>You need a user ID to identify your key; the software constructs the user ID<br \/> from the Real Name, Comment and Email Address in this form:<br \/> \u201cHeinrich Heine (Der Dichter) \u201d<\/p>\n<p>Real name: <span style=\"color: #ff0000;\">Mohan Ramadoss<\/span><br \/> Email address: <span style=\"color: #ff0000;\">rmohan@rmohan.com<\/span><br \/> Comment: Press Enter<br \/> You selected this USER-ID:<br \/> \u201cMohan Ramadoss\u201d<\/p>\n<p>Change (N)ame, (C)omment, (E)mail or (O)kay\/(Q)uit?<span style=\"color: #ff0000;\"> o<\/span><\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p>You need a Passphrase to protect your secret key.<br \/> <span style=\"color: #ff0000;\">fedorahat@123<\/span><\/p>\n<\/blockquote>\n<p>Use following commands to list your keys. You need to note the key-id to export your key. In below you can see the key-id after<strong> pub2048R <\/strong>which is<strong> ABF9DEAB<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">gpg \u2013list-keys<\/span><br \/> \/home\/rmohan\/.gnupg\/pubring.gpg<br \/> \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/> pub 2048R\/ABF9DEAB 2012-02-16<br \/> uid Mohan Ramadoss<br \/> sub 2048R\/68DA88B7 2012-02-16<br \/> It will create a file named mohan-public.key on current location. Now transfer this file to your partner which you need to have it to decrypt or verify your files.<\/p>\n<p>Where -a is to put output in text rather than binary format. key-id will ensure we are using same key.<\/p>\n<p>scp mohan-public.key test@192.168.10.12:<\/p>\n<p>Now let your partner to import your public key.<\/p>\n<p>gpg \u2013import mohan-public.key<\/p>\n<p><span style=\"color: #ff0000;\">gpg \u2013list-keys<\/span><br \/> \/home\/rmohan\/.gnupg\/pubring.gpg<br \/> \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/> pub 2048R\/ABF9DEAB 2012-02-16<br \/> uid Mohan Ramadoss<br \/> sub 2048R\/68DA88B7 2012-02-16<\/p>\n<p><strong>2). Export Public Key<\/strong><\/p>\n<p>Use following command to export your public key.<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0\u00a0 gpg -a -o mohan-public.key \u2013export ABF9DEAB<\/span><\/p>\n<p>It will create a file named mohan-public.key on current location. Now transfer this file to your partner which you need to have it to decrypt or verify your files.<\/p>\n<p>Where -a is to put output in text rather than binary format. key-id will ensure we are using same key.<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0\u00a0 scp mohan-public.key test@192.168.10.12:<\/span><\/p>\n<p>Now let your partner to import your public key.<\/p>\n<p><span style=\"color: #ff0000;\"> gpg \u2013import mohan-public.key<\/span><\/p>\n<p><strong>3) Encrypt and Decrypt the File.<\/strong><\/p>\n<p>Lets now test it by encrypting one file by test using mohan public key and then decrypt it.<\/p>\n<p>echo \u2018This text is encrpted and can only be view by using sohail public key\u2019 &gt; decrypt.txt<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0 gpg \u2013encrypt -a -r ABF9DEAB decrypt.txt<\/span><\/p>\n<p>It will create encrypted file with appended .asc extension. where -r will require to pub recipient name or key-id to whom this encryption has done.<\/p>\n<p>ls<br \/> <span style=\"color: #ff0000;\">\u00a0\u00a0 decrypt-me.txt.asc<\/span><\/p>\n<p>Now transfer this file to your partner computer.<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0\u00a0 scp test@192.168.10.12:decrypt.txt.asc .<\/span><\/p>\n<p>Now decrypt the file on your computer.<\/p>\n<p>First see what it contain.<\/p>\n<p><span style=\"color: #ff0000;\"> cat decrypt.txt.asc<\/span><\/p>\n<p>\u2014\u2013BEGIN PGP MESSAGE\u2014\u2013<br \/> Version: GnuPG v1.4.11 (GNU\/Linux)<\/p>\n<p>hQEMAy7GnyBo2oi3AQgAg1m\/6bcLj+RZ4IKSr0HitWWyWc3mkIUkZ6KAMJnY2kSx<br \/> JmZ6e0Sc+D\/D9CUy0cmD6PGQcO2LjfQvTKpPvups9Ug3mr9JCqJyjfeDb59uiKN1<br \/> 8cvq2U0\/OVppLb+yf4Z19OryuCdX2MlDdkmhlUaNbftWOA3YlYubi5Db0Fwl+e+X<br \/> nt6SZv51XnQ1wM3fsGN0q5+DAfPsIYtmRkDHvMkkdojkdO8Oxnj4LNu3\/iFhgNTl<\/p>\n<p>\u2014\u2013END PGP MESSAGE\u2014\u2013<\/p>\n<p>Now decrypt and save output on a file named decrypted.txt, note it will require passphrase which you used while creating keys.<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0\u00a0 gpg \u2013decrypt decrypttxt.asc &gt; decrypted.txt<\/span><\/p>\n<p>You need a passphrase to unlock the secret key for<br \/> user: \u201cMohan Ramadoss\u201d<br \/> 2048-bit RSA key, ID 68DA88B7, created 2012-02-16 (main key ID ABF9DEAB)<\/p>\n<p>gpg: encrypted with 2048-bit RSA key, ID 68DA88B7, created 2012-02-16<br \/> \u201cMohan Ramadoss\u201d<\/p>\n<p>cat decrypted.txt<\/p>\n<p>This text is encrypted using mohan public key and can only be decrypt by mohan<\/p>\n<p>For more options you can see man pages of gpg using following command.<\/p>\n<p>man gpg<\/p>\n<p>For any question please comment.<\/p>\n<p>&nbsp;<\/p>\n<p>Quick&#8217;n easy gpg cheatsheet<\/p>\n<p>If you found this page, hopefully it&#8217;s what you were looking for. It&#8217;s just a brief explanation of some of the command line functionality from gnu privacy guard (gpg). Please email me if you find any errors (<a href=\"mailto:scout3801@gmail.com\">scout3801@gmail.com<\/a>\u00a0).<\/p>\n<p>Filenames are italicized (loosely, some aren&#8217;t, sorry), so if you see something italicized, think &#8220;put my filename there.&#8221;<\/p>\n<p>I&#8217;ve used User Name as being the name associated with the key. Sorry that isn&#8217;t very imaginative. I *think* gpg is pretty wide in it&#8217;s user assignments, ie. the name for my private key is Charles Lockhart, but I can reference that by just putting in Lockhart. That doesn&#8217;t make any sense, sorry.<\/p>\n<p><strong>to create a key:<br \/>gpg &#8211;gen-key<br \/><\/strong>generally you can select the defaults.<\/p>\n<p><strong>to export a public key into file public.key:<br \/>gpg &#8211;export -a &#8220;User Name&#8221; &gt;\u00a0<em>public.key<\/em><\/strong><br \/>This will create a file called public.key with the ascii representation of the public key for User Name. This is a variation on:<br \/>gpg &#8211;export<br \/>which by itself is basically going to print out a bunch of crap to your screen. I recommend against doing this.<br \/>gpg &#8211;export -a &#8220;User Name&#8221;<br \/>prints out the public key for User Name to the command line, which is only semi-useful<\/p>\n<p><strong>to export a private key:<br \/>gpg &#8211;export-secret-key -a &#8220;User Name&#8221; &gt;\u00a0<em>private.key<\/em><\/strong><br \/>This will create a file called private.key with the ascii representation of the private key for User Name.<br \/>It&#8217;s pretty much like exporting a public key, but you have to override some default protections. There&#8217;s a note (*) at the bottom explaining why you may want to do this.<\/p>\n<p><strong>to import a public key:<br \/>gpg &#8211;import\u00a0<em>public.key<\/em><\/strong><br \/>This adds the public key in the file &#8220;public.key&#8221; to your public key ring.<\/p>\n<p><strong>to import a private key:<br \/>gpg &#8211;allow-secret-key-import &#8211;import\u00a0<em>private.key<\/em><\/strong><br \/>This adds the private key in the file &#8220;private.key&#8221; to your private key ring. There&#8217;s a note (*) at the bottom explaining why you may want to do this.<\/p>\n<p><strong>to delete a public key (from your public key ring):<br \/>gpg &#8211;delete-key &#8220;User Name&#8221;<br \/><\/strong>This removes the public key from your public key ring.<br \/>NOTE! If there is a private key on your private key ring associated with this public key, you will get an error! You must delete your private key for this key pair from your private key ring first.<\/p>\n<p><strong>to delete an private key (a key on your private key ring):<br \/>gpg &#8211;delete-secret-key &#8220;User Name&#8221;<br \/><\/strong>This deletes the secret key from your secret key ring.<\/p>\n<p><strong>To list the keys in your public key ring:<br \/>gpg &#8211;list-keys<br \/><\/strong><\/p>\n<p><strong>To list the keys in your secret key ring:<br \/>gpg &#8211;list-secret-keys<br \/><\/strong><\/p>\n<p><strong>To generate a short list of numbers that you can use via an alternative method to verify a public key, use:<br \/>gpg &#8211;fingerprint &gt;\u00a0<em>fingerprint<\/em><br \/><\/strong>This creates the file fingerprint with your fingerprint info.<\/p>\n<p><strong>To encrypt data, use:<br \/>gpg -e -u &#8220;Sender User Name&#8221; -r &#8220;Receiver User Name&#8221;\u00a0<em>somefile<\/em><\/strong><br \/>There are some useful options here, such as -u\u00a0to specify the secret key to be used, and -r\u00a0to specify the public key of the recipient.<br \/>As an example: gpg -e -u &#8220;Charles Lockhart&#8221; -r &#8220;A Friend&#8221; mydata.tar<br \/>This should create a file called &#8220;mydata.tar.gpg&#8221; that contains the encrypted data. I think you specify the senders username so that the recipient can verify that the contents are from that person (using the fingerprint?).<br \/>NOTE!: mydata.tar is not removed, you end up with two files, so if you want to have only the encrypted file in existance, you probably have to delete mydata.tar yourself.<br \/>An interesting side note, I encrypted the preemptive kernel patch, a file of 55,247 bytes, and ended up with an encrypted file of 15,276 bytes.<\/p>\n<p><strong>To decrypt data, use:<br \/>gpg -d\u00a0<em>mydata.tar.gpg<\/em><br \/><\/strong>If you have multiple secret keys, it&#8217;ll choose the correct one, or output an error if the correct one doesn&#8217;t exist. You&#8217;ll be prompted to enter your passphrase. Afterwards there will exist the file &#8220;mydata.tar&#8221;, and the encrypted &#8220;original,&#8221; mydata.tar.gpg.<\/p>\n<p>NOTE: when I originally wrote this cheat sheet, that&#8217;s how it worked on my system, however it looks now like &#8220;gpg -d mydata.tar.gpg&#8221; dumps the file contents to standard output. The working alternative (worked on my system, anyway) would be to use &#8220;gpg -o outputfile -d encryptedfile.gpg&#8221;, or using mydata.tar.gpg as an example, I&#8217;d run &#8220;gpg -o mydata.tar -d mydata.tar.gpg&#8221;. Alternatively you could run something like &#8220;gpg -d mydata.tar.gpg &gt; mydata.tar&#8221; and just push the output into a file. Seemed to work either way.<\/p>\n<p>Ok, so what if you&#8217;re a paranoid bastard and want to encrypt some of your own files, so nobody can break into your computer and get them? Simply encrypt them using yourself as the recipient.<\/p>\n<p>I haven&#8217;t used the commands:<br \/><strong>gpg &#8211;edit-key<br \/>gpg &#8211;gen-revoke<\/strong><\/p>\n<ul>\n<li>&#8211;gen-revoke creates a revocation certificate, which when distributed to people and keyservers tells them that your key is no longer valid, see http:\/\/www.gnupg.org\/gph\/en\/manual\/r721.html<\/li>\n<li>&#8211;edit-key allows you do do an assortment of key tasks, see http:\/\/www.gnupg.org\/gph\/en\/manual\/r899.html<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>Sharing Secret Keys<\/h4>\n<p>NOTE!: the following use cases indicate why the secret-key import\/export commands exist, or at least a couple ideas of what you could do with them. HOWEVER, there&#8217;s some logistics required for sharing that secret-key. How do you get it from one computer to another? I guess encrypting it and sending it by email would probably be ok, but I wouldn&#8217;t send it unencrypted with email, that&#8217;d be DANGEROUS.<\/p>\n<p>Use Case *.1 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you&#8217;d want to do this. Basically if you want one key-pair for all of your computers (assuming you have multiple computers), then this allows you export that key-pair from the original computer and import it to your other computers.\u00a0<\/p>\n<p>Use Case *.2 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you&#8217;d want to do this. Basically, if you belonged to a group, and wanted to create a single key-pair for that group, one person would create the key-pair, then export the public and private keys, give them to the other members of the group, and they would all import that key-pair. Then a member of the group or someone outside could use the group public key, encrypt the message and\/or data, and send it to members of the group, and all of them would be able to access the message and\/or data. Basically you could create a simplified system where only one public key was needed to send encrypted stuffs to muliple recipients.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Encrypt Files Using GnuPG <\/p>\n<p>HowTo we will discuss to encypt files using GnuPG. Encryption is a method which protect data stored on your computer or sending over the network from compromise. It can be used to ensure and verify data comes from a rightful owner, and also to maintain confidentiality of the data. We will [&#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\/468"}],"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=468"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":470,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/468\/revisions\/470"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}