This may seem like a simple question, but it really made me scratch my head. The problem is that our code worked perfectly, working on the .NET 3.5 platform, but now that we have switched to .NET 4.0, we get this error. Here is the relevant code:
SignedCms signed = new SignedCms(content, false);
CmsSigner signer = new CmsSigner(
SubjectIdentifierType.IssuerAndSerialNumber,
signingCertificate);
signed.ComputeSignature(signer);
Once again, on .NET 3.5 this works great. But now that our project is targeting .NET 4.0, it throws a CryptographicException when using the same certificate.
[CryptographicException: Provider public key is invalid.]
at System.Security.Cryptography.Pkcs.PkcsUtils.CreateSignerEncodeInfo(CmsSigner signer, Boolean silent)
at System.Security.Cryptography.Pkcs.SignedCms.Sign(CmsSigner signer, Boolean silent)
at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer, Boolean silent)
at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer)
...
Any ideas what might cause this?
UPDATE
, , , . , . , , - . . , .NET 4. :
var result = new X509Certificate2(certificate);
byte[] decryptedKey;
var rsa = new RSACryptoServiceProvider(new CspParameters
{
Flags = CspProviderFlags.UseMachineKeyStore
});
try
{
rsa.ImportCspBlob(decryptedKey);
result.PrivateKey = rsa;
return result;
}
catch
{
rsa.Dispose();
throw;
}