X509从证书验证和创建Base64字符串

简介:

我手中的灯笼 使眼前黑暗的路途与我为敌


Program.cs代码:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("X509证书实用程序");
            Console.WriteLine("--------------------------");
            Console.WriteLine();
            Console.WriteLine("请输入证书(.cer)文件的路径: ");
            string location = Console.ReadLine();
            if (location != null && File.Exists(location))
            {
                ICertificateHelper certHelper = new CertificateHelper();
                X509Certificate2 certificate = new X509Certificate2(location);

                Console.WriteLine("Encoded Base64 Value for Cert: ");
                Console.WriteLine(certHelper.CertificateBase64Value(certificate));

                Console.WriteLine(certHelper.VerifyCertificate(certificate));

                ConsoleKeyInfo key = Console.ReadKey();
            }

            Console.WriteLine("完成.");
        }
    }

ICertificateHelper.cs代码:

  public interface ICertificateHelper
    {
        bool VerifyCertificate(X509Certificate exportedCert);
        string CertificateBase64Value(X509Certificate certificate);
    }

CertificateHelper.cs代码:

 public class CertificateHelper : ICertificateHelper
    {
        public bool VerifyCertificate(X509Certificate exportedCert)
        {
            string base64 = CertificateBase64Value(exportedCert);
            X509Certificate2 importCert = GetCertificateFromBase64(base64);
            return String.Equals(exportedCert.Subject, importCert.Subject);
        }

        private static X509Certificate2 GetCertificateFromBase64(string base64)
        {
            byte[] import = Encoding.Default.GetBytes(base64);
            return new X509Certificate2(import);
        }

        // 将证书保存为文件时,我们有三种选择:

        //带有私钥的证书
        //由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形式,以pfx作为证书文件后缀名。

        //二进制编码的证书
        //证书中没有私钥,DER 编码二进制格式的证书文件,以cer作为证书文件后缀名。

        //Base64编码的证书
        //证书中没有私钥,BASE64 编码格式的证书文件,也是以cer作为证书文件后缀名。
        public string CertificateBase64Value(X509Certificate certificate)
        {
            //证书导出到byte[]中,导出容器证书使用的是Export方法。
            //第一个参数X509ContentType.Pfx表示要导出为含有私钥的pfx证书形式,第二个参数为私钥保护密码。
            //如果要导出为不含私钥的cer证书,第一个参数使用X509ContentType.Cert,表示导出为不含私钥的cer证书,也就不需要密码了。
            byte[] export = certificate.Export(X509ContentType.Cert);
            return Convert.ToBase64String(export);
        }
    }
相关文章
|
Android开发 iOS开发 MacOS
APP备案公钥、证书MD5指纹/签名MD5值获取最简单方法
APP备案公钥、证书MD5指纹/签名MD5值获取方法,Android安卓平台、Windows平台、macOS平台,三个平台获取方法, Android平台使用 APP备案助手,各大安卓应用市场搜索 APP备案助手 即可,Windows/macOS平台使用jadx-gui工具。
3848 2
|
6月前
|
数据安全/隐私保护
【Azure 环境】向Azure Key Vault中导入证书有输入密码,那么导出pfx证书的时候,为什么没有密码呢?
【Azure 环境】向Azure Key Vault中导入证书有输入密码,那么导出pfx证书的时候,为什么没有密码呢?
|
JavaScript 前端开发
🎖️typeScrpt中如何从验证字符串?
模板文字类型本质上是一种字符串类型。通过定义字符串必须匹配的模式,这些类型提供了一种验证和推断数据的方式。它们是大约三年前在 TypeScript 4.1 中引入的。根据最初的 GitHub PR,以下示例演示了 TypeScript 利用模板文字类型获得的多功能特性。
147 0
|
存储 算法 安全
浅析MD5及其用途
简介 MD5(Message-Digest Algorithm,对应的中文名为消息摘要算法)是计算机安全领域广泛使用的散列函数(又称哈希算法、摘要算法),可以产生出一个128位(16字节)的散列值(hash value),主要用来确保信息(message)传输完整和一致。常见的应用场景有密码保护、下载文件校验等。
|
数据安全/隐私保护
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
992 0
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
|
数据安全/隐私保护
注册与登录中相关字段格式的正则表达式验证
注册与登录中相关字段格式的正则表达式验证
123 0
|
Web App开发
如何把密钥改成pem格式教程
说明:   这里以支付宝公钥为例,进行演示其他密钥也是一样的操作方式   如何生成RSA2密钥:https://openclub.alipay.com/read.php?tid=2177&fid=46   我们拿到的支付宝公钥都是字符串形式的(如下图)   pem密钥模板:pem模板密钥.zip 第一步:找到一个pem格式商户公钥。
1774 0
RSA2私钥填写错误报错集合
说明:   正确的填写是:appid+匹配的私钥+对应签名类型        正确唤起对应页面    测试appid私钥填写不同的值报错表现   1.填写和appid不匹配的私钥报错,应该检查自己项目中的私钥和上传的商户公钥是否匹配     1)报错信息     错误代码 invalid-si...
3709 0
|
存储 程序员 区块链
如何使用Web3j生成私钥和地址,而不只是创建密钥存储JSON文件?
一个我提供的方法,通过将结果privatekey导入到MetaMask中并获得与预期相同的地址来验证: private static JSONObject process(String seed){ ...
3791 0