3
I am trying to pass the PIN through the code, so that the user does not need to enter it always, it is returning me this error:
Invalidcastexception: Unable to cast Object of type 'System.Security.Cryptography.Rsacng'
This is the function I’m using:
public static RSACryptoServiceProvider LerDispositivo(RSACryptoServiceProvider key, string PIN)
{
CspParameters csp = new CspParameters(key.CspKeyContainerInfo.ProviderType, key.CspKeyContainerInfo.ProviderName);
SecureString ss = new SecureString();
foreach (char a in PIN)
{
ss.AppendChar(a);
}
csp.ProviderName = key.CspKeyContainerInfo.ProviderName;
csp.ProviderType = key.CspKeyContainerInfo.ProviderType;
csp.KeyNumber = key.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2;
csp.KeyContainerName = key.CspKeyContainerInfo.KeyContainerName;
csp.KeyPassword = ss;
csp.Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseDefaultKeyContainer;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
return rsa;
}
And I added those three lines to my signing job:
RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
Key = (System.Security.Cryptography.RSACryptoServiceProvider)x509Cert.PrivateKey;
signedXml.SigningKey = x509Cert.PrivateKey;
signedXml.SigningKey = LerDispositivo(Key, "senhaaqui");
Error occurs on this line:
Key = (System.Security.Cryptography.RSACryptoServiceProvider)x509Cert.PrivateKey;
EDIT I have seen several examples on the Internet, but none of them solved my problem. I managed to find this link, that has an explanation, but I can not adapt to the code.
Following the LINK above, I tried to do this way:
RSACryptoServiceProvider publicKeyProvider = (System.Security.Cryptography.RSACryptoServiceProvider)x509Cert.GetRSAPrivateKey();
signedXml.KeyInfo = keyInfo;
signedXml.SigningKey = LerDispositivo(publicKeyProvider, "senhaaqui");
and it returns the same error:
Invalidcastexception: Unable to cast Object of type 'System.Security.Cryptography.Rsacng' to type 'System.Security.Cryptography.Rsacryptoserviceprovider'.
Is there any way to convert Rsacryptoserviceprovider to Rsacng ? Every way I try returns the same error.
EDIT
According to Pedro’s answer, I made the changes, but even so without success, it returns the following error:
error CS0433: Type "Cngpropertyoptions" exists in "System.Security.Cryptography.Algorithms, Version=4.3.1.0, Culture=neutral, Publickeytoken=b03f5f7f11d50a3a" and "System.Security.Cryptography.Cng, Version=4.3.1.0, Culture=neutral, Publickeytoken=b03f5f7f11d50a3a"
I tried several "solutions" that could solve the problem, but none solved the problem yet.
Apparently the property
PrivateKey
of the objectx509Cert
is not the type who is trying to do theCast
. Confirm inDebug
if this is the guy.– João Martins
@Joãomartins I saw some examples on the internet this way, there is some other way to pass the PIN via code ?
– Mariana
I’m not saying the code is wrong, but the
Cast
is for sure. You have to see what kind returnsx509Cert.PrivateKey
.– João Martins
@Joãomartins I checked on
Debug
ParentWindowHandle = '((System.Security.Cryptography.RSACng)x509Cert.PrivateKey).Key.ParentWindowHandle' threw an exception of type 'Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException', realmente ele é RSACng, porém não sei como corrigir.– Mariana
CryptographicException:
The key value is not an RSA or DSA key or the key is unreadable. https://docs.microsoft.com/pt-br/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.privatekey?view=netframework-4.7.2– Leandro Angelo
@Joãomartins has some solution to help me ?? I could not solve it
– Mariana
@Leandroangelo took a look at the link, but it did not help me, I need to pass the PIN through the code, but every way I try, returns me this error, I do not know how to convert.
– Mariana
How about this:
Key = (System.Security.Cryptography.RSACryptoServiceProvider).PrivateKey
?– Reginaldo Rigo
@Reginaldorigo the problem happens, because I need to pass the certificate PIN directly in the code, so that the user does not need to type every time to sign the file, but it seems that in older versions, this my code above works, and the most recent does not work, every way I try it returns me this same error.
– Mariana
Hmmmm. I have not tested and I have no way to test now. But I think that the way I passed you did not return the error Invalidcastexception. Or it will come back null or the key. Or any exception for trying to access the variable of an unspecified object.
– Reginaldo Rigo
@Reginaldorigo I’ve tried this way, I need to put the certificate before
PrivateKey
, the mistake he returned to me was:InvalidCastException: Unable to cast object of type 'System.Security.Cryptography.RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'.
– Mariana
Last try. Key =
(System.Security.Cryptography.RSACryptoServiceProvider)(x509Cert.PrivateKey)
– Reginaldo Rigo
@Reginaldorigo same thing, I looked for examples of
RSACng
but unsuccessfully, I am failing to solve this problem.– Mariana