我需要获取计算机中的发出证书的主体的名称以及发出证书的主体的名称. 
微软提供的getname()方法和GetIssuerName()方法现在都已经过时了,有没有谁知道和这个差不多的方法?或者类似方法?

解决方案 »

  1.   

    大多数情况下,您应该改用 X509Certificate2 类。
      

  2.   

    下面的代码示例创建一个命令行可执行文件,该可执行文件以一个证书文件作为参数,并将各种证书属性输出到控制台。using System;
    using System.Security.Cryptography;
    using System.Security.Permissions;
    using System.IO;
    using System.Security.Cryptography.X509Certificates;
    public class CertInfo
    {
    //Reads a file.
    internal static byte[] ReadFile (string fileName)
    {
    FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    int size = (int)f.Length;
    byte[] data = new byte[size];
    size = f.Read(data, 0, size);
    f.Close();
    return data;
    }
    //Main method begins here.
    public static void Main(string[] args)
    {
    //Test for correct number of arguments.
    if (args.Length < 1)
    {
    Console.WriteLine("Usage: CertInfo <filename>");
    return;
    }
    try
    {
    X509Certificate2 x509 = new X509Certificate2();
    //Create X509Certificate2 object from .cer file.
    byte[] rawData = ReadFile(args[0]);
    x509.Import(rawData); //Print to console information contained in the certificate.
    Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine,x509.Subject);
    Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine,x509.Issuer);
    Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine,x509.Version);
    Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine,x509.NotBefore);
    Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine,x509.NotAfter);
    Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine,x509.Thumbprint);
    Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine,x509.SerialNumber);
    Console.WriteLine("{0}Friendly Name: {1}{0}",  Environment.NewLine,x509.PublicKey.Oid.FriendlyName);
    Console.WriteLine("{0}Public Key Format: {1}{0}",  Environment.NewLine,x509.PublicKey.EncodedKeyValue.Format(true));
    Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine,x509.RawData.Length);
    Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine,x509.ToString(true)); Console.WriteLine("{0}Certificate to XML String: {1}{0}",  Environment.NewLine,x509.PublicKey.Key.ToXmlString(false)); //Add the certificate to a X509Store.
    X509Store store = new X509Store();
    store.Open(OpenFlags.MaxAllowed);
    store.Add(x509);
    store.Close();
    } catch (DirectoryNotFoundException)
    {
       Console.WriteLine("Error: The directory specified could not be found.");
    }
    catch (IOException)
    {
    Console.WriteLine("Error: A file in the directory could not be accessed.");
    }
    catch (NullReferenceException)
    {
    Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
    }
    }}
      

  3.   

    X509Certificate2类,我试了,但是没有能达到预期目标的方法
      

  4.   

    1.从磁盘上的证书文件中读取证书数据   unsigned char* pbX509Data; // 证书数据   unsigned long ulX509DataLen; // 证书数据长度   2.获取CertContext   PCCERT_CONTEXT pCertContext = CertCreateCertificateContext(X509_ASN_ENCODING, pbX509Data, ulX509DataLen);   3.获取证书信息   pCertContext->pCertInfo->dwVersion; // 证书版本号   CRYPT_INTEGER_BLOB snBlob = pCertContext->pCertInfo->SerialNumber; // 证书SN   CERT_NAME_BLOB issuerBlob = pCertContext->pCertInfo->Issuer; // 证书颁发者   CERT_NAME_BLOB subjectBlob = pCertContext->pCertInfo->Subject; // 证书主题   // 证书有效起始日期   SYSTEMTIME sysTime;   memset(&sysTime, 0, sizeof(sysTime));   FileTimeToSystemTime(&pCertContext->pCertInfo->NotBefore, &sysTime);   char szTime[128] = {0};   sprintf_s(szTime, 128, "%d年%d月%d日 %d:%d:%d", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);   // 证书有效终止日期   memset(&sysTime, 0, sizeof(sysTime));   FileTimeToSystemTime(&pCertContext->pCertInfo->NotAfter, &sysTime);   memset(szTime, 0, sizeof(szTime));   sprintf_s(szTime, 128, "%d年%d月%d日 %d:%d:%d", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);   4.创建临时密钥容器   HCRYPTPROV hTmpProv = NULL;   CryptAcquireContext(&hTmpProv, "My_Temporary_Container", NULL, PROV_RSA_AES, 0); // NULL表示使用系统默认CSP   5.向容器中导入公钥,获取公钥句柄   HCRYPTKEY hKey = NULL;   CERT_PUBLIC_KEY_INFO certPubKeyInfo = pCertContext->pCertInfo->SubjectPublicKeyInfo;   CryptImportPublicKeyInfo(hTmpProv, X509_ASN_ENCODING|PKCS_7_ASN_ENCODING, &certPubKeyInfo, &hKey);   6.导出公钥(最好采用二次调用方式)   unsigned char* pBuf = NULL;   unsigned long ulBufLen = 0;   CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, pBuf, &ulBufLen);   pBuf = new unsigned char[ulBufLen];   memset(pBuf, 0, ulBufLen);   CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, pBuf, &ulBufLen);   7.获取公钥信息   unsigned char* p = pBuf + sizeof(PUBLICKEYSTRUC);   (*(RSAPUBKEY*)p).bitlen; // 公钥模长(以bit为单位)   (*(RSAPUBKEY*)p).pubexp; // 公钥的e(注意字节顺序)   p += sizeof(RSAPUBKEY); // 公钥的n(注意字节顺序)   8.清理工作   delete[] pBuf;   pBuf = NULL;   CryptDestroyKey(hKey);   CryptReleaseContext(hTmpProv, 0);   CertFreeCertificateContext(pCertContext);
      

  5.   

    现在已有数字证书,如何用.net语言获得X509数字证书中的信息。