Why did the SignedCms.ComputeSignature () method choose a Provider Public Key exception?

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;

// Long ugly code to decrypt private key omitted...                

var rsa = new RSACryptoServiceProvider(new CspParameters
{
    Flags = CspProviderFlags.UseMachineKeyStore
});

try
{
    rsa.ImportCspBlob(decryptedKey);

    result.PrivateKey = rsa;

    return result;
}
catch
{
    rsa.Dispose();
    throw;
}
+3
1

, , RSACryptoServiceProvider. .NET 4.0 ( "CLR {GUID}" ). .NET 4.0 null. (, , null, , CspParameters.)

, RSACryptoService . , , - , , , . ( , " ".)

, . :

var rsa = new RSACryptoServiceProvider(new CspParameters
{
    Flags = CspProviderFlags.UseMachineKeyStore,

    KeyContainerName = String.Format("MyPrefix {{{0}}}", Guid.NewGuid())
});

try {
    rsa.PersistKeyInCsp = false; // ensure key is deleted when provider is disposed

    rsa.ImportCspBlob(decryptedKey);

    result.PrivateKey = rsa;

    return result;
}
+6

All Articles