从 Ruby 向 Android 发送公钥
服务器
OpenSSL::PKey::RSA.generate(1024).public_key.to_s
会返回
"-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAPHpKZe0jKkyyslkhnoQzRovzQB+6OLtACNkILk+6UKgYJ1UM7Qkpxab\noEHnpR/XHrIfFUB8dfhaIeqHGC3IASZh6vveH6ypwZTqDHrejqYcfOuKOJcCRDJf\n/qxeVy1jwt7oMbrDhCeVNd3eIYdq0joEnZ6k4KwqvG1ZIOKkE8adAgMBAAE=\n-----END RSA PUBLIC KEY-----\n"
当我将此字符串复制并粘贴为 java 字符串时, 该字符串。但是,我需要通过 httpResponse 获取此公钥。当被 HttpRequest 调用时,它返回:
-----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCySmMxjxX1Xw80yRB35BQHP27V
EhIUG9/bxsyJMs4rhmvnpP7saeBznyDMQ3I5wt7cJEPABy+QuuAGjphj6/FsfsXP
9zLRroX02f48fQXNM7j8RtQ5y8bhcZrnb8/MNoAKnbAqkKlkuW/gRxSB0qeG5Q17
vvSJ6KHb5heAFwaGtwIDAQAB
-----END RSA PUBLIC KEY-----
但这是无效的。什么给?
On the server
OpenSSL::PKey::RSA.generate(1024).public_key.to_s
returns
"-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAPHpKZe0jKkyyslkhnoQzRovzQB+6OLtACNkILk+6UKgYJ1UM7Qkpxab\noEHnpR/XHrIfFUB8dfhaIeqHGC3IASZh6vveH6ypwZTqDHrejqYcfOuKOJcCRDJf\n/qxeVy1jwt7oMbrDhCeVNd3eIYdq0joEnZ6k4KwqvG1ZIOKkE8adAgMBAAE=\n-----END RSA PUBLIC KEY-----\n"
which works when I copy and paste this string as a java String. However, I need to get this public key through a httpResponse. When called by an HttpRequest, it returns:
-----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCySmMxjxX1Xw80yRB35BQHP27V
EhIUG9/bxsyJMs4rhmvnpP7saeBznyDMQ3I5wt7cJEPABy+QuuAGjphj6/FsfsXP
9zLRroX02f48fQXNM7j8RtQ5y8bhcZrnb8/MNoAKnbAqkKlkuW/gRxSB0qeG5Q17
vvSJ6KHb5heAFwaGtwIDAQAB
-----END RSA PUBLIC KEY-----
But this is invalid. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实是一个不错的:) 好吧,让我们从头开始。
WTF?!然后我重新编码了能够解码为 PEM 的 RSA 公钥 - 它给了我:
看到区别 - 这是 PEM 标头。 Base64 编码的结构是公钥的通用 X.509 表示形式(因此标头也有所不同),而不是 PKCS#1 中描述的 RSA 特定表示形式,您的标头将暗示这一点!如果您更改 PEM 标头,那么它就会起作用!
This is really a nice one :) OK, let's start from the beginning.
WTF?! Then I reencoded the RSA public key I was able to decode to PEM - and it gave me:
See the difference - it's the PEM headers. The structure that is Base64-encoded is the generic X.509 representation of a public key (therefore also the difference in the headers), and not the RSA-specific representation described in PKCS#1, which your headers would imply! If you change the PEM headers, then it'll work!