internal override NCryptKeyOrCryptProviderSafeHandle OpenExisting(string keyName, out KeySpec keySpec) { keySpec = KeySpec.NONE; NCryptKeyOrCryptProviderSafeHandle keyHandle; var result = NCrypt.NCryptOpenKey(_storageProvider.Handle, out keyHandle, keyName, KeySpec.NONE, 0u); if (result == SECURITY_STATUS.ERROR_SUCCESS) { return(keyHandle); } if (result == SECURITY_STATUS.NTE_BAD_KEYSET) { return(null); } throw new InvalidOperationException("Failed to open key."); }
void acquirePrivateKeyFromKeyBuilder() { Int32 hresult = NCrypt.NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProv, _keyInfo.ProviderName, 0); if (hresult != 0) { openLegacyPrivateKey(); return; } UInt32 dwFlags = 0; if (_keyInfo.MachineContext) { dwFlags = nCrypt2.NCRYPT_MACHINE_KEY_FLAG; } hresult = NCrypt.NCryptOpenKey(phProv, out phPrivKey, _keyInfo.KeyContainerName, (UInt32)_keyInfo.KeySpec, dwFlags); if (hresult != 0) { throw new CryptographicException(hresult); } isCng = true; }