C# 在Net Framework中创建PKCS1公钥

C# 在Net Framework中创建PKCS1公钥,c#,.net,cryptography,rsa,pkcs#1,C#,.net,Cryptography,Rsa,Pkcs#1,我需要创建一个RSA 1024位的“不带PEM头的PKCS1公钥”,并将其存储为字节数组。 在网上。FrameworkI有两个数组:rsapameters.module和rsapameters.Exponent(据我所知,它们构成了一个公钥)。 如何将这两个数组转换为“没有PEM头的PKCS1公钥” 谢谢。一定有更简单的方法,但有一种方法是使用及其一些类,如以下示例所示: using System.Security.Cryptography; using Org.BouncyCastle.Asn

我需要创建一个RSA 1024位的“不带PEM头的PKCS1公钥”,并将其存储为字节数组。 在网上。FrameworkI有两个数组:rsapameters.module和rsapameters.Exponent(据我所知,它们构成了一个公钥)。 如何将这两个数组转换为“没有PEM头的PKCS1公钥”


谢谢。

一定有更简单的方法,但有一种方法是使用及其一些类,如以下示例所示:

using System.Security.Cryptography;
using Org.BouncyCastle.Asn1;

    public static byte[] DEREncode(RSACryptoServiceProvider rsa)
    {
        RSAParameters rsaParams = rsa.ExportParameters(false);
        DerInteger n = new DerInteger(rsaParams.Modulus);
        DerInteger e = new DerInteger(rsaParams.Exponent);
        DerSequence seq = new DerSequence(new Asn1Encodable[] {n, e});
        return seq.GetEncoded();
    }

如果您只需要对RSA公钥进行编码,您可以编写自己的包装器,它需要5行编码。 RSA公钥应表示为以下ASN.1结构:

RSAPublicKey::=序列{ 模数整数,--n 指数整数——e }

但是,这需要您学习一些ASN.1基础知识。 此外,您应该确保只需要以这种格式保存,应用程序也可能需要添加算法标识符