openssl - Load PKCS#8 binary key into Ruby -


i'm trying load particular private key encoded in binary der format (pkcs#8) ruby.

however, openssl::pkey won't recognize it. can make work doing console work , transforming pem so:

openssl pkcs8 -inform der -in file.key -passin pass:xxxxxxxx >private_key.pem 

after this, key can correctly read.

however, since whole process done in memory instead of writing , reading files.

so question is: possible load private keys binary encoded der format ruby/openssl?

thank time,

fernando

yes, can indirectly load pkcs#8 der-encoded private keys using ruby openssl.

openssl::pkey::rsa.new handle pem-formatted pkcs#8, easy read binary der , convert pem-formatted string , load string.

for example, these der-encoded private keys:

$ openssl genrsa | openssl pkcs8 -topk8 -outform der \     -nocrypt -out pkcs8.key $ openssl genrsa | openssl pkcs8 -topk8 -outform der \     -v2 des3 -passout pass:secret -out pkcs8_des3.key 

you can this:

require 'openssl' require 'base64'  def box(tag, lines)   lines.unshift "-----begin #{tag}-----"   lines.push "-----end #{tag}-----"   lines.join("\n") end  def der_to_pem(tag, der)   box tag, base64.strict_encode64(der).scan(/.{1,64}/) end  pem = der_to_pem('private key', file.read('pkcs8.key')) key = openssl::pkey::rsa.new(pem)  pem2 = der_to_pem('encrypted private key', file.read('pkcs8_des3.key')) key2 = openssl::pkey::rsa.new(pem2, 'secret') 

read der bytes, base64 them , put pem tags on top , bottom, , load key.


Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -