当前位置: 首页>>代码示例>>C#>>正文


C# X509CertificateParser.GetKeyUsage方法代码示例

本文整理汇总了C#中Org.BouncyCastle.X509.X509CertificateParser.GetKeyUsage方法的典型用法代码示例。如果您正苦于以下问题:C# X509CertificateParser.GetKeyUsage方法的具体用法?C# X509CertificateParser.GetKeyUsage怎么用?C# X509CertificateParser.GetKeyUsage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Org.BouncyCastle.X509.X509CertificateParser的用法示例。


在下文中一共展示了X509CertificateParser.GetKeyUsage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: checkCreation1


//.........这里部分代码省略.........
            certGen.SetSerialNumber(BigInteger.One);
            certGen.SetIssuerDN(new X509Name(ord, values));
            certGen.SetNotBefore(DateTime.UtcNow.AddSeconds(-50));
            certGen.SetNotAfter(DateTime.UtcNow.AddSeconds(50));
            certGen.SetSubjectDN(new X509Name(ord, values));
            certGen.SetPublicKey(pubKey);
            certGen.SetSignatureAlgorithm("SHA256WithRSAEncryption");

            X509Certificate cert = certGen.Generate(privKey);

            cert.CheckValidity(DateTime.UtcNow);

            cert.Verify(pubKey);

            ISet dummySet = cert.GetNonCriticalExtensionOids();
            if (dummySet != null)
            {
                Fail("non-critical oid set should be null");
            }
            dummySet = cert.GetCriticalExtensionOids();
            if (dummySet != null)
            {
                Fail("critical oid set should be null");
            }

            //
            // create the certificate - version 3 - with extensions
            //
            certGen = new X509V3CertificateGenerator();

            certGen.SetSerialNumber(BigInteger.One);
            certGen.SetIssuerDN(new X509Name(ord, values));
            certGen.SetNotBefore(DateTime.UtcNow.AddSeconds(-50));
            certGen.SetNotAfter(DateTime.UtcNow.AddSeconds(50));
            certGen.SetSubjectDN(new X509Name(ord, values));
            certGen.SetPublicKey(pubKey);
            certGen.SetSignatureAlgorithm("MD5WithRSAEncryption");
            certGen.AddExtension("2.5.29.15", true,
                new X509KeyUsage(X509KeyUsage.EncipherOnly));
            certGen.AddExtension("2.5.29.37", true,
                new DerSequence(KeyPurposeID.AnyExtendedKeyUsage));
            certGen.AddExtension("2.5.29.17", true,
                new GeneralNames(new GeneralName(GeneralName.Rfc822Name, "[email protected]")));

            cert = certGen.Generate(privKey);

            cert.CheckValidity(DateTime.UtcNow);

            cert.Verify(pubKey);

            cert = new X509CertificateParser().ReadCertificate(cert.GetEncoded());

            if (!cert.GetKeyUsage()[7])
            {
                Fail("error generating cert - key usage wrong.");
            }

            IList l = cert.GetExtendedKeyUsage();
            if (!l[0].Equals(KeyPurposeID.AnyExtendedKeyUsage.Id))
            {
                Fail("failed extended key usage test");
            }

            foreach (IList gn in cert.GetSubjectAlternativeNames())
            {
                if (!gn[1].Equals("[email protected]"))
                {
                    Fail("failed subject alternative names test");
                }
            }

            // Console.WriteLine(cert);

            //
            // create the certificate - version 1
            //
            X509V1CertificateGenerator certGen1 = new X509V1CertificateGenerator();

            certGen1.SetSerialNumber(BigInteger.One);
            certGen1.SetIssuerDN(new X509Name(ord, values));
            certGen1.SetNotBefore(DateTime.UtcNow.AddSeconds(-50));
            certGen1.SetNotAfter(DateTime.UtcNow.AddSeconds(50));
            certGen1.SetSubjectDN(new X509Name(ord, values));
            certGen1.SetPublicKey(pubKey);
            certGen1.SetSignatureAlgorithm("MD5WithRSAEncryption");

            cert = certGen1.Generate(privKey);

            cert.CheckValidity(DateTime.UtcNow);

            cert.Verify(pubKey);

            cert = new X509CertificateParser().ReadCertificate(cert.GetEncoded());

            // Console.WriteLine(cert);
            if (!cert.IssuerDN.Equivalent(cert.SubjectDN))
            {
                Fail("name comparison fails");
            }
        }
开发者ID:KimikoMuffin,项目名称:bc-csharp,代码行数:101,代码来源:CertTest.cs

示例2: checkKeyUsage

        internal void checkKeyUsage(
            int		id,
            byte[]	bytes)
        {
            string dump = "";

            try
            {
                X509Certificate cert = new X509CertificateParser().ReadCertificate(bytes);

                AsymmetricKeyParameter k = cert.GetPublicKey();

                if (cert.GetKeyUsage()[7])
                {
                    Fail("error generating cert - key usage wrong.");
                }

                // Console.WriteLine(cert);
            }
            catch (Exception e)
            {
                Fail(dump + SimpleTest.NewLine + Name + ": "+ id + " failed - exception " + e.Message, e);
            }

        }
开发者ID:KimikoMuffin,项目名称:bc-csharp,代码行数:25,代码来源:CertTest.cs


注:本文中的Org.BouncyCastle.X509.X509CertificateParser.GetKeyUsage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。