2017-02-22 34 views
0

我有一個私鑰的pem證書。 我使用上述信息產生被密碼保護如下P12證書:紅寶石編碼一個p12證書(二進制)併發送爲json響應

def p12_cert 
    ca_cert = x509_cert(File.open("#{root}/ca-cert.crt").read) 
    p12 = OpenSSL::PKCS12.create(@random_pass, 'My Certificate', 
      rsa_pkey(private_key), x509_cert(public_cert), [ca_cert]) 
    create_file('p12', p12.to_der, ':ASCII-8BIT') 
    end 

的問題是,這是二進制格式,無法通過JSON API傳輸。

有人可以如何我如何編碼它(也許base64),以便這可以作爲JSON響應發送?

編輯:我打開閱讀P12文件,然後試圖使用Base64編碼,得到如下:

irb(main):017:0> enc_p12 = Base64.encode64(p12) TypeError: no implicit conversion of OpenSSL::PKCS12 into String

+0

有些功能並不在https://github.com/Mitka20/tango_api/blob/2cc0127b8fc601526ddd000ff1ac46d6531592c0/tango_api.rb代碼幫助嗎? – sheppe

+0

@sheppe。我不確定。在你的例子中,你讀了p12文件,但我不知道如何通過JSON(非二進制格式)發送它 – Micheal

+0

密鑰附加到請求中的一行:https.key = p12.key 另外,這不是我的代碼,但似乎相關。 :) – sheppe

回答

1

你通常不編碼PKCS12對象本身,而是原始文件。像 Base64.encode64(File.read(filename, mode: 'rb')