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);
        }
    }
相关文章
|
8月前
|
数据安全/隐私保护
Base64文本加密
Base64文本加密
|
数据安全/隐私保护
批量注册图片,可以根据需要修改data,base64编码
批量注册图片,可以根据需要修改data,base64编码
94 0
批量注册图片,可以根据需要修改data,base64编码
|
数据安全/隐私保护
MD5 加密解密 判断密码是否相等 全套实现方式
MD5 加密解密 判断密码是否相等 全套实现方式
253 0
|
Java Android开发
Android/Java中解析.crt证书文件的公钥(public key)---Android拓展篇
Android/Java中解析.crt证书文件的公钥(public key)---Android拓展篇
1371 0
|
数据安全/隐私保护
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
867 0
【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
|
算法 安全 数据安全/隐私保护
Base64 加密处理|学习笔记
快速学习 Base64 加密处理
217 0
|
算法 Java 数据安全/隐私保护
Base64 加密与解密|学习笔记
快速学习 Base64 加密与解密
247 0
|
安全
通过MD5校验游戏安装文件完整性实例演示,MD5校验工具Hash使用演示
通过MD5校验游戏安装文件完整性实例演示,MD5校验工具Hash使用演示
751 0
通过MD5校验游戏安装文件完整性实例演示,MD5校验工具Hash使用演示
|
存储 程序员 区块链
如何使用Web3j生成私钥和地址,而不只是创建密钥存储JSON文件?
一个我提供的方法,通过将结果privatekey导入到MetaMask中并获得与预期相同的地址来验证: private static JSONObject process(String seed){ ...
3629 0
|
Python 数据格式 JSON
文件完整性hash验证demo(python脚本)
一个简单的文件完整性hash验证脚本 #!/usr/bin/env python # -*- coding: utf-8 -*- import os import hashlib import json #网站目录所有文件列表 path_list=[] #静态文件可以不做hash效验 White_list=['.
1332 0