Help and Support
 

powered byLive Search

PRB: X509Certificate 支持仅 DER 编码证书

Retired KB Article本文介绍那些 Microsoft 不再提供支持的产品。因此本文按“原样”提供,并且不再更新。
文章编号:318217
最后修改:2003年10月20日
修订:4.0
本文引用下面的 Microsoft .NET 框架类库名称空间:
? System.IO
? System.Security.Cryptography.X509Certificates
? System.Text

症状

当您使用 System.Security.Cryptography.X509Certificates.X509Certificate 类时,您可能会收到以下错误信息:
输入的数据不能被编码为一个有效的证书。
this problem occurs if one of following conditions is true:
?您使用 X509Certificate 构造函数并传递的从 Base 64 编码 X.509 (.cer) 文件中读取到 X509Certificate 构造函数的字节数组。 -or-

?则使用 X509Certificate.CreateFromCertFile 方法,并将路径中的传递给一个 Base 64 编码 X.509 (.cer) 文件。

回到顶端

原因

发生此问题的原因, X509Certificate 类仅支持编码的二进制 X.509 (.cer) 证书中的可分辨编码规则 (DER)。

回到顶端

解决方案

if certificate is encoded Base64-,follow these steps to resolve this problem:
1.从证书数据中删除以下字符串:
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
						
2.解码 Base 64 证书数据。 下面的 Visual C# 示例代码例如,解码 Base 64 证书数据:
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace ReadBase64Cert
{
    public class ReadBase64Cert
    {
        public ReadBase64Cert()
        {
        }
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: Base64EncodedFile (.cer)\n");
                return;
            }

            // args[0] - Base64Encoded .cer file

            // Open the certificate, and read it into a byte array.
            FileStream certFile = new FileStream(args[0],
                FileMode.Open,
                FileAccess.Read);
            int size = (int)certFile.Length;
            byte[] certBytes = new byte[size];
            size = certFile.Read(certBytes, 0, size);
            certFile.Close();
    
            // Remove the unnecessary characters.
            String certString = Encoding.ASCII.GetString(certBytes);
            StringBuilder sb = new StringBuilder(certString);
            sb.Replace("-----BEGIN CERTIFICATE-----", "");
            sb.Replace("-----END CERTIFICATE-----", "");
    
            // Decode the bytes from base64 to raw bytes.
            certBytes = Convert.FromBase64String(sb.ToString());
            X509Certificate cert = new X509Certificate(certBytes);
            Console.WriteLine(cert.GetName()); 
        }
    }
}
					

回到顶端

状态

此行为是设计使然。

回到顶端


这篇文章中的信息适用于:
?Microsoft .NET Framework Class Libraries 1.0
?Microsoft .NET Framework Class Libraries 1.1

回到顶端

关键字:?
kbmt kbcrypt kbkernbase kbprb kbsecurity KB318217 KbMtzh

回到顶端

机器翻译注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请完成文章末尾的在线调查。
点击这里察看该文章的英文版: 318217 (http://support.microsoft.com/kb/318217/en-us/)

回到顶端

Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。

文章翻译

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.