question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RsaSecurityKey.HasPrivateKey throws a NotSupportedException when the underlying algorithm instance is a RSACng on .NET Desktop

See original GitHub issue
System.NotSupportedException: Méthode non prise en charge.
   à System.Security.Cryptography.RSA.DecryptValue(Byte[] rgb)
   à Microsoft.IdentityModel.Tokens.RsaSecurityKey.get_HasPrivateKey()

In > .NET 4.6, RSA.DecryptValue and RSA.EncryptValue are considered as “obsolete” (which is a very debatable choice, IMHO) and are now virtual instead of abstract. It wouldn’t be a big deal if RSACng implemented them… but it does not. The sad news is that IdentityModel can’t handle RSACng-based keys on .NET Desktop as it uses RSA.DecryptValue to “determine” if there’s a private key (I guess IM 4.5 Desktop is similarly impacted).

Ideally, we should use Decrypt(byte[] data, RSAEncryptionPadding padding) or SignData(byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) but these APIs are not part of .NET 4.5. One option might be to re-target IM 5 to require .NET 4.6. Another one is to use reflection to use these new APIs when they exist.

Reference: http://referencesource.microsoft.com/#mscorlib/system/security/cryptography/rsa.cs,193

/cc @brentschmaltz @polita

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:32 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
brentschmaltzcommented, Sep 7, 2017

@ohadschn glad it worked. @PinpointTownes fair enough. POR is to get this into 5.2.0 release.

0reactions
kevinchaletcommented, Oct 15, 2017

@PinpointTownes please feel free to open an issue (again) for a new TFM for 4.6.0+.

I’m fine doing that, but this ticket should be re-open to fix the existing API on existing targets.

I considered using bool?, but landed on an enum as it seemed to read better.

Actually, it could even be a bool, because the decrypting/encrypting/signing/verifying operation either fails or succeeds (there’s no intermediate state).

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - .NET Framework RsaCng exception
When I try executing it, rsab.Decrypt() throws a Cryptographic exception with the message: "The parameter is incorrect.". Why is this happening?
Read more >
RsaSecurityKey Class (System.IdentityModel.Tokens)
Initializes a new instance of the RsaSecurityKey class using the specified RSA algorithm. Properties. KeySize. Gets the size, in bits, of the key....
Read more >
Accessing and using certificate private keys in .NET ...
Note: this example throws exception, when you access PrivateKey property and private key is stored in KSP. This means that KSP keys ...
Read more >
RSA.DecryptValue(Byte[]) Method
Exceptions. NotSupportedException. This method call is not supported. This exception is thrown starting with the .NET Framework 4.6. Remarks.
Read more >
How to use RSA in .NET: RSACryptoServiceProvider vs. ...
Both classes are sealed and derive from the base RSA class and implement their members. However, both classes throw a NotSupportedException when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found