有弹性的城堡解码CSR c# [英] Bouncy Castle Decode CSR c#

查看:88
本文介绍了有弹性的城堡解码CSR c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图学习如何用充气城堡解码csr,因为我打算用它来学习其他加密货币。

I'm attempting to learn how to decode a csr with the bouncy castle as I intend to use it to learn other crypto things.

我也没有找到有关c#的任何文档,只是java。

I'm also not finding any documentation for c# just java.

这就是我调用 DecodeCSR(string str)的方式:

 static void Main(string[] args)
    {
        string csr = "-----BEGIN NEW CERTIFICATE REQUEST-----...";
        DecodeCSR(csr);
        Console.ReadLine();
    }

这是我获取IO异常的方法(包括以下内容),我的主要问题是我不知道该给 Pkcs10CertificationRequest

Here's my method where I'm getting an IO Exception(Included below), my main problem is I don't know what to give Pkcs10CertificationRequest.

private static void DecodeCSR(string csr)
    {
        try
        {
            byte[] csrEncode = Encoding.UTF8.GetBytes(csr);
            Pkcs10CertificationRequest csrDecoder = new Pkcs10CertificationRequest(csrEncode);
            byte[] csrDecode = csrDecoder.GetEncoded();
        }
        catch (IOException e)
        {
            Console.WriteLine(e);
        }

    }

错误消息:

 System.IO.IOException: unknown tag 13 encountered
   at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
   at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
   at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)
   at Org.BouncyCastle.Pkcs.Pkcs10CertificationRequest..ctor(Byte[] encoded)


推荐答案

伍德沃德的回答是非常正确的。我只补充说该功能是由Org.BouncyCastle.OpenSsl.PemReader直接实现的:

Luke Woodward's answer is quite correct. I will add only that the functionality is implemented directly by Org.BouncyCastle.OpenSsl.PemReader:

Pkcs10CertificationRequest decodedCsr = (Pkcs10CertificationRequest)new PemReader(new StringReader(csr)).ReadObject();

除了简洁起见,这对于包括更好的语法检查以及支持标头和加密(都没有在此特定示例中使用。)

Apart from brevity, this is preferable for including better syntax checking, and supporting things like headers and encryption (neither used in this particular example).

这篇关于有弹性的城堡解码CSR c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆