我正在打开存储在HSM中的加密密钥。我的机器上安装了HSM的客户端库。如果我运行以下Windows Powershell代码段:
##Open an existing Key
$keyName = 'foo'
$providername = 'SafeNetProtectApp Key Storage Provider'
$provider = New-Object 'System.Security.Cryptography.CngProvider' -ArgumentList $providername
$key = [System.Security.Cryptography.CngKey]::Open($keyName, $provider)
有效。
如果我尝试编译以下C#代码段:
using System.Security.Cryptography;
namespace SaveSecret
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(">>> Starting application <<<");
string keyName = "foo";
string providerName = "SafeNetProtectApp Key Storage Provider";
CngProvider provider = new CngProvider(providerName);
CngKey key = null;
key = CngKey.Open(keyName, provider);
System.Console.Write("[Hit Enter to Continue]");
System.Console.ReadLine();
}
}
}
我收到以下异常:
System.Security.Cryptography.CryptographicException
HResult=0x8009001E
Message=Provider DLL could not be found.
Source=System.Core
StackTrace:
at System.Security.Cryptography.NCryptNative.OpenStorageProvider(String providerName)
at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider, CngKeyOpenOptions openOptions)
at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider)
我很困惑为什么我可以在Powershell中使用相同的库来打开密钥而不会出现问题。在同一台计算机上,如果我尝试使用C#进行操作,则会出现异常。谁能解释为什么会这样?
其他事实...