如何在.net中创建一个全新的x509Certificate2?

编程入门 行业动态 更新时间:2024-10-24 21:28:46
本文介绍了如何在中创建一个全新的x509Certificate2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过网络对其进行了搜索,找到了许多示例来从中的文件生成一个新的x509Certificate2,但是没有一个示例来演示如何从的开头生成一个全新的x509Certificate2。

I google it from web, find many samples to generate a new x509Certificate2 from a file in , but there is no one sample to show how to generate a completely new x509Certificate2 from the beginning in .

有没有人可以告诉我如何在中做到这一点?

Is there any one that can tell me how to do it in ?

非常感谢。

推荐答案

以下是您可以使用的代码:

Here's a code you can use:

static X509Certificate2 GenerateCertificate(string certName) { var keypairgen = new RsaKeyPairGenerator(); keypairgen.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), 1024)); var keypair = keypairgen.GenerateKeyPair(); var gen = new X509V3CertificateGenerator(); var CN = new X509Name("CN=" + certName); var SN = BigInteger.ProbablePrime(120, new Random()); gen.SetSerialNumber(SN); gen.SetSubjectDN(CN); gen.SetIssuerDN(CN); gen.SetNotAfter(DateTime.MaxValue); gen.SetNotBefore(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0))); gen.SetSignatureAlgorithm("MD5WithRSA"); gen.SetPublicKey(keypair.Public); var newCert = gen.Generate(keypair.Private); return new X509Certificate2(DotNetUtilities.ToX509Certificate((Org.BouncyCastle.X509.X509Certificate)newCert)); }

要使此功能有效,请不要忘记添加对 BouncyCastle库

for this to work, don't forget to add reference to BouncyCastle library

更多推荐

如何在.net中创建一个全新的x509Certificate2?

本文发布于:2023-11-14 10:26:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   如何在   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!