在Net Framework中创建PKCS1公钥

时间:2010-12-26 21:53:21

标签: c# .net cryptography rsa pkcs#1

我需要创建一个RSA 1024位“没有PEM头的PKCS1公钥”并将其存储为字节数组。 在网上。框架我有2个数组:RSAParameters.Modulus和RSAParameters.Exponent(根据我的理解,它构成了一个公钥)。 如何将这两个数组转换为“没有PEM头的PKCS1公钥”?

谢谢。

2 个答案:

答案 0 :(得分:2)

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

RSAPublicKey :: = SEQUENCE {     模数INTEGER, - n     publicExponent INTEGER - e }

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

答案 1 :(得分:1)

必须有一种更简单的方法,但一种方法是使用Bouncycastle C# library及其中的一些类,如下例所示:

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();
    }