Warm tip: This article is reproduced from stackoverflow.com, please click
.net-core cryptography rsa

Using Asymmetric Key on .Net Core

发布于 2020-04-07 10:08:50

I am trying to run code from this sample

https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-store-asymmetric-keys-in-a-key-container

Under .NetCore 2.0 (Web application).

However when I try to execute any line using

CspParameters

I get the following error

'CspParameters' requires Windows Cryptographic API (CAPI), which is not available on this platform.

Suggestions please on how I work around this. Thanks.

Questioner
MHugh
Viewed
76
bartonjs 2018-02-15 01:09

.NET does not store cryptographic keys, that's ultimately a feature that is (or isn't) provided by the cryptographic platform it builds on top of.

To use CspParameters with .NET Core you have to run on Windows; because that's a very thin wrapper over the (old) Windows Cryptographic API. You can't use it in UAP, because UAP only allows the newer Cryptography: Next Generation (CNG) API.

macOS can store keys in a Keychain, but .NET Core doesn't provide API to read them out.

Linux (OpenSSL) does not have any key storage mechanism other than "save this to a file and load it again", but .NET Core does not support loading asymmetric keys from files.

The only way to accomplish your goal in a cross-platform mechanism is to have your asymmetric key associated with an X.509 certificate. If you build the X509Certificate2 object for which HasPrivateKey returns true you can save it to a PFX/PKCS#12 file and then load from that file; or you can add it to an X509Store instance (the "My" store for CurrentUser is the one that works best across the platforms) and then read it back from the X509Store instance.

Despite the page you referenced claiming to be written in 2017, what it really means is the content was moved from its previous location on msdn.microsoft.com on that date. The original page was written in 2008 (at least, that's the first hit on web.archive.org), so it long predated .NET Core.