{"id":773,"date":"2021-01-16T18:51:50","date_gmt":"2021-01-16T18:51:50","guid":{"rendered":"https:\/\/dft.wiki\/?p=773"},"modified":"2022-03-21T21:27:53","modified_gmt":"2022-03-21T21:27:53","slug":"encrypting-files-with-gnupg-openpgp-in-linux-cli","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=773","title":{"rendered":"Encrypting and Decrypting Files with GnuPG (OpenPGP) in Linux CLI"},"content":{"rendered":"<p><strong>Creating<\/strong> a key-pair:<\/p>\n<pre>gpg<strong> --gen-key<\/strong>\r\n\r\nOR\r\n\r\ngpg <strong>--full-generate-key<\/strong><\/pre>\n<p><strong>Backing up<\/strong> the secret\/private key:<\/p>\n<pre>gpg <strong>--list-secret-keys<\/strong>\r\ngpg <strong>--export-secret-keys<\/strong> -a 'youremail@example.com' &gt; priv.asc<\/pre>\n<p><strong>List the keys<\/strong> present in the system, <strong>export<\/strong> from the machine that contains the key, and <strong>import<\/strong> in the machine that will use the key to encrypt the files:<\/p>\n<pre>gpg --<strong>list-keys<\/strong>\r\ngpg --<strong>export<\/strong> -a 'youremail@example.com' &gt; pub.asc\r\ngpg --<strong>import<\/strong> pub.asc<\/pre>\n<p><strong>NOTE:<\/strong> the argument <code>-a<\/code> defines the output type to <code>asc<\/code>, if not provided the key will have a binary format.<\/p>\n<p>Replace the email with the correct recipient email associated with the key.<\/p>\n<p><strong>List the keys<\/strong> to confirm it was imported correctly and <strong>edit the key<\/strong>:<\/p>\n<pre>gpg --<strong>list-keys<\/strong>\r\ngpg --<strong>edit-key<\/strong> <em>9DC8C540E9FF1344<\/em><\/pre>\n<p>Replace the &#8216;<em>9DC8C540E9FF1344<\/em>&#8216; with the identification you find.<\/p>\n<p>It is important to trust the key so if you use a script to automate encrypt backup files, for example, it will not prompt confirmation question:<\/p>\n<pre>gpg&gt; <strong>trust<\/strong>\r\ngpg&gt; <strong>quit<\/strong><\/pre>\n<p>Answer <strong>5<\/strong> and confirm.<\/p>\n<p>Then you are able to encrypt files using the imported and trusted key:<\/p>\n<pre>gpg -a -r \"youremail@example.com\" --yes --encrypt FILE.TXT\r\n\r\nOR\r\n\r\ncat FILE.TXT | gpg -r \"youremail@example.com\" --yes --encrypt - &gt; FILE.TXT.GPG<\/pre>\n<p>Later the file can be decrypted using the following command:<\/p>\n<pre>gpg --decrypt FILE.TXT.asc &gt; FILE.TXT<\/pre>\n<p>See also this tool for encrypting and decrypting files using GnuPG using Nautilus File Browser:<\/p>\n<pre>sudo apt install seahorse-nautilus<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating a key-pair: gpg &#8211;gen-key OR gpg &#8211;full-generate-key Backing up the secret\/private key: gpg &#8211;list-secret-keys [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=773"}],"version-history":[{"count":9,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":2845,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions\/2845"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}