Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalid public keys when using the Ruby OpenSSL library
    primarykey
    data
    text
    <p>I'm trying to generate RSA keypairs in Ruby, mostly using the examples from <a href="http://stuff-things.net/2009/12/11/generating-rsa-key-pairs-in-ruby/" rel="nofollow">this blog post</a>. Here is my slightly modified code:</p> <pre><code>def generate_keypair(passphrase) rsa_key = OpenSSL::PKey::RSA.new(2048) cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') private_key = rsa_key.to_pem(cipher, passphrase) public_key = rsa_key.public_key.to_pem return private_key, public_key end </code></pre> <p>This successfully generates a private key and a public key, and I can write those out to files on the filesystem.</p> <pre><code>irb(main):002:0&gt; private_key1, public_key1 = generate_keypair('test') [...output of keys...] irb(main):003:0&gt; File.open("key.pem","w") {|f| f.write(private_key1) } =&gt; 1766 irb(main):004:0&gt; File.open("pubkey.pem","w") {|f| f.write(public_key1) } =&gt; 426 </code></pre> <p>However, OpenSSL complains when I try to use this public key:</p> <pre><code>$ openssl rsautl -encrypt -inkey pubkey.pem -pubin -in text.txt -out text.ssl unable to load Public Key </code></pre> <p>If I use the <code>openssl</code> tool to extract the public key from the private key then everything works:</p> <pre><code>$ openssl rsa -in key.pem -pubout -out pubkey2.pem Enter pass phrase for key.pem: writing RSA key $ openssl rsautl -encrypt -inkey pubkey2.pem -pubin -in text.txt -out text.ssl $ openssl rsautl -decrypt -inkey key.pem -in text.ssl Enter pass phrase for key.pem: this is a file that needs to be encrypted </code></pre> <p>The public key that the Ruby OpenSSL library produced is different from the public key that the <code>openssl</code> cli tool extracted from the private key:</p> <pre><code>$ cat pubkey.pem -----BEGIN RSA PUBLIC KEY----- MIIBCgKCAQEAzgNcsEL7yGBoLBYBXFYrDL6oLP8ZbW9+VwdoXyNG6Qt/NEhEx4Ww 5yOxtXAbqeUwyvbTUxRrJ02dQcb4FGcSMDgz2QHIZyCuDJkgC9Wj7KI1Q7g0GV+7 DcZvLcwPZOhLXqUzlcZXjDWM1PZ+az734qEribgyI+87LB8TujG8v5iOvdzT/Je4 JAllToZVGC3RddfTc6ww37gB39B++FYNzPg+nrIEU45KgEWPo2eJxBpX29lACh6q EEBCQr9xyLxOC2eomYIl3dG2dV7nGGH7Pur2HjppgJphBvNkwxIWUa/pD6hAnOQ4 MkDDFGwWv7eJLb4UZuZjafTbqokHved3bwIDAQAB -----END RSA PUBLIC KEY----- $ cat pubkey2.pem -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzgNcsEL7yGBoLBYBXFYr DL6oLP8ZbW9+VwdoXyNG6Qt/NEhEx4Ww5yOxtXAbqeUwyvbTUxRrJ02dQcb4FGcS MDgz2QHIZyCuDJkgC9Wj7KI1Q7g0GV+7DcZvLcwPZOhLXqUzlcZXjDWM1PZ+az73 4qEribgyI+87LB8TujG8v5iOvdzT/Je4JAllToZVGC3RddfTc6ww37gB39B++FYN zPg+nrIEU45KgEWPo2eJxBpX29lACh6qEEBCQr9xyLxOC2eomYIl3dG2dV7nGGH7 Pur2HjppgJphBvNkwxIWUa/pD6hAnOQ4MkDDFGwWv7eJLb4UZuZjafTbqokHved3 bwIDAQAB -----END PUBLIC KEY----- </code></pre> <p>I'm not quite sure what is going on here, but it seems as if the Ruby OpenSSL library is producing an invalid public key pem file. Am I doing something wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload