Openssl equivalent command in ruby

I need to convert a certificate file (pem format) to pfx using private key. The command works fine on linux:

openssl pkcs12 -export -out certificate1.pfx -inkey myPrivateKey.key -in myCert.pem


Can anyone help me write an equivalent code in ruby ​​using ruby-openssl.


source to share

1 answer

It should be easy :

#!/usr/bin/env ruby
# export-der.rb

require 'openssl'

def export_der(pass, key, cert, out)
  key    =
  cert   =
  name   = nil # not sure whether this is allowed
  pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert), 'w'){|f| f << pkcs12.to_der }

puts 'Password:'
export_der($, *ARGV)


And call it like this (untested; -)):

$ ruby export-der.rb myPrivateKey.key myCert.pem certificate1.pfx




All Articles