C# 生成自签名CA证书
2022/6/6 1:21:21
本文主要是介绍C# 生成自签名CA证书,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C# 生成自签名CA证书
string password = "213978863940714"; string signatureAlgorithm = "SHA1WithRSA"; // Generate RSA key pair var rsaGenerator = new RsaKeyPairGenerator(); var randomGenerator = new CryptoApiRandomGenerator(); var secureRandom = new SecureRandom(randomGenerator); var keyParameters = new KeyGenerationParameters(secureRandom, 1024); rsaGenerator.Init(keyParameters); var keyPair = rsaGenerator.GenerateKeyPair(); // Generate certificate var attributes = new Hashtable(); attributes[X509Name.E] = UserInfo.idCard;//设置dn信息的邮箱地址 attributes[X509Name.CN] = UserInfo.idCard;//设置证书的用户,也就是颁发给谁 attributes[X509Name.O] = "www.shwdztc.com";//设置证书的办法者 attributes[X509Name.C] = "Zh";//证书的语言 //这里是证书颁发者的信息 var ordering = new ArrayList(); ordering.Add(X509Name.E); ordering.Add(X509Name.CN); ordering.Add(X509Name.O); ordering.Add(X509Name.C); var certificateGenerator = new X509V3CertificateGenerator(); //设置证书序列化号 certificateGenerator.SetSerialNumber(BigInteger.ProbablePrime(120, new Random())); //设置颁发者dn信息 certificateGenerator.SetIssuerDN(new X509Name(ordering, attributes)); //设置证书生效时间 certificateGenerator.SetNotBefore(DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0))); //设置证书失效时间 certificateGenerator.SetNotAfter(DateTime.Today.AddDays(365)); //设置接受者dn信息 certificateGenerator.SetSubjectDN(new X509Name(ordering, attributes)); //设置证书的公钥 certificateGenerator.SetPublicKey(keyPair.Public); //设置证书的加密算法 certificateGenerator.SetSignatureAlgorithm(signatureAlgorithm); certificateGenerator.AddExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false)); certificateGenerator.AddExtension(X509Extensions.AuthorityKeyIdentifier, true, new AuthorityKeyIdentifier(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public))); certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false, new ExtendedKeyUsage(new ArrayList() { new DerObjectIdentifier("1.3.6.1.5.5.7.3.2") })); //创建证书,如果需要cer格式的证书,到这里就可以了。如果是pfx格式的就需要加上访问密码 var x509Certificate = certificateGenerator.Generate(keyPair.Private); byte[] pkcs12Bytes = DotNetUtilities.ToX509Certificate(x509Certificate).Export(X509ContentType.Pfx, password); var certificate = new X509Certificate2(pkcs12Bytes, password); certificate.PrivateKey = EncryHelper.ToDotNetKey((RsaPrivateCrtKeyParameters)keyPair.Private); var array = certificate.Export(X509ContentType.Pfx, password); var cerArray = certificate.Export(X509ContentType.Cert); string path = HttpContext.Current.Server.MapPath("~/files/userword/Word/" + UserInfo.idCard + ".pfx"); string pathcer = HttpContext.Current.Server.MapPath("~/files/userword/Word/" + UserInfo.idCard + ".cer"); FileStream fsCA = new FileStream(path, FileMode.Create); //将byte数组写入文件中 fsCA.Write(array, 0, array.Length); fsCA.Close(); FileStream fscer = new FileStream(pathcer, FileMode.Create); //将byte数组写入文件中 fscer.Write(cerArray, 0, cerArray.Length); fscer.Close();
这篇关于C# 生成自签名CA证书的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#