我正在尝试学习如何用充气城堡解码csr,因为我打算用它来学习其他加密事物。
我也没有找到任何留档的c#只是java。
这是我调用DecodeCSR(string str)
的方式:
static void Main(string[] args)
{
string csr = "-----BEGIN NEW CERTIFICATE REQUEST-----...";
DecodeCSR(csr);
Console.ReadLine();
}
这是我的方法,我得到一个IO异常(包括下面),我的主要问题是我不知道该给什么Pkcs10证书请求
。
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)
Luke Woodward的回答非常正确。我只补充说该功能是由Org. BouncyCastle.OpenSsl.PemReader直接实现的:
Pkcs10CertificationRequest decodedCsr = (Pkcs10CertificationRequest)new PemReader(new StringReader(csr)).ReadObject();
除了简洁之外,这对于包含更好的语法检查以及支持标头和加密等内容(在此特定示例中均未使用)更可取。
您试图以错误的方式将CSR解码为字节数组。
这是我创建的一个测试CSR来回答这个问题:
-----BEGIN NEW CERTIFICATE REQUEST-----
MIIC3zCCAccCAQAwajELMAkGA1UEBhMCWFgxEDAOBgNVBAgTB1Vua25vd24xEDAOBgNVBAcTB05v
d2hlcmUxGjAYBgNVBAoTEVRlc3RlcnMgVW5saW1pdGVkMQ0wCwYDVQQLEwRUZXN0MQwwCgYDVQQD
EwNCb2IwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCUf5kRJVMNwPu+nEnJ3Av5J59H
vkeU0fOQtI+ezvHoJXc0A4iGDBm9PUw+eAXHw237Yu7+AyzgQCD8mwQt/lHLjXG/yeX66PhTFlzH
Lhk4rqL9PQATbl4uCYtWoJmeEOWHL26dfI//AFsL9/smVyjD1mJfee6998PwwHB6BxqSaqXiR1Et
lC4jx+B2E6btvMtREQ94cECoQudSgo3MRMBH+FuEcCofNtwosEpptjIN59ywVfDDU2Me58r930Ej
A8EZbXwYpUjwaHtXK80TKq8yF3Af7Hbt6FhkXOv1QtF0EYUKvE4BVTAx4mPLUy2mct8Ft8/+/Lbt
HDmslaBCAFZrAgMBAAGgMDAuBgkqhkiG9w0BCQ4xITAfMB0GA1UdDgQWBBRt0aHvY5c29iUS6/7E
3FC+Bn3FNDANBgkqhkiG9w0BAQsFAAOCAQEAe+e+3WfxJd/ztcIDRR8YfVkeOlE0b8Erze13fQi/
GsBJQ2MF/7T8x/8ZF2CqusrmLTzb5sY5qVKlRgnguow6xnGu0QOiJdC4kgoqgAaxDwz/eIhMGkt9
hyfTkMWiMD0OfYpdhXWUHrvbzWyUNI1ouWjOoqS03LvSiT4Cq+7Xca8ETVyuBN1FZeFUxO59goqI
eKKenzPqJHcPUjkQbUBgsfKpuRXveRG+vacCt0chWUK4TEVyPzH2EDH0kiQh6dsVizKrHUihh5K/
BhZNqzyBW7G5vbxq4wBz7cLDskfqTsOnHRsAlYmNsXO2wV1LQ2f04F+FHIzZ+IWaO86cxTPMtA==
-----END NEW CERTIFICATE REQUEST-----
要解码这样的CSR,我们需要:
-----BEGIN New CERTIFICATE REQUEST-----
和-----END New CERTIFICATE REQUEST-----
行,Pkcs10证书请求
。下面是一些解码CSR并打印主题名称的代码:
string csr = "....";
char[] characters =
csr.Replace("-----BEGIN NEW CERTIFICATE REQUEST-----", "")
.Replace("-----END NEW CERTIFICATE REQUEST-----", "")
.ToCharArray();
byte[] csrEncode = Convert.FromBase64CharArray(characters, 0, characters.Length);
Pkcs10CertificationRequest decodedCsr = new Pkcs10CertificationRequest(csrEncode);
Console.WriteLine(decodedCsr.GetCertificationRequestInfo().Subject);
当我在上面的CSR上运行这段代码时,它会写入以下输出行:
C=XX,ST=Unknown,L=Nowhere,O=Testers Unlimited,OU=Test,CN=Bob