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);
}
}