From: Daniel Frank Date: 2010-01-17T20:22:55+09:00 Subject: GPGME gem - cannot figure out how to decrypt data Hello, I'm pretty new to ruby and this is my first real program I'm writing. Currently I face an issue with the GPGME gem that I cannot figure out how to solve: The GPGME::decrypt method does not seem to work as I expect it, after reading the documentation. In fact the program seems to hang somewhere during the decryption. My guess is that something is wrong with my passphrase callback method, but I cannot figure out how this needs to be done, as the examples for the gem does not contain a decryption example, only a signing example from which I have copied the passphrase callback method, but the method does not seem to work, as the callback method seems to be called with no arguments. I have written a test script to work on this problem, it can be found at the end of the email. When it hangs after the passphrase callback, after waiting about 10 seconds I'm pressing ^C to interrupt the script. The output of the test script is as follows: $ RUBYOPT=rubygems ruby test.rb pwd: xxx encrypting dumping to file decrypting Passphrase callback ^C/var/ruby/1.8/gem_home/gems/gpgme-1.0.8/lib/gpgme.rb:1212:in `decrypt_verify': Interrupt from /var/ruby/1.8/gem_home/gems/gpgme-1.0.8/lib/gpgme.rb:134:in `decrypt' from /var/ruby/1.8/gem_home/gems/gpgme-1.0.8/lib/gpgme.rb:943:in `new' from /var/ruby/1.8/gem_home/gems/gpgme-1.0.8/lib/gpgme.rb:130:in `decrypt' from test.rb:51:in `decrypt' from test.rb:43:in `run_test' from test.rb:33:in `initialize' from /usr/ruby/1.8/lib/ruby/1.8/singleton.rb:94:in `new' from /usr/ruby/1.8/lib/ruby/1.8/singleton.rb:94:in `instance' from test.rb:56 $ gpg --decrypt edata You need a passphrase to unlock the secret key for user: "xxxx xxxxxxxxxxxx " 4096-bit RSA key, ID D2BBECEB, created 2010-01-12 (main key ID ADE8309D) gpg: encrypted with 4096-bit RSA key, ID D2BBECEB, created 2010-01-12 "xxxx xxxxxxxxxxxx " Dummy test data Dummy test data Dummy test data Dummy test data Dummy test data The fact that gpg is able to decrypt the data shows that the encryption of the data is working perfectly fine, I just cannot figure out how to do the decryption in GPGME. A google search for GPGME::decrypt did not help, I could not find anything besides the documentation of the module. Script: #!/usr/bin/env ruby require 'singleton' require 'gpgme' class SMGConfig include Singleton attr_accessor :gpg_key attr_accessor :gpg_password def initialize() @gpg_key = 'ADE8309D' puts "pwd:" @gpg_password = gets end end #def passfunc(hook, uid_hint, passphrase_info, prev_was_bad, fd) # config = SMGConfig.instance # puts "Passphrase callback" # io = IO.for_fd(fd, 'w') # io.puts(config.gpg_password) # io.flush #end def passfunc() SMGConfig.instance.gpg_password end class GPG include Singleton def initialize() @config = SMGConfig.instance run_test() end def run_test() # Run a test if gpg is able to start and encrypt and decrypt data reliably with the known password data = "Dummy test data\n" * 5 puts "encrypting" edata = encrypt(data) puts "dumping to file" File.open('edata', 'w') { | f | f.write(edata) } puts "decrypting" ddata = decrypt(edata) puts "GPG failed to initialize" if !(data.eql?(ddata)) puts "done run_test" end def encrypt(data) edata = GPGME::encrypt(@config.gpg_key, data) end def decrypt(edata) GPGME::decrypt(edata, { :passphrase_callback => passfunc }) end end $stdout.sync = true x = GPG.instance