문제

나는 이것이 간단 할 것이라고 생각했지만 분명히 그렇지 않다. 개인 키가있는 인증서가 설치되어 있으며 공개 키와 함께 프로그래밍 방식으로 수출하고 싶습니다. 다시 말해서, 나는 certmgr을 통해 내보내고 .cer로 내보낼 때 "개인 키를 내보내지 말아라"를 선택하는 것과 동등한 결과를 원합니다.

모든 x509Certificate2.Export 메소드는 PKCS #12로 존재하면 개인 키를 내보내는 것으로 보입니다. 이는 내가 원하는 것과 반대입니다.

이를 달성하기 위해 C#을 사용하는 방법이 있습니까, 아니면 Capicom에 파기를 시작해야합니까?

도움이 되었습니까?

해결책

이것을 우연히 발견 한 다른 사람을 위해, 나는 그것을 알아 냈습니다. 지정하는 경우 X509ContentType.Cert 첫 번째 (그리고 유일한) 매개 변수로 X509Certificate.Export, 그것은 공개 키만 내보내는 것만 내보러집니다. 반면에, 지정 X509ContentType.Pfx 개인 키가 존재하는 경우 개인 키를 포함합니다.

지난주에 다른 행동을보고 있다고 맹세했지만 테스트 할 때 이미 개인 키를 설치해야했습니다. 오늘 해당 인증서를 삭제하고 처음부터 다시 시작했을 때 내보내는 인증서에는 개인 키가 없다는 것을 알았습니다.

다른 팁

나는 다음과 같은 프로그램이 RawData 인증서의 속성에는 공개 키 만 포함되어 있으며 (MSDN에는 명확하지 않음) X509ContentType.Cert vs. X509ContentType.Pfx 예상대로 작동합니다.

using System;
using System.Linq;
using System.IdentityModel.Tokens;
using System.Security.Cryptography.X509Certificates;

class Program
{
    static void Main( string[] args )
    {
        var certPath = @"C:\blah\somecert.pfx";
        var certPassword = "somepassword";

        var orig = new X509Certificate2( certPath, certPassword, X509KeyStorageFlags.Exportable );
        Console.WriteLine( "Orig   : RawData.Length = {0}, HasPrivateKey = {1}", orig.RawData.Length, orig.HasPrivateKey );

        var certBytes = orig.Export( X509ContentType.Cert );
        var certA = new X509Certificate2( certBytes );
        Console.WriteLine( "cert A : RawData.Length = {0}, HasPrivateKey = {1}, certBytes.Length = {2}", certA.RawData.Length, certA.HasPrivateKey, certBytes.Length );

        // NOTE that this the only place the byte count differs from the others
        certBytes = orig.Export( X509ContentType.Pfx );
        var certB = new X509Certificate2( certBytes );
        Console.WriteLine( "cert B : RawData.Length = {0}, HasPrivateKey = {1}, certBytes.Length = {2}", certB.RawData.Length, certB.HasPrivateKey, certBytes.Length );

        var keyIdentifier = ( new X509SecurityToken( orig ) ).CreateKeyIdentifierClause<X509RawDataKeyIdentifierClause>();
        certBytes = keyIdentifier.GetX509RawData();
        var certC = new X509Certificate2( certBytes );
        Console.WriteLine( "cert C : RawData.Length = {0}, HasPrivateKey = {1}, certBytes.Length = {2}", certC.RawData.Length, certC.HasPrivateKey, certBytes.Length );

        Console.WriteLine( "RawData equals original RawData: {0}", certC.RawData.SequenceEqual( orig.RawData ) );

        Console.ReadLine();
    }
}

다음을 출력합니다.

Orig   : RawData.Length = 1337, HasPrivateKey = True
cert A : RawData.Length = 1337, HasPrivateKey = False, certBytes.Length = 1337
cert B : RawData.Length = 1337, HasPrivateKey = True, certBytes.Length = 3187
cert C : RawData.Length = 1337, HasPrivateKey = False, certBytes.Length = 1337
RawData equals original RawData: True

있습니다 OpenSSL .NET 래퍼 유용하다고 생각할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top