I'm reading a certificate from a X509Store, that I want to get it's CngKey using GetCngPrivateKey. But all I'm getting is the following error.
at Security.Cryptography.X509Certificates.X509Native.AcquireCngPrivateKey(SafeCertContextHandle certificateContext)\r\n at Security.Cryptography.X509Certificates.X509Certificate2ExtensionMethods.GetCngPrivateKey(X509Certificate2 certificate)\r\n
The certificate is passing the validation HasCngKey, but I'm not able to get its value. Is there any other way to get the CngKey?
byte[] digest = null;
using (var hmac = SHA256.Create())
{
digest = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data));
}
var certificate = collection[0];
byte[] firmadigital = null;
CngKey privateKey = null;
AsymmetricAlgorithm privateKey1 = null;
var hasCng = certificate.HasCngKey();
if (hasCng)
{
privateKey = certificate.GetCngPrivateKey();
Console.WriteLine("tiene CNG");
var key = new Security.Cryptography.RSACng(privateKey);
key.SignatureHashAlgorithm = CngAlgorithm.Sha1;
var p = key.SignData(digest);
return Convert.ToBase64String(p);
}
I'm using c# net core 2.0
Thanks