위치/저장소에 인증서를 열거하는 간단한 C# DotNet 콘솔 앱이 있습니다. 그것은 다음과 같은 것들을 표시X.509 인증서를 내보낼 수 있는지 확인하는 방법은 무엇입니까?
- x509.SerialNumber
- x509.Issuer
- x509.Subject
- x509.HasPrivateKey
인증 여부를 알려줍니다 어떤 특성이 있는가 내보낼 수 있습니까? 나는 하나를 찾을 수 없었지만 나는 이것에 상당히 새로운 것이다.
위치/저장소에 인증서를 열거하는 간단한 C# DotNet 콘솔 앱이 있습니다. 그것은 다음과 같은 것들을 표시X.509 인증서를 내보낼 수 있는지 확인하는 방법은 무엇입니까?
인증 여부를 알려줍니다 어떤 특성이 있는가 내보낼 수 있습니까? 나는 하나를 찾을 수 없었지만 나는 이것에 상당히 새로운 것이다.
그 일을하는 신뢰할 수있는 방법이있다 아니다 당신이 "독립적 인 구현"에서 경계를 교차하기 때문에에 "의존 구현"을 - - 내보내기 기능은 인증서 나 키에 고유 한 것이 아니라 키 저장 방법에 대한 기능입니다.
는 Windows에서만 경우, 당신은 윈도우 XP 이상에있어이 상당히 안정적으로 작동합니다
try
{
ICspAsymmetricAlgorithm key = cert.PrivateKey;
if (key != null)
{
return key.CspKeyContainerInfo.Exportable;
}
}
catch (CryptographicException) {}
return whateverYouWantUnknownToBe;
그것을 인증서 곳을 얻을 수있어 당신이 비스타에있어 이상되면 HasPrivateKey
이 참이지만 PrivateKey
은 "잘못된 공급자 지정"예외를 throw하므로 .NET 4.6으로 이동해야합니다.
// AllowExport says whether it can be exported in a PFX (encrypted key)
// AllowPlainTextExport says whether it can be exported as (e.g.) an RSAParameters structure
const CngExportPolicies RequiredPolicies = CngExportPolicies.AllowExport;
RSACng rsaCng = cert.GetRSAPrivateKey() as RSACng;
if (rsaCng != null)
{
return (rsaCng.Key.ExportPolicy & RequiredPolicies) == RequiredPolicies;
}
// Requires 4.6.1
ECDsaCng ecdsaCng = cert.GetECDsaPrivateKey() as ECDsaCng;
if (ecdsaCng != null)
{
return (ecdsaCng.Key.ExportPolicy & RequiredPolicies) == RequiredPolicies;
}
// Requires 4.6.2
DSACng dsaCng = cert.GetDSAPrivateKey() as DSACng;
if (dsaCng != null)
{
return (dsaCng.Key.ExportPolicy & RequiredPolicies) == RequiredPolicies;
}
// previous code goes here.
하지만 궁극적으로 가장 간단한 방법은 실제로 시도하는 것입니다.
그것이 RSA 인증서가 있다면이 같은 것을 할 수있는 :
RSACryptoServiceProvider rsa = cert.PrivateKey;
var isExportable = rsa.CspKeyContainerInfo.Exportable;