5

Secrecy and (file) Corruption – diving into the murky world of GPG encryption |...

 3 years ago
source link: https://mikesmithers.wordpress.com/2020/04/18/secrecy-and-file-corruption-diving-into-the-murky-world-of-gpg-encryption/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Secrecy and (file) Corruption – diving into the murky world of GPG encryption

Posted on April 18, 2020

Transferring sensitive data between systems often requires some for of encryption to ensure that the data is protected from prying eyes.
One common method of achieving this is to use Gnu Privacy Guard (GPG).
What I’m going to look at here is :

  • Creating GPG keys on a server
  • Using a Public Key to encrypt data on a different machine
  • Decrypting an encrypted message
  • Things to try if you get some of the more wacky GPG error messages

If you’ve stumbled on this post because of the last of these, you’re in good company. I’m sure someone initimately familiar with this tool will instantly know the meaning of the error message “Ohhhh jeeee: mpi larger than packet” but as far I’m concerned, it may as well have read “Divide by Cucumber Error”.
Hopefully, things will become clearer down the page…

Creating a GPG key

In fact, we’ll be creating two keys. The Public Key is, as it’s name suggests, generally available and anyone wanting to send us a message need only use that key to encrypt it.
The Private key is the one that only we have access to and which we can use to decrypt files encrypted with the Public Key.

To create the keys, we need to be on the server that will receive these files.
Once there, we simply run :

gpg --gen-key

which results in :

NOTE – according to the manual, the default settings are sufficient in most cases so accept them unless you have a specific reason to change them.
For the length of time the key is valid, enter “0” if you don’t want it to expire.
In this case, I’ve selected 7 days.
GPG should now prompt you for a passphrase.
NOTE – it’s really important that you remember the passphrase as otherwise you will not be able to decrypt any messages :

Finally, the keys will be generated :

Distributing the Public Key

Before we go any further, we probably want to obtain a text version of our public key that we can then distribute.
This can be accomplished by running :

gpg --armour --export [email protected] >ponder_stibbons_public_key.txt

We now have a file that contains our Public Key. It looks like this :

I can now distribute this to anyone who wants to send me an encrypted message.

Using a Public Key to encrypt data on a different machine

I’m now on a different machine onto which I’ve downloaded the public key.
I now need to import it :

gpg --import ./ponder_stibbons_pubkey.txt

…which returns…

gpg: key 1DA0A003: public key "Ponder Stibbons (Test generation of a GPG key) <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

I can get a list of available keys by running :

gpg --list-keys

…which confirms that the key is now present in my keyring :

/home/mike/.gnupg/pubring.gpg
-----------------------------
pub   2048R/1DA0A003 2020-04-09 [expires: 2020-04-16]
uid                  Ponder Stibbons (Test generation of a GPG key) <[email protected]>
sub   2048R/11DC569B 2020-04-09 [expires: 2020-04-16]

We have some data in a file called secret_message.txt that we want to send.
With the recipients public key imported, we can encrypt the file like this :

gpg --encrypt -r [email protected] secret_message.txt

At this point GPG will prompt you to confirm that you want to do this :

gpg: 11DC569B: There is no assurance this key belongs to the named user
pub  2048R/11DC569B 2020-04-09 Ponder Stibbons (Test generation of a GPG key) <[email protected]>
Primary key fingerprint: 8238 E5BC C165 EA3D A927  39A3 BFFB 64EC 1DA0 A003
Subkey fingerprint: 1BE6 DCF6 F041 988F D011  A19E F66F FD9F 11DC 569B
It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N) y

We now have an encrypted copy of the file…

ls -l secret_message*
-rw-rw-r-- 1 mike mike  36 Apr  9 21:23 secret_message.txt
-rw-rw-r-- 1 mike mike 386 Apr  9 21:26 secret_message.txt.gpg

…which is encrypted :

file secret_message.txt.gpg
secret_message.txt.gpg: PGP RSA encrypted session key - keyid: 9FFD6FF6 9B56DC11 RSA (Encrypt or Sign) 2048b .

Indeed, the .gpg version of the file contains this :

We can now transfer the encrypted file to the target server.

Decrypting the file

Once the file has reached it’s intended destination it can now be decrypted by the intended recipient using their private key.
Note that in order to do this, they need to know the passphrase used to create the key in the first place :

gpg --batch --yes --passphrase ************ -o secret_message.txt --logger-file secret.log secret_message.txt.gpg

…where ************ is the passphrase.

Once we run this we can see that we have generated a log file, together with the output file :

ls -l secret*
-rw-rw-r-- 1 mike mike 169 Apr  9 21:31 secret.log
-rw-rw-r-- 1 mike mike  36 Apr  9 21:31 secret_message.txt
-rw-rw-r-- 1 mike mike 386 Apr  9 21:29 secret_message.txt.gpg

If all has gone according to plan, the logfile should look something like :

2020-04-09 21:31:11 gpg[6431] encrypted with 2048-bit RSA key, ID 11DC569B, created 2020-04-09
"Ponder Stibbons (Test generation of a GPG key) <[email protected]>"

Also, we should now be able to read the decrypted contents of the file :

cat secret_message.txt
Out of Cheese Error
Redo from start

When things go horribly wrong

In using GPG, I’ve found that error messages regarding packet sizes can be quite common when you first begin receiving file from a new source system. Perhaps the most cryptic of these is :

Ohhhh jeeee: mpi larger than packet

When this sort of thing crops up it appears to be the result of a file corruption. Either the key file used to encrypt the data, or the encrypted file itself, has somehow become corrupted.
There are at least two common ways for this to happen.

Transferring a text key from Windows to Linux

Remember that, in the example above, we used a text file to distribute our public key.
As you probably know, Windows uses two non-printing characters to terminate a line – Carriage Return (to move the cursor to the start of the line) and Line Feed (to move down to the next line). This means that each new line is preceeded by the ASCII characters 13 and 10.
Linux uses a single new-line character – ASCII 10 – in which the Carriage Return is implicit.
Unfortunately conversion between the two new-lines is not automatic. Therefore, if you find yourself transferring the text copy of a public key from a Windows system to a Linux system then you need to convert the new-lines to Linux using the dos2unix utility before using the key for GPG encryption.
In our example, we’d need to run :

dos2unix ponder_stibbons_pubkey.txt

Note : on the off-chance it’s not already installed, you can install this utility as follows :

On a RedHat-based system (RHEL, CentOS, Fedora etc) :

sudo yum install dos2unix

On a Debian-based system ( Debian, Ubuntu, Mint etc) :

sudo apt-get install dos2unix

One useful aspect of this utility is that it will not alter the file unless it finds new-lines that require changing.

Selecting the file transfer mode over sftp

Unless you are an aged person such as myself, who remembers the time when you’d ftp files on the command line, you probably won’t be aware that ftp ( the forerunner to sftp), had different modes for text files (ascii) and binary files (binary). However, many modern sftp client tools do remember this distinction. Take FileZilla, for example :

As we have seen, GPG encrypted files are binary. If they are transferred in ASCII mode then they will become corrupted and trigger GPG’s packet-size obsession when you try to decrypt them.

If you’re hitting this issue then make sure that your sftp client isn’t being “helpful” by automatically selecting the incorrect transfer mode for your encrypted file.

Using FileZilla as an example, you can specify the correct mode when you invoke a Manual Transfer from the Transfer menu :

Hopefully you should now be free of file corruption and be able to proceed without having to re-initialize your FTB.

Advertisements
Report this ad

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK