X509 validating and creating Base64 strings from Certificates

Source: Internet
Author: User
Tags base64
The lantern in my hand is the enemy of the Dark Road before me . Program.cs Code:

class Program
     {
         static void Main (string [] args)
         {
             Console.WriteLine ("X509 Certificate Utility Program");
             Console.WriteLine ("--------------------------");
             Console.WriteLine ();
             Console.WriteLine ("Please enter the path of the certificate (.cer) file:");
             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 ("Done.");
         }
     }
ICertificateHelper.cs Code:

  Public interface Icertificatehelper
    {
        bool Verifycertificate (X509Certificate exportedcert);
        String Certificatebase64value (X509Certificate certificate);
    }
CertificateHelper.cs Code:
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);
        }

        // When saving the certificate as a file, we have three options:

        // Certificate with private key
        // Defined by Public Key Cryptography Standards # 12, PKCS # 12 standards, it contains the certificate format of the public and private keys in binary format, with pfx as the certificate file suffix name.

        // Binary encoded certificate
        // There is no private key in the certificate, the DER encoded binary format certificate file, with cer as the certificate file suffix name.

        // Base64 encoded certificate
        // There is no private key in the certificate. The certificate file in BASE64 encoding format also uses cer as the suffix of the certificate file.
        public string CertificateBase64Value (X509Certificate certificate)
        {
            // The certificate is exported to byte [], and the export method is used to export the container certificate.
            // The first parameter X509ContentType.Pfx indicates that it is to be exported as a pfx certificate with a private key, and the second parameter is the private key protection password.
            // If you want to export to a cer certificate without a private key, the first parameter uses X509ContentType.Cert, which means that if you export to a cer certificate without a private key, no password is required.
            byte [] export = certificate.Export (X509ContentType.Cert);
            return Convert.ToBase64String (export);
        }
    } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.