Public key encrypt


#1

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!


#2

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.


#3

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