一个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); // 根证书是自签名。所以能够通过。

非常easy,由于根证书是自签名的,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);
}
}
}

上面的代码也非常easy,事实上就是把整个证书链里面的每个证书打印信息一下。详细的函数调用參数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(); } }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

cer证书签名验证的更多相关文章

  1. JAVA调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据: 密钥实体( ...

  2. 安装.cer证书并将证书从.cer格式转化为.pem格式

    ## 安装.cer证书并将证书从.cer格式转化为.pem格式 ### 安装.cer证书到本地 打开*运行*窗口 输入MMC.exe, 单击*确定* 在打开的控制台1的窗口中. 选择*文件*, 选择* ...

  3. 下载https协议需要的cer证书

    一:https简介 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...

  4. android手机导入.cer证书文件的方法

    访问很多https协议的网站需要安装证书,手机也可以导入cer文件,你知道么?本文将通过简单的两步告诉你手机安装cer文件的方法. 步骤一:请先将数字证书文件“******.cer”文件复制到SD卡的 ...

  5. java 调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据:密钥实体(K ...

  6. .pfx和.Cer 证书

    通常情况下,作为文件形式存在的证书一般有三种格式: 第一种:带有私钥的证书,由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进 ...

  7. (苹果AppleWWDRCA.cer证书过期)Failed to locate or generate matching signing assets

    从2月14号开始,上传AppStore会碰到:Failed to locate or generate matching signing assets 字数462 阅读13571 评论16 喜欢61 ...

  8. 从自签名证书导出pfx和cer证书

    完整代码: public sealed class DataCertificate { #region 生成证书 /// <summary> /// 根据指定的证书名和makecert全路 ...

  9. 将.cer证书导入java密钥库?

    导入.cer从浏览器下载的证书文件(打开网址并挖掘详细信息)到cacerts keystore中java_home\jre\lib\security为我工作,而不是尝试生成和使用我自己的密钥库. 去你 ...

随机推荐

  1. SICP 锻炼 (2.15)解决摘要:深入思考间隔误差

    SICP 2.15 是接着 题目 2.14 的, 题目 2.14中提到了Alyssa设计的区间计算模块在并联电阻计算时会出现故障,这个问题是Lem发现的. 接着,一个叫Eva的人也发现了这个问题.同一 ...

  2. 北邮iptv用WindowsMediaplayer打不开的解决的方法

    前言:之前我的iptv能够用,可是有次我安装了realplayer,它就偷偷把iptv文件的默认打开方式给篡改了,卸载了                  realplayer之后,iptv不能直接用 ...

  3. ISAPI_Rewrite 3 伪静态软件

    下载地址 http://www.helicontech.com/isapi_rewrite/download.html

  4. hdu3572 任务分配/最大流量推论全流

    意甲冠军:将n分配的任务m机.到的每个任务需要的天数(如果没有持续的日常),并能做到在哪些天任务.询问是否有计划. 典型的任务(X)----日(Y)一半的最大流量,(因为这个任务是天之间的关系)处理器 ...

  5. 文件搜索神器everything 你不知道的技巧总结

    everything这个软件用了很久,总结了一些大家可能没注意到的技巧,分享给大家 1.指定文件目录搜索示例: TDDOWNLOAD\ abc        在所有TDDOWNLOAD文件夹下搜索包含 ...

  6. c# winform panel 流式布局 panel块可自动排列

    代码下载地址  http://download.csdn.net/detail/simadi/7677053

  7. Windows 8本地化多语言支持

    原文:Windows 8本地化多语言支持 在Win8平台处理本地化多语言的支持相对比较容易的,但比WP8稍微复杂一点,并不像WP8平台那样大部分工作都有VS IDE处理,Win8平台的操作基本需要开发 ...

  8. 分散式-ubuntu12.04安装spark-1.0.0

    1. 安装文档: http://spark.apache.org/docs/latest/spark-standalone.html 2. web UI: master's web UI, which ...

  9. SIGPIPE并产生一个信号处理

    阅读TCP某物,知道server并关闭sockfd当写两次,会产生SIGPIPE信号,假如不治疗,默认将挂起server 弄个小样本试验: #include <unistd.h> #inc ...

  10. Android供TextView添加多个点击文字

    我们使用社会性软件的过程中会或多或少像别人的帖子点,图. : 能够看到用户页面显示出来的仅仅是点了赞的用户的名称,点击这些名称能够进入到该用户的主页.我们就来实现相似的效果.直接上代码吧. @Over ...