RSACryptoServiceProvider 和 openSSL 之间的互操作性

发布于 2024-09-10 06:27:11 字数 389 浏览 8 评论 0原文

我已经使用 .NET 类 RSACryptoServiceProvider 来获取密钥对:

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
  File.WriteAllText ("PublicKeyOnly.xml", rsa.ToXmlString (false));
  File.WriteAllText ("PublicPrivate.xml", rsa.ToXmlString (true));
}

现在,我想将其与 openSSH 一起使用,但密钥格式看起来并不相似。 有谁知道如何将公钥和私钥转换为 openSSH 可以使用的文件?

谢谢!

I've used the .NET class RSACryptoServiceProvider to get a keypair:

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
  File.WriteAllText ("PublicKeyOnly.xml", rsa.ToXmlString (false));
  File.WriteAllText ("PublicPrivate.xml", rsa.ToXmlString (true));
}

Now, I would like to use this with openSSH, but the key format looks nothing alike.
Does anyone know how to convert both the public and private keys to files that openSSH can use?

Thanks!

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

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

发布评论

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

评论(2

世界如花海般美丽 2024-09-17 06:27:11

我确实需要实现 Openssl 与 RSACryptoServiceProvider 的互操作性,以便我可以实现软件许可证密钥系统 (参考)。

我需要能够使用 openssl 在 Linux 中创建私钥和公钥,以便以后可以将它们用于 PHP Web 应用程序中的许可证管理。然而,也可以将它们用作 VB.Net 应用程序中 RSA 签名许可系统的基础。

经过一周的搜索,我最终发现这是完全可能的,所以我想我会分享它。

在 Linux(或任何其他有用的操作系统)上启动并使用 openssl 创建私钥 (private.pem)、公钥 (public.pem)、证书 (certificate.crt) 和个人信息交换文件 (certificate.pfx) )。不用担心 CN 和 emailAddress 字段,证书和 pfx 文件仅用作将公钥或私钥获取到 RSACryptoServiceProvider 对象中的工具。

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout
openssl req -nodes -x509 -days 3650 -subj '/CN=www.example.com/[email protected]' -new -key private.pem -out certificate.crt
openssl pkcs12 -export -out certificate.pfx -inkey private.pem -in certificate.crt

现在将私钥放入代码中:

Dim cert As New X509Certificate2("certificate.pfx", "", X509KeyStorageFlags.Exportable)
Dim rsaProvider As RSACryptoServiceProvider = DirectCast(cert.PrivateKey, RSACryptoServiceProvider)

如果您需要私钥或公钥,请尝试以下操作:

msgbox(rsaProvider.ToXmlString(True))  'Private key in XML format
msgbox(rsaProvider.ToXmlString(False)) 'Public key in XML format

将公钥放入代码中:

Dim cert As New X509Certificate2("certificate.crt")
Dim rsaProvider As RSACryptoServiceProvider = DirectCast(cert.PublicKey.Key, RSACryptoServiceProvider)

如果您需要公钥,请尝试以下操作:

msgbox(rsaProvider.ToXmlString(False))  'Public key in XML format

更多内容即将到来......

I really needed to achieve Openssl interoperability with RSACryptoServiceProvider, so that I could implement a software licence key system (Ref).

I needed to be able to create the private and public keys in Linux using openssl so that they could later be used for license management in a PHP web application. Yet, also use them as the basis of an RSA signature license system in a VB.Net applciation.

After a week of searching, I eventually discovered that this is perfectly possible, so I thought I would share it.

Start on Linux (or any other useful OS) and use openssl to create a private key (private.pem), a public key (public.pem), a certificate (certificate.crt) and a Personal Information Exchange File (certificate.pfx). Don't worry about the CN and emailAddress fields, the certificate and pfx files are only being used as a vehicle to get the public or private key into the RSACryptoServiceProvider object.

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout
openssl req -nodes -x509 -days 3650 -subj '/CN=www.example.com/[email protected]' -new -key private.pem -out certificate.crt
openssl pkcs12 -export -out certificate.pfx -inkey private.pem -in certificate.crt

Now to get the private key into the code:

Dim cert As New X509Certificate2("certificate.pfx", "", X509KeyStorageFlags.Exportable)
Dim rsaProvider As RSACryptoServiceProvider = DirectCast(cert.PrivateKey, RSACryptoServiceProvider)

If you need the private key or public key try this:

msgbox(rsaProvider.ToXmlString(True))  'Private key in XML format
msgbox(rsaProvider.ToXmlString(False)) 'Public key in XML format

To get the public key into the code:

Dim cert As New X509Certificate2("certificate.crt")
Dim rsaProvider As RSACryptoServiceProvider = DirectCast(cert.PublicKey.Key, RSACryptoServiceProvider)

If you need the public key try this:

msgbox(rsaProvider.ToXmlString(False))  'Public key in XML format

More to come .....

留蓝 2024-09-17 06:27:11

这篇关于使用 OpenSSL 和 RSACryptoServiceProvider 的博客文章指出这是可能的,但作者最终使用 Chilkat RSA 库最终在 C# 内与 OpenSSL 进行互操作。 .NET 世界不支持 PEM 格式,因此您可以使用 JavaScience 中名为 OpenSSLKey.cs 的库;然而,正如博客文章的作者提到的,他们因此遇到了问题(引用):

OpenSSL:只能签署一小部分
适合单个块的数据。
数据被填充并签名。这
反向称为“验证”,其中
如果数据是“无符号”的,然后
未填充,原始数据是
返回。

[Windows]:可以签署任意数量的
数据。 Sign* 方法首先对
数据,然后填充哈希值
签署了。验证*方法期望
三个输入:原始数据,a
哈希算法名称和签名
数据。原始数据经过哈希处理,
取消签名/取消填充的结果是
与原始哈希值进行比较
数据。

所以我建议您使用 Chilkat RSA 库。

This blog post on using OpenSSL and RSACryptoServiceProvider states that it is possible, but the author ended up using the Chilkat RSA Library to ultimately interoperate with OpenSSL from within C#. The PEM format is not supported in the .NET world so you could use this library from JavaScience called OpenSSLKey.cs; however, as the author of the blog post mentions they had problems due to this (quoted):

OpenSSL: Can only sign small bits of
data that fit within a single block.
The data is padded and signed. The
reverse is called "verify" and in that
case the data is "unsigned" and then
unpadded and the original data is
returned.

[Windows]: Can sign any amount of
data. The Sign* methods first hash the
data and then the hash is padded and
signed. The Verify* methods expect
three inputs: the original data, a
hash algorithm name, and the signature
data. The original data is hashed and
the result of unsigning/unpadding is
compared with the hash of the original
data.

So I recommend you go with the Chilkat RSA library.

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