Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4059322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:11:37+00:00 2026-05-20T15:11:37+00:00

This one may seem like a simple question, but it’s really got me scratching

  • 0

This one may seem like a simple question, but it’s really got me scratching my head. The trouble is that our code was working perfectly while running on the .NET framework 3.5, but now that we’ve switched to .NET 4.0 we’re getting this error. Here’s 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 fine. But now that our project targets .NET 4.0, it throws a CryptographicException when using the exact same certificate.

[CryptographicException: Provider's 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 could cause this?

UPDATE

After digging a little deeper, I’ve discovered that the problem only occurs when I deserialize the signing certificate myself. If I load it from the machine’s store, everything works. So, evidentally, there’s something wrong with my deserialization code. This code hasn’t changed. The only difference is that it’s now targeting .NET 4. Here’s the code:

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:11:37+00:00Added an answer on May 20, 2026 at 3:11 pm

    It turns out the problem was a subtle, breaking change in the RSACryptoServiceProvider. Prior to .NET 4.0, a new instance of this class would have a randomly generated key container name (of the form “CLR{GUID}”). In .NET 4.0, it leaves the key container name as null. (Or perhaps it’s setting it to null because that’s what I passed with the CspParameters.)

    In any case, this means that each subsequent RSACryptoService provider overwrites the key pairs of any previous providers. When my code finally got around signing something, the key pair associated with the certificate had been overwritten by a different certificate’s key pair. (Thus the “public key is invalid” error.)

    The solution was to simply generate a random name myself. The change to my deserialization code is as follows:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem a trivial question, but it's one that's bothered me a lot
This may seem like a very simple question, but I have been struggling with
This may seem like a simple question, but I can't find the definite answer:
This seems like a simple question but I can't seem to find an answer
Sorry if this question may be simple, but I can't seem to find an
This may seem like a simple and straightforward question and it probably has a
NOTE : In hindsight, I realize this may seem like a stupid question, but
That may seem like a broad question, but I'm talking specifically here about apps
I realise that this may seem like a stupid request but I'm going to
This may seem like a silly question, but I've looked high and low for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.