本文介绍了如何在中创建一个全新的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?
发布评论