How to use RSA in .net Core

岁酱吖の 提交于 2019-12-12 09:53:20

问题


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 ?


回答1:


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.




回答2:


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"
      ]
    }
  },


来源:https://stackoverflow.com/questions/38288574/how-to-use-rsa-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!