Forum: Ruby public key encrypt

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sonn Y. (Guest)
on 2007-07-31 19:27
Hello all,

I know how to use public key to encrypt data if I create public/private
key pair by myself through OpenSSL::PKey::RSA. However, if I only know
other guy's public key, how could I encrypt data with his/her public
key? Is there any method in the module OpenSSL::PKey::RSA to support it?
Thanks!
Sonn Y. (Guest)
on 2007-08-02 20:47
Sonn Y. wrote:
> Hello all,
>
> I know how to use public key to encrypt data if I create public/private
> key pair by myself through OpenSSL::PKey::RSA. However, if I only know
> other guy's public key, how could I encrypt data with his/her public
> key? Is there any method in the module OpenSSL::PKey::RSA to support it?
> Thanks!

Does any one can help me about this question? thanks.
Chris Lee (Guest)
on 2007-08-03 08:20
(Received via mailing list)
Sonn Y. wrote:
> Sonn Y. wrote:
>> Hello all,
>>
>> I know how to use public key to encrypt data if I create public/private
>> key pair by myself through OpenSSL::PKey::RSA. However, if I only know
>> other guy's public key, how could I encrypt data with his/her public
>> key? Is there any method in the module OpenSSL::PKey::RSA to support it?
>> Thanks!
>
> Does any one can help me about this question? thanks.

If you're trying to do GPG, there's a module that might help.  If not,
I'd have
to research this closely as the documentation for OpenSSL is lacking.

require 'gpgme'

  def get_pgp_keys emails
    # this function searches your GPG keyring for valid public keys for
    # each of the email addresses listed in emails
    # if a key is not found for an email, it will be omitted from the
output list
    ctx = GPGME::Ctx.new
    keys = []
    emails.each do |email|
      begin
        ctx.keylist_start email
        key = ctx.keylist_next
        ctx.keylist_end
        keys << key
      rescue EOFError
        # the key for this email was not found and keylist_next threw
this error
        # this is expected behavior when there was no key found for the
email
address
      end
    end
    keys
  end

      # check list with gpg
      keys = get_pgp_keys to_emails

      plaintext = @body.join ""
      # if all match, encrypt @body
      if keys.size == to_emails.size
        keys << Redwood.get_personal_public_key
        ciphertext = GPGME.encrypt keys, plaintext, {:armor => true,
:sign =>
true, :passphrase_callback => getpass }
        @body = ciphertext.split "\n"
        @body.map { |l| l+"\n" }
      else
        GPGME.sign plaintext, nil, { :mode => GPGME::SIG_MODE_CLEAR }
      end
    end
This topic is locked and can not be replied to.