cer证书签名验证[通俗易懂]

cer证书签名验证[通俗易懂]一个cer证书本身也是需要签名的,这是为了防止cer证书被篡改。证书有两种类型:1.根证书2.根证书签发的子证书。根证书比较特殊,它是自签名的。而其他子证书的签名公钥都保存在它的上级证书里面。可以用C#来做一些验证。首先是根证书的签名验证。//验证根证书签名X509Certificate2x509Root=newX

大家好,又见面了,我是你们的朋友全栈君。

一个cer证书本身也是需要签名的,这是为了防止cer证书被篡改。

证书有两种类型:

1. 根证书

2. 根证书签发的子证书。

根证书比较特殊,它是自签名的。而其他子证书的签名公钥都保存在它的上级证书里面。

可以用C#来做一些验证。

首先是根证书的签名验证。

        // 验证根证书签名
        X509Certificate2 x509Root = new X509Certificate2("C:\\Users\\kevin\\Desktop\\KevinRoot.cer");
        Console.WriteLine("Root Certificate Verified?: {0}{1}", x509Root.Verify(), Environment.NewLine);  // 根证书是自签名,所以可以通过。

很简单,因为根证书是自签名的,x509Root.Verify()会返回true。

然后是子证书的验证,

       X509Certificate2 x509 = new X509Certificate2("C:\\Users\\kevin\\Desktop\\ChildSubject2.cer");

        byte[] rawdata = x509.RawData;
        Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine);
        Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine);
        Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine);
        Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, true), Environment.NewLine);
        Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine);
    //    Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(false), Environment.NewLine);  // cer里面并没有私钥信息
        Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(false), Environment.NewLine);
        Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine);
        Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine);

这里我用自己创建的子证书,x509.Verify()总是返回false,就算我把根证书导入到“trust”里面,还是返回false,不知道为什么。但是如果我用公司的证书(verisign颁发的),却可以返回true。不知道是不是我自己创建的根证书,子证书有什么配置问题,有空再研究。反正验证也就这么回事。

下面的代码,用来检查整个证书链。

        //Output chain information of the selected certificate.
        X509Chain ch = new X509Chain();
        ch.Build(x509);
        Console.WriteLine("Chain Information");
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length);
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);
        //Output chain element information.
        Console.WriteLine("Chain Element Information");
        Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count);
        Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);

    //    int index = 0;
        foreach (X509ChainElement element in ch.ChainElements)
        {
            Console.WriteLine("Element subject name: {0}", element.Certificate.Subject);
            Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
            Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
            Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
            Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
            Console.WriteLine("Element information: {0}", element.Information);
            Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

            string a = element.Certificate.Thumbprint;
       //     string b = ch.ChainPolicy.ExtraStore[0].Thumbprint;
            //ch.ChainPolicy.ExtraStore[index - 1].Thumbprint;

            if (ch.ChainStatus.Length > 1)
            {
                for (int index = 0; index < element.ChainElementStatus.Length; index++)
                {
                    Console.WriteLine(element.ChainElementStatus[index].Status);
                    Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
                }
            }
        }

上面的代码也很简单,其实就是把整个证书链里面的每一个证书信息打印一下。具体的函数调用参数msdn。

下面是完整代码,注意里面的几个证书路径是我写死的,如果想测试下面的代码,只需要自己创建几个证书。

using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class CertSelect
{
    static void Main()
    {
        // 验证根证书签名
        X509Certificate2 x509Root = new X509Certificate2("C:\\Users\\kevin\\Desktop\\KevinRoot.cer");
        Console.WriteLine("Root Certificate Verified?: {0}{1}", x509Root.Verify(), Environment.NewLine);  // 根证书是自签名,所以可以通过。

        X509Certificate2 x509 = new X509Certificate2("C:\\Users\\kevin\\Desktop\\ChildSubject2.cer");

        byte[] rawdata = x509.RawData;
        Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine);
        Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine);
        Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine);
        Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, true), Environment.NewLine);
        Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine);
    //    Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(false), Environment.NewLine);  // cer里面并没有私钥信息
        Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(false), Environment.NewLine);
        Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine);
        Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine);


        //Output chain information of the selected certificate.
        X509Chain ch = new X509Chain();
        ch.Build(x509);
        Console.WriteLine("Chain Information");
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length);
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);
        //Output chain element information.
        Console.WriteLine("Chain Element Information");
        Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count);
        Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);

    //    int index = 0;
        foreach (X509ChainElement element in ch.ChainElements)
        {
            Console.WriteLine("Element subject name: {0}", element.Certificate.Subject);
            Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
            Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
            Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
            Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
            Console.WriteLine("Element information: {0}", element.Information);
            Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

            string a = element.Certificate.Thumbprint;
       //     string b = ch.ChainPolicy.ExtraStore[0].Thumbprint;
            //ch.ChainPolicy.ExtraStore[index - 1].Thumbprint;

            if (ch.ChainStatus.Length > 1)
            {
                for (int index = 0; index < element.ChainElementStatus.Length; index++)
                {
                    Console.WriteLine(element.ChainElementStatus[index].Status);
                    Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
                }
            }
        }

        x509.Reset();
        
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • QTreeView使用总结13,自定义model示例,大大优化性能和内存[通俗易懂]

    QTreeView使用总结13,自定义model示例,大大优化性能和内存[通俗易懂]1,简介前面简单介绍过Qt的模型/视图框架,提到了Qt预定义的几个model类型:QStringListModel:存储简单的字符串列表QStandardItemModel:可以用于树结构的存储,提供了层次数据QFileSystemModel:本地系统的文件和目录信息QSqlQueryModel、QSqlTableModel、QSqlRelati…

    2022年5月29日
    41
  • 5节锂电池升压充电管理芯片型号_锂电池充电管理ic

    5节锂电池升压充电管理芯片型号_锂电池充电管理ic5V升压充电21V五节锂电池升压充电管理芯片HU5911是一款工作于2.7V到6.5V的PFM升压型多节电池充电控制集成电路。HU5911采用恒流和准恒压模式(Quasi-CVTM)对电池进行充电管理,内部集成有基准电压源,电感电流检测单元,控制电路和片外场效应晶体管驱动电路等,具有外部元件少,电路简单等优点。当接通输入电源后,HU5911进入充电状态,控制片外N沟道MOSFET导通,电感电流上升,当上升到外部电流检测电阻设置的上限时,片外N沟道MOSFET截止,电感电流下降,电感中的能量转移到电池中

    2022年9月28日
    1
  • duststorm和sandstorm_Stormwind

    duststorm和sandstorm_Stormwindvirustracker·2016/03/0310:17www.cylance.com/hubfs/2015_…CylanceSPEAR发现了一起针对日本、韩国、美国、欧洲以及其他几个东南亚国家的威胁行动,在上述国家中,有大量的行业部门都遭到了攻击。0x00多样的权利形式我们研究发现DustStorm最早从2010年开始活动,使用了大量不同的作战技术,包括钓鱼、水坑攻击和0-day漏洞。…

    2022年10月14日
    3
  • 怎么做app软件_软件限制设备登录怎么激活成功教程

    怎么做app软件_软件限制设备登录怎么激活成功教程项目描述客户端,基于H5Plus使用MUI框架开发的APP,运行环境为小米手机真机测试。服务端,使用SpringBoot搭建的项目,运行环境为SpringBoot内置Tomcat,部署端口为8090。问题分析电脑和手机连接同一个WiFi,手机点击按钮,触发Ajax请求,无法访问在笔记本电脑上部署的SpringBoot后台。原Ajax请求地址,使用的是localhost,打开电脑cmd窗口,输入ipconfig查询电脑的ipv4地址,修改localhost为电脑私网IP。mui.ajax(“ht

    2025年9月22日
    6
  • ASEMI整流桥MB10F参数,MB10F特征,MB10F机械数据

    ASEMI整流桥MB10F参数,MB10F特征,MB10F机械数据编辑-ZASEMI整流桥MB10F参数:型号:MB10F最大重复峰值反向电压(VRRM):1000F最大有效值电压(VRMS):700V最大直流阻断电压(VDC):1000V最大平均正向输

    2022年7月2日
    70
  • PyPDF2模块[通俗易懂]

    PyPDF2模块[通俗易懂]1、PdfFileReader构造方法:PyPDF2.PdfFileReader(stream,strict=True,warndest=None,overwriteWarnings=True)stream:*File对象或支持与File对象类似的标准读取和查找方法的对象,也可以是表示PDF文件路径的字符串。*strict(bool):确定是否应该警告用户所用的…

    2022年6月23日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号