开发者

Ruby equivalent for php function openssl_pkey_get_public

I have the php script where the password encoding done using the openssl:

$key = openssl_get_publickey($certificate);

openssl_public_encrypt($pass,$userPassCrypted,$key,OPENSSL_PKCS1_PADDING);

openssl_free_key($key);

Now I trying to make the same with ruby

require 'openssl' cert = OpenSSL::X509::Certificate.new(certificate)

public_key = cert.public_key

passwordSSL = public_key.public_encrypt(password, 1)

The problem here is that these password encoding isn't match

The certificate the same but the public key in PHP is:

-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----

in ruby is:

-----BEGIN RSA PUBLIC KEY----- ... -----END RSA PUBLIC KEY-----

I guess here is the difference (keys body also not the same)...

How c开发者_运维技巧an I get inside ruby the same public key as in PHP? Or is it possible convert RSA key by somehow?

Or maybe I have to do it with another way?

Any suggestions highly appreciated.


You can open this by using the following:

# Public key
public_key = OpenSSL::PKey::RSA.new(File.read("/path/to/public/key"))

# Private key (with password)
private_key = OpenSSL::PKey::RSA.new(File.read("/path/to/private/key), "password")

Hope this helps!

UPDATE: Ah, okay. Yeah, I don't think there is a way to remove the " RSA " from the OpenSSL output without removing it with a regular expression.

cert = OpenSSL::X509::Certificate.new(File.read("/path/to/x509/cert"))

cert.public_key.to_s.gsub(/RSA PUBLIC KEY-----/, "PUBLIC KEY-----")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜