首页 文章

ruby中的openssl等效命令

提问于
浏览
1

我必须使用私钥将证书文件(pem格式)转换为pfx . 该命令在linux中完美有效

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

任何人都可以帮我用ruby-openssl在ruby中编写等效的代码 .

1 回答

  • 4

    应该easy

    #!/usr/bin/env ruby
    # export-der.rb
    
    require 'openssl'
    
    def export_der(pass, key, cert, out)
      key    = OpenSSL::PKey.read File.read(key)
      cert   = OpenSSL::X509::Certificate.new File.read(cert)
      name   = nil # not sure whether this is allowed
      pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert)
      File.open(out, 'w'){|f| f << pkcs12.to_der }
    end
    
    puts 'Password:'
    export_der($stdin.read, *ARGV)
    

    并且这样称呼(未经测试;-)):

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

相关问题