Exporting a Certificate as BASE-64 encoded .cer

前端 未结 4 691
青春惊慌失措
青春惊慌失措 2020-12-08 19:13

I am trying to export a cert without the private key as as BASE-64 encoded file, same as exporting it from windows. When exported from windows I am able to open the .cer fil

相关标签:
4条回答
  • 2020-12-08 19:19

    For those implementing something similar in .NET Core, here's the code, based on what tyranid did. Base64FormattingOptions.InsertLineBreaks doesn't exist in .NET Core, so I had to implement my own way to do line breaking.

        // Certificates content has 64 characters per lines
        private const int MaxCharactersPerLine = 64;
    
        /// <summary>
        /// Export a certificate to a PEM format string
        /// </summary>
        /// <param name="cert">The certificate to export</param>
        /// <returns>A PEM encoded string</returns>
        public static string ExportToPem(this X509Certificate2 cert)
        {
            var builder = new StringBuilder();
            var certContentBase64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
            // Calculates the max number of lines this certificate will take.
            var certMaxNbrLines = Math.Ceiling((double)certContentBase64.Length / MaxCharactersPerLine);
    
            builder.AppendLine("-----BEGIN CERTIFICATE-----");
            for (var index = 0; index < certMaxNbrLines; index++)
            {
                var maxSubstringLength = index * MaxCharactersPerLine + MaxCharactersPerLine > certContentBase64.Length
                    ? certContentBase64.Length - index * MaxCharactersPerLine
                    : MaxCharactersPerLine;
                builder.AppendLine(certContentBase64.Substring(index * MaxCharactersPerLine, maxSubstringLength));
            }
            builder.AppendLine("-----END CERTIFICATE-----");
    
            return builder.ToString();
        }
    
    0 讨论(0)
  • 2020-12-08 19:23

    //however, missing the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----"

    These missing lines are optional. CA may generate them or not depending on settings. For all practical reasons they can be removed from the Base64 encoded file.

    0 讨论(0)
  • 2020-12-08 19:25

    try this:

    X509Certificate2 cerifikata = new X509Certificate2("C://certificate.pfx");
    File.WriteAllBytes("D://Test.cer",cerifikata.Export(X509ContentType.Cert));
    
    0 讨论(0)
  • 2020-12-08 19:37

    Perhaps

    /// <summary>
    /// Export a certificate to a PEM format string
    /// </summary>
    /// <param name="cert">The certificate to export</param>
    /// <returns>A PEM encoded string</returns>
    public static string ExportToPEM(X509Certificate cert)
    {
        StringBuilder builder = new StringBuilder();            
    
        builder.AppendLine("-----BEGIN CERTIFICATE-----");
        builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
        builder.AppendLine("-----END CERTIFICATE-----");
    
        return builder.ToString();
    }
    
    0 讨论(0)
提交回复
热议问题