ruby-gpgme icon indicating copy to clipboard operation
ruby-gpgme copied to clipboard

How do I encrypt data with a specific Key?

Open sumdog opened this issue 7 years ago • 1 comments

I've been looking through the documentation and I'm a big confused by this. The decrypt function is pretty straight forward and launches my pgp agent to prompt for a password and automatically chose the correct decryption key.

I'm confused about the reverse. How do I sign data with a specific key? I can run GPGME::Key.find(:secret, '4F480261701CB81FB00334017322917262CCC1E5') to find the key in my local store that I want to sign my data with. The documentation around decrypt just says you need to run Keys.import first. I tried the following:

key = GPGME::Key.find(:secret, '4F480261701CB81FB00334017322917262CCC1E5').first
GPGME::Key.import(key)
#<GPGME::ImportResult:0x00000000b69360 @considered=1, @no_user_id=0, @imported=0, @imported_rsa=0, @unchanged=1, @new_user_ids=0, @new_sub_keys=0, @new_signatures=0, @new_revocations=0, @secret_read=0, @secret_imported=0, @secret_unchanged=0, @not_imported=0, @imports=[#<GPGME::ImportStatus:0x00000000b69310 @fpr="4F480261701CB81FB00334017322917262CCC1E5", @result=0, @status=0>]>

But it doesn't appear to do anything. If I run encrypt using the e-mail the key is for, I get the following:

crypt.encrypt "something", :signer=>"[email protected]"
GPGME::Error::UnusablePublicKey: GPGME::Error::UnusablePublicKey

Also, it doesn't appear that I can just use the short identifier for the key in Key.find, I have to use the whole thing.

sumdog avatar Jan 14 '18 07:01 sumdog

I was getting the same error. I think it was caused by not trusting the key when it was imported (https://www.gnupg.org/gph/en/manual/x56.html). Passing the :always_trust option allows you to circumvent it.

crypto.encrypt("Hello",:recipients => "[email protected]",:always_trust => true)

adam-haku avatar May 30 '18 21:05 adam-haku