# Public key encrypt

**URL:** <https://www.ruby-forum.com/t/public-key-encrypt/110868>\
**Category:** Ruby\
**Created:** [July 31, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/public-key-encrypt/110868 "2007-07-31T17:27:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [July 31, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/public-key-encrypt/110868/1 "2007-07-31T17:27:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [August 2, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/public-key-encrypt/110868/2 "2007-08-02T18:47:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [August 3, 2007, 6:20am UTC](https://www.ruby-forum.com/t/public-key-encrypt/110868/3 "2007-08-03T06:20:57Z")

</div>

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
