Gpg decryption without pin entry pop up using GPGME

Sushant Mittal

By Sushant Mittal

on March 27, 2018

In one of our projects, we implemented GPG decryption.

What is GPG ?

GPG is a complete and free implementation of the OpenPGP standard as defined by RFC4880 (also known as PGP).

We used GPGME gem for this purpose. It provides three levels of API. In our case, we used Crypto which has the high level convenience methods to encrypt, decrypt, sign and verify signatures.

We needed to import private key for decrypting a file that was encrypted using paired public key. First let's import the required private key.

1GPGME::Key.import File.open('certs/pgp.key')

Let's decrypt the file.

1crypto = GPGME::Crypto.new
2options = { output: File.open('file.csv', 'wb') }
3
4crypto.decrypt File.open('file.csv.gpg'), options

Above code has one problem. It will open a pop up for password input that has been used when public and private keys have been generated.

To support password input without pop up, we updated the code as below.

1crypto = GPGME::Crypto.new
2options = {
3            output: File.open('file.csv', 'wb'),
4            pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK,
5            password: 'welcome'
6          }
7
8crypto.decrypt File.open('file.csv.gpg'), options

Here, pinentry_mode option allows password input without pop up.

We did not use latest version of GPG since it does not support pinentry_mode option. Instead, We used 2.1.20 version which has support for this option. Here is the build instruction for that.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.