我手中的灯笼 使眼前黑暗的路途与我为敌
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 string CertificateBase64Value(X509Certificate certificate)
{
byte[] export = certificate.Export(X509ContentType.Cert);
return Convert.ToBase64String(export);
}
}