首页 > 编程语言 >C# 生成自签名CA证书

C# 生成自签名CA证书

时间:2022-09-06 12:55:45浏览次数:50  
标签:证书 C# CA ordering certificateGenerator var new X509Name

  • 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,ordering,certificateGenerator,var,new,X509Name
From: https://www.cnblogs.com/sexintercourse/p/16661378.html

相关文章

  • 个人翻译Introduction to Linear Algebra, 5th Edition 2.4节(仅用于交流学习,非盈利)
    本书的翻译仅为交流学习!才疏学浅,不当的地方还望指正。请勿于其它用途!PDF文件 链接一:  https://pan.baidu.com/s/1aVHp2bZeezqrz5BRSn2ZiQ提取码:wd3q  链接二:http......
  • P2398 GCD SUM
    P2398GCDSUM题目大意\(\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)\)分析这个到是蛮好想的,我们推理一下。\(\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)=\sum_{k=1}^nk*\su......
  • Ceph安装部署
     Monitor Mgr MdsOSDDeploy数量22 41CPU8c8c 4c4cMemory16g16g 16g8g硬盘200g200g 500g*3200g系统ubuntu18.04u......
  • C语言学生选课系统
    C语言学生选课系统十、学生选课系统假定有n门课程,每门课程有课程编号,课程名称,课程性质,学时,授课学时,实验或上机学时,学分,开课学期等信息,学生可按要求(如总学分不得......
  • centos报错Error: Package: containerd.io-1.6.8-3.1.el7.x86_64 (base)
    远程帮客户电脑装东西,发现客户那边的机子都不出网。。。。全新的centos执行yuminstalldocker报错了-->FinishedDependencyResolutionError:Package:slirp4netns......
  • Android技术分享| Activity 过渡动画 — 让切换更加炫酷
    介绍在android5.0以上版本中,google为我们提供了几种activity切换的过渡动画,目的是为了让activity切换转场更加美观,而在android5.0之前的activity切换显得生硬......
  • LFS(Linux From Scratch)构建过程全记录(一):准备工作
    写在前面本人修学了一门课,名曰《操作系统课程设计》,其任务为基于LFS以编译源代码的方式制作一个基本的Linux操作系统,并且编写在linux下的GUI软件。本操作系统构建的全过......
  • C# DateTime.ParseExact使用
    在使用DateTime的ParseExact方法解析日期时间的时候要将这两种日期格式都写入string[]dateFormates={"yyyy-MM-dd","MM/yyyy/dd","yyyyMMdd","yyyy/MM/dd","yyyy/......
  • 面试~ThreadLocal
    四种常见的引用强引用---不可回收软引用---内存不足即回收弱引用---发现即回收虚引用---形同虚设,目的是用于对象回收跟踪ThreadLocal1、ThreadLoca是什么ThreadL......
  • handycontrol的使用
    Handycontrol的地址https://handyorg.github.io/handycontrol/需要新建项目后,导入到项目HandyControl-Nuget引入资源文件原始控件的Lable,可以加style标签,进行快速......