Ruby 相当于 php 函数 openssl_pkey_get_public

发布于 2024-08-20 22:32:19 字数 728 浏览 2 评论 0原文

我有 php 脚本,其中密码编码使用 openssl:

$key = openssl_get_publickey($certificate);
openssl_public_encrypt($pass,$userPassCrypted,$key,OPENSSL_PKCS1_PADDING);
openssl_free_key($key);

做同样的事情

现在我尝试用 ruby​​ require 'openssl' cert = OpenSSL::X509::Certificate.new(证书)
公钥 = cert.public_key
passwordSSL = public_key.public_encrypt(password, 1)

这里的问题是这些密码编码不匹配

证书相同但公钥 在 PHP 中是:

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

在 ruby​​ 中是:

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

我想这就是区别(密钥体也不一样)...

我怎样才能在 ruby​​ 中获得与 PHP 中相同的公钥?或者是 可以通过某种方式转换 RSA 密钥吗?

或者也许我必须用另一种方式来做?

任何建议都将受到高度赞赏。

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 can 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-08-27 22:32:19

您可以使用以下命令打开它:

# 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")

希望这有帮助!

更新:啊,好的。是的,我认为没有办法从 OpenSSL 输出中删除“RSA”,而不用正则表达式删除它。

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

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

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-----")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文