# Public Key encryption

**URL:** <https://www.ruby-forum.com/t/public-key-encryption/181611>\
**Category:** Ruby\
**Created:** [January 14, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/public-key-encryption/181611 "2010-01-14T17:40:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 14, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/public-key-encryption/181611/1 "2010-01-14T17:40:46Z")

</div>

Hi all,

I have to talk to a third party system in one of my applications.

The login message has to be encrypted with their public key.  
However, in their documentation they only present me the key and not a  
pem file:  
Does anybody know how I may configure OPENSSL if I only have the  
public key ?

Key from their documentation:  
480152500100000081009CA3D65EAC3318178F31667C3C5E60EE26DD6B090B2671928F8D461B598731AE931A2C8D02B593189DCDF17C591634DE10657AA9957CB6D9952B9040C96536F96A4BFDF0F97703893EF8D52AA91762B85D010AFCE4C3E6CB0338559DECA54D774A6640CB57B8F169B96ED223000FC950254646FA8AD7AA4361D1BCAE9694477D00000003010001

Thanks a lot in advance.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2010, 3:12am UTC](https://www.ruby-forum.com/t/public-key-encryption/181611/2 "2010-01-15T03:12:42Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi all,
> 
> I have to talk to a third party system in one of my applications.
> 
> The login message has to be encrypted with their public key.  
> However, in their documentation they only present me the key and not a  
> pem file:  
> Does anybody know how I may configure OPENSSL if I only have the  
> public key ?
> 
> Key from their documentation:  
> 480152500100000081009CA3D65EAC3318178F31667C3C5E60EE26DD6B090B2671928F8D461B598731AE931A2C8D02B593189DCDF17C591634DE10657AA9957CB6D9952B9040C96536F96A4BFDF0F97703893EF8D52AA91762B85D010AFCE4C3E6CB0338559DECA54D774A6640CB57B8F169B96ED223000FC950254646FA8AD7AA4361D1BCAE9694477D00000003010001
> 
> Thanks a lot in advance.

What does this have to do with Ruby?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2010, 9:20am UTC](https://www.ruby-forum.com/t/public-key-encryption/181611/3 "2010-01-15T09:20:54Z")

</div>

Hi,

Well, I have nt thought about the fact that there might be a special  
group for ruby openssl lib. Sorry for that.

Just to explain my decision to post this to a ruby group…  
If I would have a pem file I would do the following in ruby:  
public\_key = OpenSSL::PKey::RSA.new(File.new(pem\_file))  
encrypted\_string = Base64.encode64(public\_key.public\_encrypt(string))

Maybe somebody can point me to a more appropriate group ?

Thanks a lot in advance.

–  
Volker

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2010, 10:05am UTC](https://www.ruby-forum.com/t/public-key-encryption/181611/4 "2010-01-15T10:05:25Z")

</div>

Hi,

forget about my question. Seems to be a totally proprietary system.

–  
Volker

On 15 Jan., 09:15, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/public-key-encryption/181611/5 "2010-01-15T18:09:42Z")

</div>

If it’s an RSA key you can construct it from its parameters.

require ‘openssl’  
module OpenSSL  
module PKey  
class RSA

```
  # Construct an RSA key from parameters.
  # Input: a large number n
  # an exponent
  # For private keys you also need:
  # factor p of n
  # factor q of n (n = p * q)
  # if d is nil then it is calculated from p and q

  def self.new_from_parameters(n, e, d=nil, p=nil, q=nil)
    a = self.new # self.new(64) for ruby < 1.8.2
    a.n = n # converted to OpenSSL::BN automatically
    a.e = e
    if p and q
      p,q = q,p if p < q
      a.p = p
      a.q = q
      raise "n != p * q" unless a.n == a.p * a.q
      a.d = d || a.e.mod_inverse((a.p-1)*(a.q-1))
      a.dmp1 = a.d % (a.p-1)
      a.dmq1 = a.d % (a.q-1)
      a.iqmp = a.q.mod_inverse(a.p)
    else
      a.d = d
      a.p = nil
      a.q = nil
    end
    a
  end
end

```

end  
end

Then to convert it into a PEM format, all you need is:  
puts OpenSSL::PKey::RSA.new\_from\_parameters(n, e)

The key you give is 1160 bits in total, maybe 1024 bits of product and  
136 bits of exponent? You’ll need to find out its structure.
