在C#中创建X509Certificate2时如何更改发行者名称 [英] How to change issuer Name while creating X509Certificate2 in C#

查看:61
本文介绍了在C#中创建X509Certificate2时如何更改发行者名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.net应用程序创建X509Certificate2证书.创建证书时,如何设置颁发者名称?当前发行者名称显示与主题名称相同.

I am working with creating X509Certificate2 certificate in my C#.net application. While creating certificate, how to set the issuer name? currently issuer name shows same as subject name.

请帮助.

推荐答案

嗯.

最后,我使用 Bouncy Castle dll 创建证书.使用此方法,可以设置颁发者名称.

Finally I have used Bouncy Castle dll to create certificates.Using this, there is a method to set issuer name.

这是生成 x509证书并存储到受信任的人商店中的完整代码:

This is the fully code to generate x509Certificate and to store into Trusted People store:

 private X509Certificate2 GeneratePFXFile(string certificate,string company,string   email,string state,string locality,string username,string country)
    {
        X509Certificate2 cert = null;
        try
        {
            var kpgen = new RsaKeyPairGenerator();

            kpgen.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), 2048));

            var kp = kpgen.GenerateKeyPair();

            var gen = new X509V3CertificateGenerator();

            var certName = new X509Name("CN=" + certificate);
            var issuer = new X509Name("C="+country+",O="+company+",OU=LBC Mundial Corp.USA,E="+email+",L="+locality+",ST="+state);
            var serialNo = BigInteger.ProbablePrime(120, new Random());

            gen.SetSerialNumber(serialNo);
            gen.SetSubjectDN(certName);
            gen.SetIssuerDN(issuer);
            gen.SetNotAfter(DateTime.Now.AddYears(50));
            gen.SetNotBefore(DateTime.Now);
            gen.SetSignatureAlgorithm("MD5WithRSA");
            gen.SetPublicKey(kp.Public);

            gen.AddExtension(
                X509Extensions.AuthorityKeyIdentifier.Id,
                false,
                new AuthorityKeyIdentifier(
                    SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public),
                    new GeneralNames(new GeneralName(certName)),
                    serialNo));

            gen.AddExtension(
                X509Extensions.ExtendedKeyUsage.Id,
                false,
                new ExtendedKeyUsage(new ArrayList() { new DerObjectIdentifier("1.3.6.1.5.5.7.3.1") }));

            var newCert = gen.Generate(kp.Private);
            byte[] pfx = DotNetUtilities.ToX509Certificate(newCert).Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pfx, (string)null);

            X509Store store = new X509Store((StoreName)StoreName.TrustedPeople, (StoreLocation)StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);

            cert = new X509Certificate2(pfx,(string)null, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);

            store.Add(cert);
            store.Close();
        }
        catch (Exception ex)
        {
            ShowError(ex.Message);
            return null;
        }
        return cert;
    }

这篇关于在C#中创建X509Certificate2时如何更改发行者名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆