gpt4 book ai didi

c# - 无法将类型为 'Org.BouncyCaSTLe.Asn1.DerSequence' 的对象转换为类型 'Org.BouncyCaSTLe.Asn1.DerInteger'

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:08 27 4
gpt4 key购买 nike

在我的c#项目中,我放了这段代码:

最初,我从我用充气城堡创建的证书中恢复, key 对,然后我提取私钥,我的目标是,它是一种格式。 pem,

 AsymmetricKeyParameter private_RSA = keyPair2.Private;


PrivateKeyInfo k_RSA = PrivateKeyInfoFactory.CreatePrivateKeyInfo(private_RSA);
byte[] serializedKey_priv_RSA = k_RSA.ToAsn1Object().GetDerEncoded();


// byte[] clé = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(serializedKey);

string data_priv_RSA = Convert.ToBase64String(serializedKey_priv_RSA);

using (fluxInfos2 = new StreamWriter("myprivatekey.pem"))
{
string ligne = " -----BEGIN RSA PRIVATE KEY----- ";
fluxInfos2.WriteLine(ligne);
fluxInfos2.WriteLine(data_priv_RSA);
string lige2 = "-----END RSA PRIVATE KEY----- ";

fluxInfos2.WriteLine(lige2);


}
fluxInfos2.Close();

我的 key 创建完美,即文件包含的内容;

-----BEGIN RSA PRIVATE KEY-----

MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQBr1EhPxHeVax8QbXQtUwaSyqhZehjxkgVp89Rkn3awfo7f9usQdLSp3tLSwAkHMvqi3UKSwUh8FlzCC0CBZCx1LOY05NS613tU/gaI7r+Zl4Iq+PevetEy8WxViRGoye/A9TU5r+BshT12MWYzVE/BYAb6OcsHZ5QztA+GDXA01YnTpgwOenkJXmrmmkSif80hk+NAWglnCqUzl3apZHhgVLpdkGDJPjhy8l9qOt6lu2gBQVX0AC8MyMt5Uqx9yFcR65+S8v8TV5Bc7Kk+9nkCl503k8Sn1fw4OWc6cMhK9E4/LF5MUIMO/UnIPQKeLbTlY8hAYG0sN0AJT/3IfxAgMBAAECggEAAyACT1BLYQmi9e1y5ozOr2Q40eJgLeM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ymcAL1/MLs6RvLj/Cd3DGVikS0y8nH5GGM8qxG0l1n9p96Z9AON+h0Jnen/HJvKPmq/SYkL2NNrg1CUHGL9FMDikMDBqIG/ttGIPCUZHyNAJJsMZJVfZJ9OP7Ru

-----END RSA PRIVATE KEY-----

现在,我必须做相反的事情,即从文件中包含的数据 key ,我必须有一个 RsaPrivateCrtKeyParameters。为此,我编写了这段小代码:

KeyFile string = @ "C: \ Users \ Me \ Documents \ Visual Studio 2010 \ Projects \ PEM \ PEM \ bin \ Debug \ myprivatekey.pem";

RsaPrivateCrtKeyParameters GetPrivateKey privat = (KeyFile);

比如GetPrivateKey,如下:

 public static RsaPrivateCrtKeyParameters GetPrivateKey(String pemFile)
{
if (string.IsNullOrEmpty(pemFile)) throw new ArgumentNullException("pemFile");

string privateKey = File.Exists(pemFile) ? File.ReadAllText(pemFile) : pemFile;

var reader = new PemReader(new StringReader(privateKey));
RsaPrivateCrtKeyParameters privkey = null;
Object obj = reader.ReadObject();
if (obj is AsymmetricCipherKeyPair)
{
privkey = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)obj).Private;
} return privkey;
}

问题是我收到异常,在:Object obj = reader.ReadObject();:

 Creating RSA private key problem: System.InvalidCastException:

Unable to cast object of type 'Org.BouncyCastle.Asn1.DerSequence' to type 'Org.BouncyCastle.Asn1.DerInteger'.
to Org.BouncyCastle.Asn1.Pkcs.RsaPrivateKeyStructure .. ctor (Asn1Sequence seq)
to Org.BouncyCastle.OpenSsl.PemReader.ReadKeyPair (String type, String endMarker)

你能帮我吗,谢谢你的帮助

最佳答案

由于您以 PKCS#8 格式写出私钥,因此适当的 PEM header 是“BEGIN PRIVATE KEY”(即没有“RSA”)。 PEM数据已经包含 key 的算法,“BEGIN RSA PRIVATE KEY”具有直接编码的RsaPrivateCrtKeyParameters的含义,这不是你写出来的。

在将 header 更改为“BEGIN PRIVATE KEY”后,我能够解析您的示例文件。 ReadObject 调用然后返回一个 RsaPrivateCrtKeyParameters 对象。

关于c# - 无法将类型为 'Org.BouncyCaSTLe.Asn1.DerSequence' 的对象转换为类型 'Org.BouncyCaSTLe.Asn1.DerInteger',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com