글쎄, .NET은 CNG에 좋은 API를 가지고 있지 않습니다. API의 표면을 긁어 본다면, 특히 MS와 CNG가 전체 Windows 플랫폼에서 모든 Crypto API 중 가장 심각한 API라는 점을 고려하면 다소 우스꽝 스럽습니다.
따라서 C++ CNG API에 C# 인터페이스 (P/Invoke를 통해)를 제공하는 CLRSecurity을 사용해야합니다. 그것으로도 API 디자인이 가장 좋지 않습니다. 하지만 도움이됩니다.
// Load the cert, many ways, one implementation
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectName, "My cert subject name", true);
store.Close();
if (certs.Count > 0)
cert = certs[0];
else
return;
// Magic happens here! We load the private CngKey (if it exists)
// You need CLR Security for this, it manages the P/Invoke
// into the C++ api behind the scenes.
var pvtCngKey = cert.GetCngPrivateKey();
// Create the DiffieHellman helper
var ecDh = new ECDiffieHellmanCng(ourPvtEcCngKey)
{
KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
HashAlgorithm = CngAlgorithm.Sha256
};
ECDiffieHellmanCngPublicKey theirPubCngKey = LoadOtherPartiesCngPublicKey(theirCert);
byte[] symKey = ecDh.DeriveKeyMaterial(theirPubCngKey);
'LoadOtherPartiesCngPublicKey (theirCert)'의 구현은 어떻게 생겼습니까? –