将 .pem 证书插入 OpenSSL/rails 模型的正确方法?

发布于 2024-10-12 14:12:53 字数 477 浏览 5 评论 0原文

我有一个 .pem 证书,我确信我正确生成了它,但当我将其粘贴到 Rails 3.0.2 模型中时,OpenSSL 不接受它。我所做的是这样的:

open up the .pem file in Textmate
select all and copy
user.cert = <paste into model>; user.save
OpenSSL::PKey::RSA.new(user.cert)

这给了我错误:

Neither PUB key nor PRIV key:: 

为了测试,我只是加载了文件,没有错误:

OpenSSL::PKey::RSA.new(File.read("/path/to/cert.pem"))

我想这可能是一些编码错误或换行符问题,我尝试过将换行符 gsub'ing 为 no有用。

I have a .pem cert that I'm reasonably sure I generated correctly, and it is not being accepted by OpenSSL when I paste it into a Rails 3.0.2 model. What I do is this:

open up the .pem file in Textmate
select all and copy
user.cert = <paste into model>; user.save
OpenSSL::PKey::RSA.new(user.cert)

This gives me the error:

Neither PUB key nor PRIV key:: 

To test, I just loaded in the file instead, no errors:

OpenSSL::PKey::RSA.new(File.read("/path/to/cert.pem"))

I thought maybe it would be some encoding error or newline issue, I had tried gsub'ing out the newlines to no avail.

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

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

发布评论

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

评论(2

朱染 2024-10-19 14:12:53

这确实是一个奇怪的复制粘贴神器。我将其文件读取到模型中而不是复制粘贴,效果很好......

It was a weird copy and paste artifact indeed. I File.read'd it into the model instead of copy paste and it worked fine...

匿名的好友 2024-10-19 14:12:53

也许晚了,但这就是答案:
您可以通过复制/粘贴将公钥内嵌在 Ruby 中,但请记住,对您来说格式化是行中的空格 - 您需要确保生成的 pem 字符串没有空格。我只是从 PEM 文件复制并粘贴到 Ruby 代码中,直到我删除了文本匹配的额外空格或添加到行中的任何内容后,它才起作用。

此处很难显示:

SQS_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIIBIJANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs3VeTxEgLQLL11UN2G6c
oQsc0LbpoEs4VTmu0S4XU82N4h/25XX5k4t5oTJ0JGGSBP4/gzTwz15vS5mrlnsG
缺线
rMV5ZCXToG0VCNPEHpZQnUHMCg/nF9jnk9i1ZZHv2dpYYG7GHMUPG3rtcTWJvZxI
3wIDAQAB
-----END PUBLIC KEY-----".force_encoding("us-ascii")

SQS_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs3VeTxEgLQLL11UN2G6c
  oQsc0LbpoEs4VTmu0S4XU82N4h/25XX5k4t5oTJ0JGGSBP4/gzTwz15vS5mrlnsG
  MISSINGLINES
  rMV5ZCXToG0VCNPEHpZQnUHMCg/nF9jnk9i1ZZHv2dpYYG7GHMUPG3rtcTWJvZxI
  3wIDAQAB
  -----END PUBLIC KEY-----".force_encoding("us-ascii")

即 - 不是第二个 - ruby​​ 在每一行的开头添加空格,并且 RSA 工具不会忽略空格 - 它们 我似乎只忽略换行符。

我使用复制/粘贴的密钥作为后备 - 换句话说,如果设置了 ENV,我就使用它,否则使用粘贴的公钥

--Tom 。

Perhaps late, but this is the answer:
You can put a public key inline in Ruby with copy/paste, but keep in mind that what looks like formatting to you is white space in the line - you need to make sure that the resulting pem string has no spaces. I just copied and pasted from a PEM file into Ruby code, and it did not work until I removed the extra spaces that text mate or whatever added to the lines.

Hard to show here:

SQS_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs3VeTxEgLQLL11UN2G6c
oQsc0LbpoEs4VTmu0S4XU82N4h/25XX5k4t5oTJ0JGGSBP4/gzTwz15vS5mrlnsG
MISSINGLINES
rMV5ZCXToG0VCNPEHpZQnUHMCg/nF9jnk9i1ZZHv2dpYYG7GHMUPG3rtcTWJvZxI
3wIDAQAB
-----END PUBLIC KEY-----".force_encoding("us-ascii")

SQS_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs3VeTxEgLQLL11UN2G6c
  oQsc0LbpoEs4VTmu0S4XU82N4h/25XX5k4t5oTJ0JGGSBP4/gzTwz15vS5mrlnsG
  MISSINGLINES
  rMV5ZCXToG0VCNPEHpZQnUHMCg/nF9jnk9i1ZZHv2dpYYG7GHMUPG3rtcTWJvZxI
  3wIDAQAB
  -----END PUBLIC KEY-----".force_encoding("us-ascii")

ie - NOT the second one - ruby adds spaces to the start of each line, and the RSA tools do not ignore spaces - they only seem to ignore line feeds.

I use the copy/pasted key as a fallback - in other words if an ENV is set I use that, otherwise use the pasted in public key.

--Tom

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