John
John

Reputation: 736

How to use RSA in .net Core

I try to encrypt a file with RSA, but it don't have the toXmlString() and fromXmlString method. how to use RSA class in .net core ? And I want to encrypt with private key and decrypt with public key ,so others can only read this file but can't generate this file , Is that possible ?

Upvotes: 3

Views: 5865

Answers (2)

bartonjs
bartonjs

Reputation: 33266

While the ToXmlString and FromXmlString methods are not available, ImportParameters(RSAParameters) and ExportParameters(bool) are.

If you have existing XML formatted values that you need to continue to read, the XML format of the keys isn't very interesting:

Public only:

<RSAKeyValue>
  <Modulus>[base64-encoded value]</Modulus>
  <Exponent>[base64-encoded value]</Exponent>
</RSAKeyValue>

Public+Private:

<RSAKeyValue>
  <Modulus>[base64-encoded value]</Modulus>
  <Exponent>[base64-encoded value]</Exponent>
  <P>[base64-encoded value]</P>
  <Q>[base64-encoded value]</Q>
  <DP>[base64-encoded value]</DP>
  <DQ>[base64-encoded value]</DQ>
  <InverseQ>[base64-encoded value]</InverseQ>
</RSAKeyValue>

(where each named XML element maps to the field of the same name on the RSAParameters struct)

Otherwise you can/should use whatever serialization mechanism is the best suited for you and your application.

Upvotes: 3

Legends
Legends

Reputation: 22702

Currently you get less when using pure .net core:

In the end this means you can build on .NET Core today, and expect more features to light up later, so things will get easier as time goes on. Whether you want to be an early adopter with the less featured framework now, or jump in later when more features have been added and the third party eco-system has caught up is going to be a (tough) decision that we all have to make on your own.

You will have them available if you target the full .net framework in .net core.

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "net461": {}
  }
}

They won't be available with for example:

"frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

Upvotes: 2

Related Questions