5

我需要SignerInfoAuthenticodeASN1 结构中的数字签名 PE 文件中提取。

INFO: 一个 PE 文件在Security Directory RVAinside指定的偏移处包含验证码Optional Header Data Directories在阅读了Microsoft Authenticode PE Signature Format提供的文档后,我尝试开始,但运气不好,因为我对 SSL/TSL 很陌生。

我的问题:

  1. 有没有办法解析二进制文件并以 C 字符串格式打印数据结构?

  2. 有什么方法可以解析给定的二进制文件并指向SignerInfoor SignerName

NOTE:我不想使用任何平台相关的 API,因为我希望代码独立于平台。

在此先感谢所有大师:-)

UPDATE:我在 C# 中找到了一个代码。有人能帮我找到相同的C等价物吗?

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;

public class CertInfo
{
 public static void Main(String[] args)
 {
    byte[] certBytes;
    X509Certificate x509cert;

    while (true)
    {
        Console.WriteLine("\nEnter File Name: ");
        String filename = Console.ReadLine();
        if (filename == "")  //exit while(true) loop
            break;
        if (!File.Exists(filename))
        {
            Console.WriteLine("File \"{0}\" does not exist!\n", filename);
            continue;
        }

        try
        {             //try binary DER format first
            x509cert = X509Certificate.CreateFromCertFile(filename);
            showCertInfo(x509cert);
        }

        catch (System.Security.Cryptography.CryptographicException cryptder)
        { //not binary DER
            StreamReader sr = File.OpenText(filename);
            String filestr = sr.ReadToEnd();
            sr.Close();
            StringBuilder sb = new StringBuilder(filestr);
            sb.Replace("-----BEGIN CERTIFICATE-----", "");
            sb.Replace("-----END CERTIFICATE-----", "");
            //Decode 
            try
            {        //see if the file is a valid Base64 encoded cert
                certBytes = Convert.FromBase64String(sb.ToString());
                x509cert = new X509Certificate(certBytes);
                showCertInfo(x509cert);
            }
            catch (System.FormatException formexc)
            {
                Console.WriteLine("Not valid binary DER or Base64 X509 certificate format");
            }
            catch (System.Security.Cryptography.CryptographicException cryptb64)
            {
                Console.WriteLine("Not valid binary DER or Base64 X509 certificate format");
            }
        }


    } // end while true
}

private static void showCertInfo(X509Certificate x509cert)
{
    Console.WriteLine("Name: " + x509cert.GetName());
    Console.WriteLine("Issuer: " + x509cert.GetIssuerName());
    Console.WriteLine("Serial Number: " + x509cert.GetSerialNumberString());
    Console.WriteLine("Expiration Date: " + x509cert.GetExpirationDateString());
    Console.WriteLine("PublicKey: " + x509cert.GetPublicKeyString());
}
}
4

0 回答 0