我正在唱一张智能卡和一个令牌,我有一个代码可以检查他们的PIN码,但是我想知道如果密码不正确,直到被阻止的PIN码数量。
这是我的代码:
public static bool IsValidPasswordForCertificate(X509Certificate2 certificate, string password)
{
CspParameters csp = new CspParameters();
try
{
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)certificate.PrivateKey;
csp.KeyContainerName = rsaEncryptor.CspKeyContainerInfo.KeyContainerName;
csp.ProviderName = rsaEncryptor.CspKeyContainerInfo.ProviderName;
csp.ProviderType = rsaEncryptor.CspKeyContainerInfo.ProviderType;
}
catch (Exception)
{
throw;
}
//Import the password to the csp details
try
{
SecureString secure = new SecureString();
foreach (char c in password)
secure.AppendChar(c);
csp.KeyPassword = secure;
}
catch (Exception)
{
throw;
}
//Try to import the csp and password details to the provider
try
{
RSACryptoServiceProvider prov = new RSACryptoServiceProvider(csp);
}
catch (CryptographicException ex)
{
if (ex.Message.Contains("wrong PIN"))
return false;
}
catch (Exception)
{
throw;
}
return true;
}
如果引脚不正确,将抛出CryptographicException,并在此之后或之后,知道在智能卡/令牌阻止之前剩下的PIN重试次数。