ueno / ruby-gpgme

a ruby interface to GnuPG Made Easy (GPGME).
GNU Lesser General Public License v2.1
232 stars 99 forks source link

GPGME::Error::DecryptFailed #108

Closed Dimcha closed 6 years ago

Dimcha commented 6 years ago

Having trouble making decryption work by passing the passphrase but no matter what I do it does not work, and cannot find any explanation why, maybe someone here could help me.

text = "My PGP message"
@gpg = GPGME::Crypto.new
GPGME::Key.import(File.open(File.join(Rails.root, 'config', 'pgp_keys.asc')))
options = { :password => Rails.application.secrets.gpg_password, :pinentry_mode => GPGME::PINENTRY_MODE_LOOPBACK }
@gpg.decrypt(text, options)

And here I get GPGME::Error::DecryptFailed. To be more specific I get:

gpg-error 117440664 117440664 = (7, 152) = (GPG_ERR_SOURCE_GPGME, GPG_ERR_DECRYPT_FAILED) = (GPGME, Decryption failed)

I tried

echo allow-loopback-pinentry >> ~/.gnupg/gpg-agent.conf

but no help as well.

More info:

GPGME::Engine.info.first => #<GPGME::EngineInfo:0x00000009213758 @protocol=0, @file_name="/usr/bin/gpg2", @version="2.1.5", @req_version="1.4.0">

If I do it without pinentry_mode it prompts a field to enter a passphrase, and everything works good, but I need to do it to work by itself. Any ideas?

oogali commented 6 years ago

What was your resolution here?

Dimcha commented 6 years ago

Here is the solution I did.

require 'gpgme'

module GPG
  GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, "/usr/bin/gpg", nil)
  @gpg = GPGME::Crypto.new

  class << self
    def decrypt(text, options = {})
      GPGME::Key.import(File.open(File.join(Rails.root, 'config', 'pgp_keys.asc')))
      options = { password: Rails.application.secrets.gpg_password }.merge(options)

      @gpg.decrypt(text, options)
    end
  end
end

result = GPG.decrypt(text).read