【问题标题】:Creating PKCS1 public key in Net Framework在 Net Framework 中创建 PKCS1 公钥
【发布时间】:2011-05-30 22:49:39
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: c# .net cryptography rsa pkcs#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();
        }
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-25
        • 2022-01-15
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-14
        相关资源
        最近更新 更多