Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrypt PGP encrypted file with Ruby gpgme

I can't find a single example of how to do this. I have a PGP encrypted XLS file and a PGP key. Here's is my code that returns an empty string:

require 'rubygems'
require 'gpgme'

def passfunc(obj, uid_hint, passphrase_info, prev_was_bad, fd)
  io = IO.for_fd(fd, 'w')
  io.puts "PASSPHRASE"
  io.flush
end

encrypted_data = GPGME::Data.new(File.open("file.xls.pgp"))
key = GPGME::Data.new(File.open("key.txt"))

ctx = GPGME::Ctx.new :passphrase_callback => method(:passfunc)
ctx.import_keys key

decrypted = ctx.decrypt encrypted_data

puts decrypted.read

I'm able to decrypt this file in a program called GNU Privacy Assistant on Windows using the same key. Any help is appreciated.

like image 383
Vincent Avatar asked Apr 10 '12 15:04

Vincent


People also ask

Can GPG decrypt PGP files?

GPG can open and decrypt files encrypted by PGP or Open PGP, meaning it works well with other products. It provides an interface for users to easily encrypt their files.

Can you decrypt PGP without key?

PGP Encryption secures data transmission over the internet and only the person with whom you have shared the keys is able to decrypt the data and use it. This protects your data so that it cannot be misused, even if someone has the file. Without keys, no one can decrypt the file and access the data.

Can PGP encryption be cracked?

People Are Freaking Out That PGP Is 'Broken'—But You Shouldn't Be Using It Anyway. Hackers that can intercept your encrypted emails, or steal your emails from your computer or a server, may be able to decrypt them taking advantage of new vulnerabilities found in the way some email clients treat HTML.


1 Answers

Daiki Ueno, the developer of Ruby gpgme reached out to me on GitHub:

adding decrypted.seek(0) after the line decrypted = ctx.decrypt encrypted_data seems to solve the problem

like image 130
Vincent Avatar answered Sep 25 '22 18:09

Vincent