2016-11-07 1 views

답변

1

그 일을하는 신뢰할 수있는 방법이있다 아니다 당신이 "독립적 인 구현"에서 경계를 교차하기 때문에에 "의존 구현"을 - - 내보내기 기능은 인증서 나 키에 고유 한 것이 아니라 키 저장 방법에 대한 기능입니다.

는 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. 

하지만 궁극적으로 가장 간단한 방법은 실제로 시도하는 것입니다.

1

그것이 RSA 인증서가 있다면이 같은 것을 할 수있는 :

RSACryptoServiceProvider rsa = cert.PrivateKey; 
var isExportable = rsa.CspKeyContainerInfo.Exportable; 
관련 문제