I want to validate a certificate password. At this moment I have code based on handling CryptographicException and checking an exception message. But this approach is dependent on English culture info.
public bool VerifyPassword(byte[] fileContent, string password)
{
try
{
var certificate = new X509Certificate2(fileContent, password);
}
catch (CryptographicException ex)
{
if (ex.Message.StartsWith("The specified network password is not correct."))
{
return false;
}
throw;
}
return true;
}
I have been looking for other solution how to validate the certificate password, but without success.
What is correct approach how to validate the certificate password?
I would appreciate any idea...
After few months I found better solution (perhaps the best). It is based on HResult value of the CryptograhpicExcaption.
static bool VerifyPassword(byte[] fileContent, string password)
{
try
{
// ReSharper disable once UnusedVariable
var certificate = new X509Certificate2(fileContent, password);
}
catch (CryptographicException ex)
{
if ((ex.HResult & 0xFFFF) == 0x56)
{
return false;
};
throw;
}
return true;
}
All HResults (System error codes) documentation is available on: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx