简体   繁体   English

反序列化RSA公私钥C#

[英]Deserialize RSA public and private key C#

Note: There is no answer to my question on stackoverflow注意:我在stackoverflow上的问题没有答案

As a part of my project I have to encrypt some text with RSA and I have got a public key from another company.作为我项目的一部分,我必须使用 RSA 加密一些文本,并且我从另一家公司获得了公钥。 The public key looks like this:公钥如下所示:

var publicKey="MIGfMA0GCSq2GSIb3DQEBAQUAA4GNADCBiQKBgQCgFGVfrY4jQSoZQWWygZ83roKXWD4YeT2x2p41dGkPixe73rT2IW04glatgN2vgoZsoHuOPqah5and6kAmK2ujmCHu6D1auJhE2tXP+yLkpSiYMQucDKmCsWXlC5K7OSL77TXXcfvTvyZcjObEz6LIBRzs6+FqpFbUO9SJEfh6wIDAQAB" 

The problem is that I don't know what is its format and how to deserialize it to RSAParameters .问题是我不知道它的格式是什么以及如何将其反序列化为RSAParameters Other examples on the Internet have used XML serialization.网上其他例子都使用了XML序列化。 The key is created by Java.密钥由 Java 创建。

Then I also want to know how to deserialize its related private key which I don't have access to any sample of it right now.然后我还想知道如何反序列化它的相关私钥,我现在无法访问它的任何样本。

Update:更新:

Here is part of my code:这是我的代码的一部分:

var pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiiTx4F35eWP10AFMAo8MLhCKq2ryKFG9PKKWeMLQuwMSdiQq347BkMYA+Q+YscScf7weUSTk9BHVNNfTchDwzjQrIoz6TZGggqD+ufin1Ccy0Sp6QeBMnIB89JsdzQGpVcsoTxk53grW0nYY8D+rlFvBwFicKe/tmVPVMYsEyFwIDAQAB";


...

  public static RSACryptoServiceProvider ImportPublicKey(string pem)
        {
            //var newPem = "-----BEGIN PUBLIC KEY-----\n" + pem + "-----END PUBLIC KEY-----";
            Org.BouncyCastle.OpenSsl.PemReader pr = new Org.BouncyCastle.OpenSsl.PemReader(new StringReader(Pem));
            Org.BouncyCastle.Crypto.AsymmetricKeyParameter publicKey = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pr.ReadObject();
            RSAParameters rsaParams = Org.BouncyCastle.Security.DotNetUtilities.ToRSAParameters((Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)publicKey);

           

            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();// cspParams);
            csp.ImportParameters(rsaParams);
            return csp;

        }

The posted key is a PEM encoded public key in X.509 (SPKI) format, but without header ( -----BEGIN PUBLIC KEY----- ) and footer ( -----END PUBLIC KEY----- ).发布的密钥是 X.509 (SPKI) 格式的 PEM 编码公钥,但没有 header ( -----BEGIN PUBLIC KEY----- ) 和页脚 ( -----END PUBLIC KEY----- )。 This can be easily verified with an ASN.1 parser, eg here .这可以通过 ASN.1 解析器轻松验证,例如这里

The import of such a key depends on the .NET version.此类密钥的导入取决于 .NET 版本。 .NET Core offers from v3.0 on methods that directly support the import of PKCS#1, PKCS#8 and X.509 keys, eg RSA.ImportSubjectPublicKeyInfo for the latter. .NET Core从 v3.0 开始提供直接支持导入 PKCS#1、PKCS#8 和 X.509 密钥的方法,例如后者的RSA.ImportSubjectPublicKeyInfo This option is not available for .NET Framework , but BouncyCastle offers a similarly comfortable solution.此选项不适用于.NET 框架,但BouncyCastle提供了类似的舒适解决方案。

Here (see ImportPublicKey method) is an example that imports a PEM encoded public key in X.509 (SPKI) format using BouncyCastle . 这里(参见ImportPublicKey方法)是一个使用BouncyCastle 导入X.509 (SPKI) 格式的 PEM 编码公钥的示例。 However, the PemReader used there expects the complete PEM data, including header and footer, both separated from the body by line breaks.但是,在那里使用的PemReader需要完整的 PEM 数据,包括 header 和页脚,两者都通过换行符与正文分开。 Therefore, when using the public keys posted here, header and footer must be added accordingly, eg:因此,使用此处发布的公钥时,必须相应添加 header 和页脚,例如:

using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
...
// from: https://gist.github.com/valep27/4a720c25b35fff83fbf872516f847863
public static RSACryptoServiceProvider ImportPublicKey(string pemBody)
{
    var pem = "-----BEGIN PUBLIC KEY-----\n" + pemBody + "\n-----END PUBLIC KEY-----";      // Add header and footer
    PemReader pr = new PemReader(new StringReader(pem));
    AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter)pr.ReadObject();
    RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKey);

    RSACryptoServiceProvider csp = new RSACryptoServiceProvider();// cspParams);
    csp.ImportParameters(rsaParams);
    return csp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM