将PEM编码的RSA公钥转换为AsymmetricKeyParameter [英] convert PEM encoded RSA public key to AsymmetricKeyParameter
问题描述
我正在尝试创建一种方法,该方法从PEM编码的 public 密钥构造一个 AsymmetricKeyParameter
。不幸的是, pemReader.ReadObject()
返回null。
这是私钥的可行解决方案:将PEM编码的RSA私钥转换为AsymmetricKeyParameter
此方法有什么问题?
静态AsymmetricKeyParameter ReadPublicKeyFromPemEncodedString(string pemEncodedKey)
{
AsymmetricKeyParameter result = null;
使用(var stringReader = new StringReader(pemEncodedKey))
{
var pemReader = new PemReader(stringReader);
var pemObject = pemReader.ReadObject(); // 空值!
结果=(((AsymmetricCipherKeyPair)pemObject).Public;
}
返回结果;
}
这里是我正在测试的PEM编码的公钥。我试过不加评论,也删除了SSH2。
---- BEGIN SSH2公钥----
点评: RSA的密钥-20170608
AAAAB3NzaC1yc2EAAAABJQAAAQEAk0AmagKx285Ufbri / OLC + f3WagL1Ho + DrYdD
SbuU7cJAq + uD9xGvvP9m2JavSP4wO9i9pB / cmCFMPoIj3oGJt1 / cnLb / U2juneOw
6Uo0N3F8TXdyXfZNAIPhq / jw0YfIypTFTTvFkKXfTArIwW / bQBW8 / dujFR8i5CxP
jRKRDOBEy0PPOLJDD0iUr9GX /小时/ EO4jQ7B / GszjhPiPx + gJCilaMY + jrSczjxpsK
OXzpZEdT1NqMrzgvIZPHYhQzAiw9vQzov3vezDwKgKcRrUixZ2B8uiEQNn7Wa2Qz
WF3vL + 6CGflFNYQcc0leDQBe86baYhCollouP4jfaH9KcMkYYw ==
---- END SSH2 PUBLIC KEY ----
Bouncy castle只是不了解这种公钥(SSH2)格式(您可以通过查看如果您愿意,请使用 PemReader
的源代码)。不幸的是,我不知道如何在C#中将其转换为适当的格式,但是您可以使用许多工具来做到这一点,例如使用ssh-keygen(也可以在gitbash中使用)或openssl。转换为PEM时,您的公钥将如下所示:
----- BEGIN RSA公钥-----
MIIBCAKCAQEAk0AmagKx285Ufbri / OLC + f3WagL1Ho + DrYdDSbuU7cJAq + uD9xGv
vP9m2JavSP4wO9i9pB / cmCFMPoIj3oGJt1 / cnLb / U2juneOw6Uo0N3F8TXdyXfZN
AIPhq / jw0YfIypTFTTvFkKXfTArIwW / bQBW8 / dujFR8i5CxPjRKRDOBEy0PPOLJD
D0iUr9GX /小时/ EO4jQ7B / GszjhPiPx + gJCilaMY + jrSczjxpsKOXzpZEdT1NqMrzgv
IZPHYhQzAiw9vQzov3vezDwKgKcRrUixZ2B8uiEQNn7Wa2QzWF3vL + 6CGflFNYQc
c0leDQBe86baYhCollouP4jfaH9KcMkYYwIBJQ == b
-c
您的当前代码将正确处理它,但有一点改动:
var pemReader =新的PemReader(stringReader);
var pemObject = pemReader.ReadObject(); // 空值!
//已经是AsymmetricKeyParameter
结果=(((AsymmetricKeyParameter)pemObject);
I am trying o create a method that constructs an AsymmetricKeyParameter
from a PEM encoded public key. Unfortunately, pemReader.ReadObject()
return null.
Here's a working solution for a private key: convert PEM encoded RSA private key to AsymmetricKeyParameter
What is wrong with this method?
static AsymmetricKeyParameter ReadPublicKeyFromPemEncodedString(string pemEncodedKey)
{
AsymmetricKeyParameter result = null;
using (var stringReader = new StringReader(pemEncodedKey))
{
var pemReader = new PemReader(stringReader);
var pemObject = pemReader.ReadObject(); // null!
result = ((AsymmetricCipherKeyPair)pemObject).Public;
}
return result;
}
Here is the PEM-encoded public key I am testing with. I have tried without the comment and also removing SSH2.
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20170608"
AAAAB3NzaC1yc2EAAAABJQAAAQEAk0AmagKx285Ufbri/olc+f3WagL1Ho+DrYdD
SbuU7cJAq+uD9xGvvP9m2JavSP4wO9i9pB/cmCFMPoIj3oGJt1/cnLb/U2juneOw
6Uo0N3F8TXdyXfZNAIPhq/jw0YfIypTFTTvFkKXfTArIwW/bQBW8/dujFR8i5CxP
jRKRDOBEy0PPOLJDD0iUr9GX/h/EO4jQ7B/GszjhPiPx+gJCilaMY+jrSczjxpsK
OXzpZEdT1NqMrzgvIZPHYhQzAiw9vQzov3vezDwKgKcRrUixZ2B8uiEQNn7Wa2Qz
WF3vL+6CGflFNYQcc0leDQBe86baYhCollouP4jfaH9KcMkYYw==
---- END SSH2 PUBLIC KEY ----
解决方案 Bouncy castle just does not understand this format of public key (SSH2) (you can verify this by looking at source code of PemReader
if you would like to). Unfortunately I don't know how to convert it to appropriate format in C#, but you can do that with many tools, for example with ssh-keygen (also available in gitbash for windows), or openssl. Your public key will look like this when converted to PEM:
-----BEGIN RSA PUBLIC KEY-----
MIIBCAKCAQEAk0AmagKx285Ufbri/olc+f3WagL1Ho+DrYdDSbuU7cJAq+uD9xGv
vP9m2JavSP4wO9i9pB/cmCFMPoIj3oGJt1/cnLb/U2juneOw6Uo0N3F8TXdyXfZN
AIPhq/jw0YfIypTFTTvFkKXfTArIwW/bQBW8/dujFR8i5CxPjRKRDOBEy0PPOLJD
D0iUr9GX/h/EO4jQ7B/GszjhPiPx+gJCilaMY+jrSczjxpsKOXzpZEdT1NqMrzgv
IZPHYhQzAiw9vQzov3vezDwKgKcRrUixZ2B8uiEQNn7Wa2QzWF3vL+6CGflFNYQc
c0leDQBe86baYhCollouP4jfaH9KcMkYYwIBJQ==
-----END RSA PUBLIC KEY-----
And it will be correctly handled by your current code, with a little change:
var pemReader = new PemReader(stringReader);
var pemObject = pemReader.ReadObject(); // null!
// it's already AsymmetricKeyParameter
result = ((AsymmetricKeyParameter)pemObject);
这篇关于将PEM编码的RSA公钥转换为AsymmetricKeyParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!