Dim cp As New CspParameters()
cp.KeyContainerName = ContainerName
cp.Flags = CspProviderFlags.UseMachineKeyStore
如何确保如果与容器名称的键不存在新的密钥不是创造出来的?
Dim cp As New CspParameters()
cp.KeyContainerName = ContainerName
cp.Flags = CspProviderFlags.UseMachineKeyStore
如何确保如果与容器名称的键不存在新的密钥不是创造出来的?
您可以在加密提供商使用
.PersistKeyInCsp = false
,这将确保密钥没有得到在容器中遗留下来的。你是这个意思吗?
下面是我们用来测试对于给定的容器名称PowerShell脚本:
# Test if an rsa key container exists on this system.
function Test-RsaKeyContainerName(
[Parameter(Mandatory=$true)][string] $ContainerName,
[Parameter(Mandatory=$false)][switch] $UserContainer = $false
) {
$csp = New-Object -TypeName "System.Security.Cryptography.CspParameters";
$csp.KeyContainerName = $ContainerName;
if (!($UserContainer)) {
$csp.Flags = [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore;
}
$csp.Flags = $csp.Flags -bor [System.Security.Cryptography.CspProviderFlags]::UseExistingKey;
try {
$rsa = New-Object -TypeName "System.Security.Cryptography.RSACryptoServiceProvider" -ArgumentList ($csp);
} catch [System.Management.Automation.MethodInvocationException] {
if ($error[0].Exception.InnerException -ne $null -and
$error[0].Exception.InnerException.GetType() -eq [System.Security.Cryptography.CryptographicException] -and
$error[0].Exception.InnerException.Message.StartsWith("Keyset does not exist")) {
return $false;
} else {
throw;
}
}
return $true;
}
如果你确实需要枚举安装在系统上的按键,你可以在http://www.jensign.com/KeyPal/index.html
因为jensign页面不见了,你可以用repo链接更新吗? – 2018-03-05 00:47:08
试试这个:
public static bool DoesKeyExists(string containerName)
{
var cspParams = new CspParameters
{
Flags = CspProviderFlags.UseExistingKey,
KeyContainerName = containerName
};
try
{
var provider = new RSACryptoServiceProvider(cspParams);
}
catch (Exception e)
{
return false;
}
return true;
}
不是一个好主意,如果在过程开始之前存在密钥,它将被删除。 – 2014-10-29 12:53:33